-
Notifications
You must be signed in to change notification settings - Fork 0
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
93c1e56
commit 734a7b1
Showing
11 changed files
with
122 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MediatR.AspNet { | ||
public class ApplicationErrorDetails { | ||
public string Code { get; set; } | ||
public int Status { get; set; } | ||
public string Message { get; set; } | ||
} | ||
} |
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,23 @@ | ||
using System; | ||
|
||
namespace MediatR.AspNet { | ||
public abstract class BaseApplicationException : Exception { | ||
private string _code; | ||
private int _status; | ||
private string _message; | ||
|
||
public BaseApplicationException(string code, int status, string message) { | ||
_code = code; | ||
_status = status; | ||
_message = message; | ||
} | ||
|
||
public ApplicationErrorDetails ToProblemDetails() { | ||
return new ApplicationErrorDetails { | ||
Code = _code, | ||
Status = _status, | ||
Message = _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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using MediatR.AspNet.Exceptions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet { | ||
public class ExceptionMiddleware { | ||
private readonly RequestDelegate _request; | ||
|
||
|
||
public ExceptionMiddleware(RequestDelegate request) { | ||
_request = request; | ||
} | ||
|
||
public async Task InvokeAsynce(HttpContext context) { | ||
try { | ||
await _request(context); | ||
} | ||
catch (NotFoundException e) { | ||
context.Response.StatusCode = (int) HttpStatusCode.NotFound; | ||
var error = e.Message; | ||
await context.Response.WriteAsync(error); | ||
} | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
MediatR.AspNet/MediatR.AspNet/Exceptions/DeleteNotAllowedException.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class DeleteNotAllowedException : Exception { | ||
public DeleteNotAllowedException() : base("Cannot delete entity") { } | ||
public DeleteNotAllowedException(Type entityType) : base($"Cannot delete {entityType.Name}") { } | ||
public class DeleteNotAllowedException : BaseApplicationException { | ||
public DeleteNotAllowedException() : base("DeleteNotAllowed",StatusCodes.Status400BadRequest, "Cannot delete entity") { } | ||
public DeleteNotAllowedException(Type entityType) : base("DeleteNotAllowed",StatusCodes.Status400BadRequest,$"Cannot delete {entityType.Name}") { } | ||
public DeleteNotAllowedException(Type entityType, string id) : base( | ||
"DeleteNotAllowed",StatusCodes.Status400BadRequest, | ||
$"Cannot delete {entityType.Name} with id {id}") { } | ||
public DeleteNotAllowedException(string message, Exception innerException) : base(message, innerException) { } | ||
public DeleteNotAllowedException(string message) : base(message) { } | ||
public DeleteNotAllowedException(string message) : base("DeleteNotAllowed",StatusCodes.Status400BadRequest, message) { } | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
MediatR.AspNet/MediatR.AspNet/Exceptions/ExistsException.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class ExistsException : Exception { | ||
public ExistsException() : base("Entity already exists") { } | ||
public ExistsException(Type entityType) : base($"{entityType.Name} already exists") { } | ||
public class ExistsException : BaseApplicationException { | ||
public ExistsException() : base("Exists", StatusCodes.Status409Conflict, "Entity already exists") { } | ||
public ExistsException(Type entityType) : base("Exists", StatusCodes.Status409Conflict, $"{entityType.Name} already exists") { } | ||
public ExistsException(Type entityType, string id) : base( | ||
"Exists", StatusCodes.Status409Conflict, | ||
$"{entityType.Name} with id {id} already exists") { } | ||
public ExistsException(string message, Exception innerException) : base(message, innerException) { } | ||
public ExistsException(string message) : base(message) { } | ||
public ExistsException(string message) : base("Exists", StatusCodes.Status409Conflict, message) { } | ||
} | ||
} | ||
} |
11 changes: 5 additions & 6 deletions
11
MediatR.AspNet/MediatR.AspNet/Exceptions/ExternalServiceFailureException.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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class ExternalServiceFailureException : Exception { | ||
public ExternalServiceFailureException() : base("External service failed") { } | ||
public ExternalServiceFailureException(string message, Exception innerException) : base(message, innerException) { } | ||
public ExternalServiceFailureException(string message) : base(message) { } | ||
public class ExternalServiceFailureException : BaseApplicationException { | ||
public ExternalServiceFailureException() : base("ExternalServiceFailure", StatusCodes.Status502BadGateway, "External service failed") { } | ||
public ExternalServiceFailureException(string message) : base("ExternalServiceFailure", StatusCodes.Status502BadGateway, message) { } | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
MediatR.AspNet/MediatR.AspNet/Exceptions/NotAuthorizedException.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class NotAuthorizedException : Exception { | ||
public NotAuthorizedException() : base("User is not allowed to access entity") { } | ||
public NotAuthorizedException(Type entityType) : base($"User is not allowed to access {entityType.Name}") { } | ||
public class NotAuthorizedException : BaseApplicationException { | ||
public NotAuthorizedException() : base("NotAuthorized", StatusCodes.Status401Unauthorized, "User is not allowed to access entity") { } | ||
public NotAuthorizedException(Type entityType) : base("NotAuthorized", StatusCodes.Status401Unauthorized, $"User is not allowed to access {entityType.Name}") { } | ||
public NotAuthorizedException(Type entityType, string id) : base( | ||
"NotAuthorized", StatusCodes.Status401Unauthorized, | ||
$"User is not allowed to access {entityType.Name} with id {id}") { } | ||
public NotAuthorizedException(string message, Exception innerException) : base(message, innerException) { } | ||
public NotAuthorizedException(string message) : base(message) { } | ||
public NotAuthorizedException(string message) : base("NotAuthorized", StatusCodes.Status401Unauthorized, message) { } | ||
} | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
MediatR.AspNet/MediatR.AspNet/Exceptions/NotFoundException.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class NotFoundException : Exception { | ||
public NotFoundException() : base("Entity not found") { } | ||
public NotFoundException(Type entityType) : base($"{entityType.Name} not found") { } | ||
public NotFoundException(Type entityType, string id) : base($"{entityType.Name} not found with id {id}") { } | ||
public NotFoundException(string message, Exception innerException) : base(message, innerException) { } | ||
public NotFoundException(string message) : base(message) { } | ||
public class NotFoundException : BaseApplicationException { | ||
public NotFoundException() : base("NotFound", StatusCodes.Status404NotFound, "Entity not found") { } | ||
public NotFoundException(Type entityType) : base("NotFound", StatusCodes.Status404NotFound, $"{entityType.Name} not found") { } | ||
public NotFoundException(Type entityType, string id) : base("NotFound", StatusCodes.Status404NotFound, $"{entityType.Name} not found with id {id}") { } | ||
public NotFoundException(string message) : base("NotFound", StatusCodes.Status404NotFound, message) { } | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
MediatR.AspNet/MediatR.AspNet/Exceptions/OperationNotAllowedException.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class OperationNotAllowedException : Exception { | ||
public OperationNotAllowedException() : base("Cannot make operation on entity") { } | ||
public OperationNotAllowedException(Type entityType) : base($"Cannot make operation on {entityType.Name}") { } | ||
public class OperationNotAllowedException : BaseApplicationException { | ||
public OperationNotAllowedException() : base("OperationNotAllowed", StatusCodes.Status403Forbidden, "Cannot make operation on entity") { } | ||
public OperationNotAllowedException(Type entityType) : base("OperationNotAllowed", StatusCodes.Status403Forbidden, $"Cannot make operation on {entityType.Name}") { } | ||
public OperationNotAllowedException(Type entityType, string id) : base( | ||
"OperationNotAllowed", StatusCodes.Status403Forbidden, | ||
$"Cannot make operation on {entityType.Name} with id {id}") { } | ||
public OperationNotAllowedException(string message, Exception innerException) : base(message, innerException) { } | ||
public OperationNotAllowedException(string message) : base(message) { } | ||
public OperationNotAllowedException(string message) : base("OperationNotAllowed", StatusCodes.Status403Forbidden, message) { } | ||
} | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
MediatR.AspNet/MediatR.AspNet/Exceptions/UpdateNotAllowedException.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MediatR.AspNet.Exceptions { | ||
public class UpdateNotAllowedException : Exception { | ||
public UpdateNotAllowedException() : base("Cannot update entity") { } | ||
public UpdateNotAllowedException(Type entityType) : base($"Cannot update {entityType.Name}") { } | ||
public class UpdateNotAllowedException : BaseApplicationException { | ||
public UpdateNotAllowedException() : base("UpdateNotAllowed", StatusCodes.Status409Conflict, "Cannot update entity") { } | ||
public UpdateNotAllowedException(Type entityType) : base("UpdateNotAllowed", StatusCodes.Status409Conflict, $"Cannot update {entityType.Name}") { } | ||
public UpdateNotAllowedException(Type entityType, string id) : base( | ||
"UpdateNotAllowed", StatusCodes.Status409Conflict, | ||
$"Cannot update {entityType.Name} with id {id}") { } | ||
public UpdateNotAllowedException(string message, Exception innerException) : base(message, innerException) { } | ||
public UpdateNotAllowedException(string message) : base(message) { } | ||
public UpdateNotAllowedException(string message) : base("UpdateNotAllowed", StatusCodes.Status409Conflict, message) { } | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
MediatR.AspNet/MediatR.AspNet/ProblemsDetails/NotFoundExtensions.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,18 @@ | ||
using MediatR.AspNet.Exceptions; | ||
|
||
namespace MediatR.AspNet.ProblemsDetails { | ||
public class NotFoundExtensions { | ||
public static NotFoundProblemDetails ToProblemDetails(this NotFoundException ex) { | ||
var error = new NotFoundProblemDetails { | ||
Type = "test", | ||
Status = 404 | ||
}; | ||
return error; | ||
} | ||
} | ||
} | ||
// stwórz klasę ApplicationErrrorDetails która będzie domyślna dla wszystkich moich wyjątków | ||
// powinna zawierać: status, code błedu, message | ||
// klasa BaseApplicationException, będzie miała metodę ToProblemDeatils, ma dziedziczyć z exception, ma mieć konstruktor | ||
// który przyjmie wszystko co potrzebne dla problem deatils | ||
// w moim middleware będe miała try i catch BaseApplicationExceptionw |