-
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.
* Added editing product after note recognized by photo * Fill product form values after note recognized * Fixed double toggle dialog bug * Fix not closing dialog after edit * Added isProblemDetailsError type guard * Show error message from non successful recognize note response * Handle OpenAI errors as ProblemDetails * Refactor error handling, use Result pattern * Handle empty ProblemDetails and unknown error responses * Handle no food found on photo case * Added new exception handler with error handling tests * Removed unused folder
- Loading branch information
Showing
8 changed files
with
138 additions
and
117 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/backend/src/FoodDiary.API/ErrorHandling/ExceptionHandler.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,43 @@ | ||
using System; | ||
using System.ClientModel; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FoodDiary.Domain.Exceptions; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FoodDiary.API.ErrorHandling; | ||
|
||
public class ExceptionHandler( | ||
ILogger<ExceptionHandler> logger, | ||
IProblemDetailsService problemDetailsService) : IExceptionHandler | ||
{ | ||
public async ValueTask<bool> TryHandleAsync( | ||
HttpContext httpContext, | ||
Exception exception, | ||
CancellationToken cancellationToken) | ||
{ | ||
logger.LogError("{Exception}", exception); | ||
|
||
var (statusCode, detail) = exception switch | ||
{ | ||
ImportException e => (StatusCodes.Status400BadRequest, e.Message.Trim()), | ||
ClientResultException e => (StatusCodes.Status500InternalServerError, e.Message.Trim()), | ||
_ => (StatusCodes.Status500InternalServerError, "Something went wrong") | ||
}; | ||
|
||
httpContext.Response.StatusCode = statusCode; | ||
|
||
return await problemDetailsService.TryWriteAsync(new ProblemDetailsContext | ||
{ | ||
HttpContext = httpContext, | ||
Exception = exception, | ||
ProblemDetails = | ||
{ | ||
Title = "An error occurred", | ||
Detail = detail | ||
} | ||
}); | ||
} | ||
} |
90 changes: 0 additions & 90 deletions
90
src/backend/src/FoodDiary.API/Middlewares/ExceptionHandlerMiddleware.cs
This file was deleted.
Oops, something went wrong.
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
10 changes: 0 additions & 10 deletions
10
src/backend/src/FoodDiary.Domain/Exceptions/AccessDeniedException.cs
This file was deleted.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
src/backend/tests/FoodDiary.ComponentTests/Scenarios/ErrorHandling/ErrorHandlingContext.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,51 @@ | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using FoodDiary.ComponentTests.Infrastructure; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FoodDiary.ComponentTests.Scenarios.ErrorHandling; | ||
|
||
public class ErrorHandlingContext(FoodDiaryWebApplicationFactory factory, InfrastructureFixture infrastructure) | ||
: BaseContext(factory, infrastructure) | ||
{ | ||
private HttpResponseMessage? _response; | ||
private HttpStatusCode _statusCode; | ||
|
||
public Task Given_application_is_broken_because_of_an_unhandled_exception(Exception exception) | ||
{ | ||
Factory = Factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
services.Configure<MvcOptions>(options => | ||
{ | ||
options.Filters.Add(new FakeExceptionActionFilter(exception)); | ||
}); | ||
}); | ||
}); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task When_user_is_trying_to_access_resource(string resource) | ||
{ | ||
_response = await ApiClient.GetAsync(resource); | ||
_statusCode = _response.StatusCode; | ||
} | ||
|
||
public Task Then_response_has_status(HttpStatusCode status) | ||
{ | ||
_statusCode.Should().Be(status); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task Then_response_is_problem_details() | ||
{ | ||
var problemDetails = await _response!.Content.ReadFromJsonAsync<ProblemDetails>(); | ||
problemDetails!.Status.Should().Be((int)_statusCode); | ||
problemDetails.Title.Should().NotBeNullOrWhiteSpace(); | ||
problemDetails.Detail.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/backend/tests/FoodDiary.ComponentTests/Scenarios/ErrorHandling/ErrorHandlingTests.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,24 @@ | ||
using System.Net; | ||
using FoodDiary.ComponentTests.Infrastructure; | ||
|
||
namespace FoodDiary.ComponentTests.Scenarios.ErrorHandling; | ||
|
||
public class ErrorHandlingTests(FoodDiaryWebApplicationFactory factory, InfrastructureFixture infrastructure) : | ||
ScenarioBase<ErrorHandlingContext>(factory, infrastructure) | ||
{ | ||
protected override ErrorHandlingContext CreateContext( | ||
FoodDiaryWebApplicationFactory factory, | ||
InfrastructureFixture infrastructure) => new(factory, infrastructure); | ||
|
||
[Scenario] | ||
public Task I_receive_unhandled_errors_in_problem_details_format() | ||
{ | ||
var exception = new Exception("some error"); | ||
|
||
return Run( | ||
c => c.Given_application_is_broken_because_of_an_unhandled_exception(exception), | ||
c => c.When_user_is_trying_to_access_resource("/api/v1/auth/status"), | ||
c => c.Then_response_has_status(HttpStatusCode.InternalServerError), | ||
c => c.Then_response_is_problem_details()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ckend/tests/FoodDiary.ComponentTests/Scenarios/ErrorHandling/FakeExceptionActionFilter.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,15 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace FoodDiary.ComponentTests.Scenarios.ErrorHandling; | ||
|
||
public class FakeExceptionActionFilter(Exception exception) : IActionFilter | ||
{ | ||
public void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
throw exception; | ||
} | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
} | ||
} |