@@ -11,16 +11,15 @@ namespace Voltiq.API.Controllers;
1111[ Route ( "api/v{version:apiVersion}/[controller]" ) ]
1212public abstract class BaseApiController : ControllerBase
1313{
14- [ field: AllowNull , MaybeNull ]
14+ [ field: AllowNull ]
15+ [ field: MaybeNull ]
1516 protected ISender Sender =>
1617 field ??= HttpContext . RequestServices . GetRequiredService < ISender > ( ) ;
1718
1819 protected IActionResult ToErrorResult ( List < Error > ? errors )
1920 {
2021 if ( errors is null || errors . Count == 0 )
21- {
2222 return Problem ( title : ResourceErrorMessages . TITULO_ERRO_INESPERADO , statusCode : 500 ) ;
23- }
2423
2524 return errors . All ( error => error . Type == ErrorType . Validation )
2625 ? BuildValidationProblem ( errors )
@@ -29,21 +28,18 @@ protected IActionResult ToErrorResult(List<Error>? errors)
2928
3029 private ObjectResult BuildProblem ( Error error )
3130 {
32- var statusCode = error . Type switch
31+ var ( statusCode , title ) = error . Type switch
3332 {
34- ErrorType . Conflict => StatusCodes . Status409Conflict ,
35- ErrorType . Validation => StatusCodes . Status400BadRequest ,
36- ErrorType . NotFound => StatusCodes . Status404NotFound ,
37- ErrorType . Unauthorized => StatusCodes . Status401Unauthorized ,
38- _ => StatusCodes . Status500InternalServerError
39- } ;
40-
41- var title = error . Type switch
42- {
43- ErrorType . Conflict => ResourceErrorMessages . TITULO_CONFLITO ,
44- ErrorType . NotFound => ResourceErrorMessages . TITULO_NAO_ENCONTRADO ,
45- ErrorType . Unauthorized => ResourceErrorMessages . TITULO_NAO_AUTORIZADO ,
46- _ => ResourceErrorMessages . TITULO_ERRO_INESPERADO
33+ ErrorType . Conflict => ( StatusCodes . Status409Conflict ,
34+ ResourceErrorMessages . TITULO_CONFLITO ) ,
35+ ErrorType . NotFound => ( StatusCodes . Status404NotFound ,
36+ ResourceErrorMessages . TITULO_NAO_ENCONTRADO ) ,
37+ ErrorType . Unauthorized => ( StatusCodes . Status401Unauthorized ,
38+ ResourceErrorMessages . TITULO_NAO_AUTORIZADO ) ,
39+ ErrorType . Validation => ( StatusCodes . Status400BadRequest ,
40+ ResourceErrorMessages . TITULO_VALIDACAO ) ,
41+ _ => ( StatusCodes . Status500InternalServerError ,
42+ ResourceErrorMessages . TITULO_ERRO_INESPERADO )
4743 } ;
4844
4945 return Problem (
@@ -57,12 +53,12 @@ private ActionResult BuildValidationProblem(List<Error> errors)
5753 var modelStateDictionary = new ModelStateDictionary ( ) ;
5854
5955 foreach ( var error in errors )
60- {
6156 modelStateDictionary . AddModelError (
6257 error . Code ,
6358 error . Description ) ;
64- }
6559
66- return ValidationProblem ( modelStateDictionary ) ;
60+ return ValidationProblem (
61+ title : ResourceErrorMessages . TITULO_VALIDACAO ,
62+ modelStateDictionary : modelStateDictionary ) ;
6763 }
6864}
0 commit comments