Table of Contents

Class FromResultToProblemDetailExtension

Namespace
Arc4u.AspNetCore.Results
Assembly
Arc4u.AspNetCore.Results.dll

Translates the errors of a failed FluentResults.Result into a ProblemDetails.

public static class FromResultToProblemDetailExtension
Inheritance
FromResultToProblemDetailExtension
Inherited Members

Remarks

The default translation is, in this order: an exceptional error is logged and hidden behind a generic technical error (status 500); ValidationErrors become a ValidationProblemDetails grouped by severity (status 422); a ProblemDetailError is copied field by field (status 500 when none is set); any other error gives a 400 with the error message. Replace the translation with SetFromErrorFactory(Func<IEnumerable<IError>, ProblemDetails>), or customize it while keeping the default rules for the other errors with SetFromErrorFactory(Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails>).

Properties

FromError

Gets the function used to translate a set of errors into a ProblemDetails: the default translation, or the one set by SetFromErrorFactory.

public static Func<IEnumerable<IError>, ProblemDetails> FromError { get; }

Property Value

Func<IEnumerable<IError>, ProblemDetails>

Remarks

The function always calls the translation registered when it runs. Do not call it from a custom translation, which would call itself until the process dies with a stack overflow: use SetFromErrorFactory(Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails>), which gives the default translation to your function.

Methods

SetFromErrorFactory(Func<IEnumerable<IError>, ProblemDetails>)

Replaces the function translating errors into a ProblemDetails for the whole process.

public static void SetFromErrorFactory(Func<IEnumerable<IError>, ProblemDetails> fromErrors)

Parameters

fromErrors Func<IEnumerable<IError>, ProblemDetails>

The function that builds the ProblemDetails from the errors of a failed result.

Remarks

The setting is static and not synchronized: call it once at startup, before requests are served. To keep the default translation for the errors your function does not handle, use SetFromErrorFactory(Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails>).

Exceptions

ArgumentNullException

fromErrors is null.

SetFromErrorFactory(Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails>)

Replaces the function translating errors into a ProblemDetails for the whole process, giving it the default translation to call for the errors it does not handle.

public static void SetFromErrorFactory(Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails> fromErrors)

Parameters

fromErrors Func<IEnumerable<IError>, Func<IEnumerable<IError>, ProblemDetails>, ProblemDetails>

The function that builds the ProblemDetails from the errors of a failed result; its second argument is the default translation.

Remarks

The setting is static and not synchronized: call it once at startup, before requests are served. The second argument of fromErrors is always the default Arc4u translation, so calling it never recurses. To restore the default translation, pass (errors, defaultFromError) => defaultFromError(errors).

Exceptions

ArgumentNullException

fromErrors is null.

ToGenericMessage(Result, bool)

Logs the failed result and returns a generic technical error, using the id of the current Activity (when any) as correlation id.

public static ProblemDetails ToGenericMessage(this Result result, bool unexpectedType = true)

Parameters

result Result

The result to log.

unexpectedType bool

When true (default) the problem is typed as an unexpected error with status 500; otherwise as an expected error with status 400.

Returns

ProblemDetails

A ProblemDetails with a generic message that does not disclose the errors.

ToGenericMessage(Result, string?, bool)

Logs the failed result and returns a generic technical error mentioning activityId as correlation id.

public static ProblemDetails ToGenericMessage(this Result result, string? activityId, bool unexpectedType = true)

Parameters

result Result

The result to log.

activityId string

The correlation id written in the detail; when null a message without id is returned.

unexpectedType bool

When true (default) the problem is typed as an unexpected error with status 500; otherwise as an expected error with status 400.

Returns

ProblemDetails

A ProblemDetails with a generic message that does not disclose the errors.

ToGenericMessage<TResult>(Result<TResult>, bool)

Logs the failed result and returns a generic technical error, using the id of the current Activity (when any) as correlation id.

public static ProblemDetails ToGenericMessage<TResult>(this Result<TResult> result, bool unexpectedType = true)

Parameters

result Result<TResult>

The result to log.

unexpectedType bool

When true (default) the problem is typed as an unexpected error with status 500; otherwise as an expected error with status 400.

Returns

ProblemDetails

A ProblemDetails with a generic message that does not disclose the errors.

Type Parameters

TResult

The type of the value carried by the result.

ToGenericMessage<TResult>(Result<TResult>, string?, bool)

Logs the failed result and returns a generic technical error mentioning activityId as correlation id.

public static ProblemDetails ToGenericMessage<TResult>(this Result<TResult> result, string? activityId, bool unexpectedType = true)

Parameters

result Result<TResult>

The result to log.

activityId string

The correlation id written in the detail; when null a message without id is returned.

unexpectedType bool

When true (default) the problem is typed as an unexpected error with status 500; otherwise as an expected error with status 400.

Returns

ProblemDetails

A ProblemDetails with a generic message that does not disclose the errors.

Type Parameters

TResult

The type of the value carried by the result.

ToProblemDetails(Result)

Translates the errors of a failed result into a ProblemDetails using FromError.

public static ProblemDetails ToProblemDetails(this Result result)

Parameters

result Result

The failed result.

Returns

ProblemDetails

The ProblemDetails describing the errors of the result.

Remarks

Calling it on a successful result is a programming error: the problem details are then built from an exceptional error wrapping an UnreachableException, which is translated to the generic technical error. The result itself is not modified.

ToProblemDetails<TResult>(Result<TResult>)

Translates the errors of a failed result into a ProblemDetails using FromError.

public static ProblemDetails ToProblemDetails<TResult>(this Result<TResult> result)

Parameters

result Result<TResult>

The failed result.

Returns

ProblemDetails

The ProblemDetails describing the errors of the result.

Type Parameters

TResult

The type of the value carried by the result.

Remarks

Calling it on a successful result is a programming error: the problem details are then built from an exceptional error wrapping an UnreachableException, which is translated to the generic technical error. The result itself is not modified.