forked from github/codespaces-blank
-
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.
Feat: Added Middleware for catching double register
error; now returns general error. Still needs work
- Loading branch information
1 parent
8085bc2
commit 9e181c2
Showing
2 changed files
with
37 additions
and
1 deletion.
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,32 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace TodoistClone.Api.Middleware; | ||
|
||
public class ErrorHandlingMiddlware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ErrorHandlingMiddlware(RequestDelegate next) { | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) { | ||
try { | ||
await _next(context); | ||
} | ||
catch (Exception exception) { | ||
await HandleExceptionAsync(context, exception); | ||
} | ||
} | ||
|
||
private static Task HandleExceptionAsync(HttpContext context, Exception exception) { | ||
var code = HttpStatusCode.InternalServerError; | ||
var result = JsonSerializer.Serialize(new { error = "An error occured while processing your request." }); | ||
context.Response.ContentType = "application/json"; | ||
context.Response.StatusCode = (int)code; | ||
return context.Response.WriteAsync(result); | ||
} | ||
} | ||
|
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