-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffcb034
commit f74377c
Showing
7 changed files
with
95 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/SwiftLink.Presentation/Extensions/MapToProblemDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using SwiftLink.Shared; | ||
|
||
namespace SwiftLink.Presentation.Extensions; | ||
|
||
public static class MapToProblemDetailsExtension | ||
{ | ||
public static Result MapToProblemDetails(this Result result) | ||
{ | ||
static int GetStatusCode(ErrorType type) | ||
=> type switch | ||
{ | ||
ErrorType.Validation => StatusCodes.Status400BadRequest, | ||
ErrorType.Failure => StatusCodes.Status500InternalServerError, | ||
ErrorType.NotFound => StatusCodes.Status404NotFound, | ||
ErrorType.None => StatusCodes.Status500InternalServerError, | ||
_ => StatusCodes.Status500InternalServerError | ||
}; | ||
|
||
static string GetTitle(ErrorType type) | ||
=> type switch | ||
{ | ||
ErrorType.Validation => "Bad Request", | ||
ErrorType.Failure => "Internal Server Error", | ||
ErrorType.NotFound => "Not Found", | ||
ErrorType.None => "Internal Server Error", | ||
_ => "Internal Server Error" | ||
}; | ||
|
||
return Result.Problem( | ||
status: GetStatusCode(result.Error.Type), | ||
type: result.Error.Type.ToString(), | ||
title: GetTitle(result.Error.Type), | ||
detail: result.Error.Message | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,40 @@ | ||
namespace SwiftLink.Shared; | ||
public sealed record Error(string Code, string? Message = null) | ||
| ||
using System.Net.Http.Headers; | ||
|
||
namespace SwiftLink.Shared; | ||
public sealed record Error | ||
{ | ||
|
||
public static readonly Error None = new(string.Empty, string.Empty); | ||
public static readonly Error None = new(string.Empty, string.Empty, ErrorType.Failure); | ||
public static implicit operator Result(Error error) => Result.Failure(error); | ||
|
||
private Error(string code, string message, ErrorType type) | ||
{ | ||
Code = code; | ||
Message = message; | ||
Type = type; | ||
} | ||
|
||
public string Code { get; set; } | ||
public string Message { get; set; } | ||
public ErrorType Type { get; set; } | ||
|
||
public static Error NotFound(string code, string message) | ||
=> new(code, message, ErrorType.NotFound); | ||
|
||
public static Error Failure(string code, string message) | ||
=> new(code, message, ErrorType.Failure); | ||
|
||
public static Error Validation(string code, string message) | ||
=> new(code, message, ErrorType.Validation); | ||
|
||
|
||
} | ||
|
||
public enum ErrorType : byte | ||
{ | ||
Validation = 0, | ||
Failure = 1, | ||
NotFound = 2, | ||
None = 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters