MarcinOrlowski / laravel-api-response-builder

Builds nice, normalized and easy to consume REST JSON responses for Laravel powered APIs.
MIT License
721 stars 79 forks source link

Exception Handler is not working #218

Closed rizkyarlin closed 3 years ago

rizkyarlin commented 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. image

It doesn't return the code from ApiCodes.

It also happens in validation exception: image

rizkyarlin commented 3 years ago

I just found out the api_code should be int

MarcinOrlowski commented 3 years ago

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