Closed rizkyarlin closed 3 years ago
I can't manage to make the exception handler working as expected.
response_builder.php
'exception_handler' => [ \Illuminate\Validation\ValidationException::class => [ 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class, 'pri' => -100, 'config' => [ 'api_code' => \App\Exceptions\ApiCodes::INVALID_REQUEST, 'http_code' => \Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY, ], ], \Illuminate\Database\Eloquent\ModelNotFoundException::class => [ 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, 'pri' => -300, 'config' => [ 'api_code' => \App\Exceptions\ApiCodes::HTTP_GENERIC, 'http_code' => \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND, ], ], \Symfony\Component\HttpKernel\Exception\HttpException::class => [ 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, 'pri' => -100, 'config' => [ \Symfony\Component\HttpKernel\Exception\HttpException::class => [ // used by unauthenticated() to obtain api and http code for the exception \Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED => [ 'api_code' => \App\Exceptions\ApiCodes::UNAUTHORIZED, ], // Required by ValidationException handler \Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY => [ 'api_code' => \App\Exceptions\ApiCodes::INVALID_REQUEST, ], // default handler is mandatory and MUST have both `api_code` and `http_code` set. 'default' => [ 'api_code' => \App\Exceptions\ApiCodes::HTTP_GENERIC, 'http_code' => \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST, ], ], ], 'default' => [ 'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class, 'pri' => -127, 'config' => [ 'api_code' => \App\Exceptions\ApiCodes::UNCAUGHT_GENERIC, 'http_code' => \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR, ], ], ], ],
ApiCodes.php
class ApiCodes { /** * Generic Code */ public const HTTP_GENERIC = 'ERR-001'; public const UNCAUGHT_GENERIC = 'ERR-500'; /** * Auth related error code */ public const UNAUTHENTICATED = 'AUTH-001'; public const UNAUTHORIZED = 'AUTH-002'; /** * Validation related error code */ public const INVALID_REQUEST = 'VLD-001'; public const USER_NOT_FOUND = 'RSC-001'; }
Postman screenshoot.
It doesn't return the code from ApiCodes.
It also happens in validation exception:
I just found out the api_code should be int
Yes, these must be int. I will take a look to see if I can make it more clear. Closing as resolved. Please reopen if it's not
int
I can't manage to make the exception handler working as expected.
response_builder.php
ApiCodes.php
Postman screenshoot.
It doesn't return the code from ApiCodes.
It also happens in validation exception: