Skip to content

Commit

Permalink
Added CriticalError to ResultStatuses (#140) (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnochMtzR committed Sep 12, 2023
1 parent 31464ea commit 8e93188
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Ardalis.Result.AspNetCore/ResultStatusMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public ResultStatusMap AddDefaultMap()
.For(ResultStatus.NotFound, HttpStatusCode.NotFound, resultStatusOptions => resultStatusOptions
.With(NotFoundEntity))
.For(ResultStatus.Conflict, HttpStatusCode.Conflict, resultStatusOptions => resultStatusOptions
.With(ConflictEntity));
.With(ConflictEntity))
.For(ResultStatus.CriticalError, HttpStatusCode.InternalServerError, resultStatusOptions =>
resultStatusOptions
.With(CriticalEntity));
}

/// <summary>
Expand Down Expand Up @@ -139,6 +142,19 @@ private static ProblemDetails ConflictEntity(ControllerBase controller, IResult
Detail = result.Errors.Any() ? details.ToString() : null
};
}

private static ProblemDetails CriticalEntity(ControllerBase controller, IResult result)
{
var details = new StringBuilder("Next error(s) occured:");

foreach (var error in result.Errors) details.Append("* ").Append(error).AppendLine();

return new ProblemDetails
{
Title = "Something went wrong.",
Detail = result.Errors.Any() ? details.ToString() : null
};
}
}

public class ResultStatusOptions
Expand Down Expand Up @@ -217,4 +233,4 @@ public ResultStatusOptions With(Type responseType, Func<ControllerBase, IResult,
return this;
}
}
}
}
12 changes: 12 additions & 0 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,17 @@ public static Result<T> Conflict(params string[] errorMessages)
{
return new Result<T>(ResultStatus.Conflict) { Errors = errorMessages };
}

/// <summary>
/// Represents a critical error that occurred during the execution of the service.
/// Everything provided by the user was valid, but the service was unable to complete due to an exception.
/// See also HTTP 500 Internal Server Error: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors
/// </summary>
/// <param name="errorMessages">A list of string error messages.</param>
/// <returns>A Result<typeparamref name="T"/></returns>
public static Result<T> CriticalError(params string[] errorMessages)
{
return new Result<T>(ResultStatus.CriticalError) { Errors = errorMessages };
}
}
}
1 change: 1 addition & 0 deletions src/Ardalis.Result/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static Result<TDestination> Map<TSource, TDestination>(this Result<TSourc
case ResultStatus.Conflict: return result.Errors.Any()
? Result<TDestination>.Conflict(result.Errors.ToArray())
: Result<TDestination>.Conflict();
case ResultStatus.CriticalError: return Result<TDestination>.CriticalError(result.Errors.ToArray());
default:
throw new NotSupportedException($"Result {result.Status} conversion is not supported.");
}
Expand Down
1 change: 1 addition & 0 deletions src/Ardalis.Result/ResultStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public enum ResultStatus
Invalid,
NotFound,
Conflict,
CriticalError
}
}

0 comments on commit 8e93188

Please sign in to comment.