Decouple workflow services from HTTP result types
This commit is contained in:
@@ -112,6 +112,22 @@ internal static class EndpointHelpers
|
||||
|
||||
public static IResult UnauthorizedError(string detail = "Unauthorized") => Problem(StatusCodes.Status401Unauthorized, "Unauthorized", detail);
|
||||
|
||||
public static IResult ToHttpResult<T>(this ServiceResult<T> result, Func<T, IResult> onSuccess)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
return onSuccess(result.Value!);
|
||||
|
||||
return ToHttpError(result.Error!);
|
||||
}
|
||||
|
||||
public static IResult ToHttpResult(this ServiceResult<Unit> result, Func<IResult> onSuccess)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
return onSuccess();
|
||||
|
||||
return ToHttpError(result.Error!);
|
||||
}
|
||||
|
||||
public static bool IsSqliteConstraintViolation(DbUpdateException ex)
|
||||
{
|
||||
return ex.InnerException is SqliteException sqliteEx
|
||||
@@ -160,6 +176,18 @@ internal static class EndpointHelpers
|
||||
|| path.EndsWith(".avif", StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static IResult ToHttpError(ServiceError error)
|
||||
{
|
||||
return error.Code switch
|
||||
{
|
||||
ServiceErrorCode.BadRequest => BadRequestError(error.Detail),
|
||||
ServiceErrorCode.Unauthorized => UnauthorizedError(error.Detail),
|
||||
ServiceErrorCode.NotFound => NotFoundError(error.Detail),
|
||||
ServiceErrorCode.Conflict => ConflictError(error.Detail),
|
||||
_ => Problem(StatusCodes.Status500InternalServerError, "Internal Server Error", "Unhandled service error.")
|
||||
};
|
||||
}
|
||||
|
||||
public static HttpMessageHandler CreateImageValidationHandler()
|
||||
{
|
||||
return new SocketsHttpHandler
|
||||
|
||||
Reference in New Issue
Block a user