Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CriticalError to ResultStatuses (#140) #141

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading