-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] Error & Loading Interceptors #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7bebb28
[nit] rename toasts to toast
robchendev a47b3e1
[qol] configure base url with file replacement
robchendev 61136a5
[feat] add middleware to serialize exceptions
robchendev 4054a85
[feat] http error interceptor
robchendev 91aa932
[feat] add not found and server error routes
robchendev fcc4ce0
[feat] add buttons to invoke errors
robchendev 6f72c0a
[feat] buggy error endpoints to simulate error response
robchendev 4148795
[style] rename test-errors to sandbox, style sandbox
robchendev ef8f94a
[Feat] Loading Visual Feedback (#14)
robchendev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
using Backend.Data; | ||
using Backend.Entities; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Backend.Controllers; | ||
|
||
// This is a mock controller for testing purposes | ||
public class BuggyController(DataContext context) : BaseApiController | ||
{ | ||
[Authorize] | ||
[HttpGet("auth")] | ||
public ActionResult<string> GetAuth() | ||
{ | ||
return "secret text"; | ||
} | ||
|
||
[HttpGet("not-found")] | ||
public ActionResult<User> GetNotFound() | ||
{ | ||
var thing = context.Users.Find(-1); // produce not found user | ||
if (thing == null) return NotFound(); | ||
return thing; | ||
} | ||
|
||
[HttpGet("server-error")] | ||
public ActionResult<User> GetServerError() // 5xx = server error | ||
{ | ||
var thing = context.Users.Find(-1) ?? throw new Exception("A bad thing has happened"); // generate null reference error | ||
|
||
return thing; | ||
} | ||
|
||
[HttpGet("bad-request")] | ||
public ActionResult<string> GetBadRequest() // 4xx = user error | ||
{ | ||
return BadRequest("This was not a good request"); | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
namespace Backend.Errors; | ||
|
||
public class ApiException(int statusCode, string message, string? details) | ||
{ | ||
public int StatusCode { get; set; } = statusCode; | ||
public string Message { get; set; } = message; | ||
public string? Details { get; set; } = details; | ||
} |
This file contains hidden or 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 System.Net; | ||
using System.Text.Json; | ||
using Backend.Errors; | ||
|
||
namespace Backend.Middleware; | ||
|
||
public class ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger, IHostEnvironment env) | ||
{ | ||
// have to call InvokeAsync since its expected when we call the next method | ||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await next(context); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, ex.Message); | ||
context.Response.ContentType = "application/json"; | ||
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | ||
|
||
var response = env.IsDevelopment() | ||
? new ApiException(context.Response.StatusCode, ex.Message, ex.StackTrace) | ||
: new ApiException(context.Response.StatusCode, ex.Message, "Internal server error"); | ||
|
||
var options = new JsonSerializerOptions | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(response, options); | ||
|
||
await context.Response.WriteAsync(json); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should nest the exceptions incase we want to abstract the backend logic. i.e. service exception, repo exceptiion, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should set up the api to utilize swagger or something else for debugging