Skip to content

Commit 8e93188

Browse files
authored
Added CriticalError to ResultStatuses (#140) (#141)
1 parent 31464ea commit 8e93188

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/Ardalis.Result.AspNetCore/ResultStatusMap.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public ResultStatusMap AddDefaultMap()
3535
.For(ResultStatus.NotFound, HttpStatusCode.NotFound, resultStatusOptions => resultStatusOptions
3636
.With(NotFoundEntity))
3737
.For(ResultStatus.Conflict, HttpStatusCode.Conflict, resultStatusOptions => resultStatusOptions
38-
.With(ConflictEntity));
38+
.With(ConflictEntity))
39+
.For(ResultStatus.CriticalError, HttpStatusCode.InternalServerError, resultStatusOptions =>
40+
resultStatusOptions
41+
.With(CriticalEntity));
3942
}
4043

4144
/// <summary>
@@ -139,6 +142,19 @@ private static ProblemDetails ConflictEntity(ControllerBase controller, IResult
139142
Detail = result.Errors.Any() ? details.ToString() : null
140143
};
141144
}
145+
146+
private static ProblemDetails CriticalEntity(ControllerBase controller, IResult result)
147+
{
148+
var details = new StringBuilder("Next error(s) occured:");
149+
150+
foreach (var error in result.Errors) details.Append("* ").Append(error).AppendLine();
151+
152+
return new ProblemDetails
153+
{
154+
Title = "Something went wrong.",
155+
Detail = result.Errors.Any() ? details.ToString() : null
156+
};
157+
}
142158
}
143159

144160
public class ResultStatusOptions
@@ -217,4 +233,4 @@ public ResultStatusOptions With(Type responseType, Func<ControllerBase, IResult,
217233
return this;
218234
}
219235
}
220-
}
236+
}

src/Ardalis.Result/Result.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,17 @@ public static Result<T> Conflict(params string[] errorMessages)
194194
{
195195
return new Result<T>(ResultStatus.Conflict) { Errors = errorMessages };
196196
}
197+
198+
/// <summary>
199+
/// Represents a critical error that occurred during the execution of the service.
200+
/// Everything provided by the user was valid, but the service was unable to complete due to an exception.
201+
/// See also HTTP 500 Internal Server Error: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors
202+
/// </summary>
203+
/// <param name="errorMessages">A list of string error messages.</param>
204+
/// <returns>A Result<typeparamref name="T"/></returns>
205+
public static Result<T> CriticalError(params string[] errorMessages)
206+
{
207+
return new Result<T>(ResultStatus.CriticalError) { Errors = errorMessages };
208+
}
197209
}
198210
}

src/Ardalis.Result/ResultExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static Result<TDestination> Map<TSource, TDestination>(this Result<TSourc
2929
case ResultStatus.Conflict: return result.Errors.Any()
3030
? Result<TDestination>.Conflict(result.Errors.ToArray())
3131
: Result<TDestination>.Conflict();
32+
case ResultStatus.CriticalError: return Result<TDestination>.CriticalError(result.Errors.ToArray());
3233
default:
3334
throw new NotSupportedException($"Result {result.Status} conversion is not supported.");
3435
}

src/Ardalis.Result/ResultStatus.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public enum ResultStatus
99
Invalid,
1010
NotFound,
1111
Conflict,
12+
CriticalError
1213
}
1314
}

0 commit comments

Comments
 (0)