janephp icon indicating copy to clipboard operation
janephp copied to clipboard

Improve exception handling

Open odolbeau opened this issue 5 years ago • 2 comments

Right now, a dedicated exception is created for each endpoint for declared error responses. Each of those exceptions are independents.

When using responses declared in components, we should have something common between exceptions.

For example, given the following schema:

paths:
    '/foo/{id}':
        get:
            responses:
                404:
                    $ref: '#/components/responses/404'
                403:
                    $ref: '#/components/responses/403'
    '/bar/{id}':
        get:
            responses:
                404:
                    $ref: '#/components/responses/404'
                403:
                    $ref: '#/components/responses/403'
components:
    responses:
        404:
            description: 'Not found'
        403:
            description: 'Forbidden'

We currently have 2 exceptions: class GetFooNotFoundException extends \RuntimeException implements ClientException class GetFooForbiddenException extends \RuntimeException implements ClientException class GetBarNotFoundException extends \RuntimeException implements ClientException class GetBarForbiddenException extends \RuntimeException implements ClientException

I would like to have something like this: abstract class NotFoundException extends \RuntimeException implements **ClientException abstract class ForbiddenException extends \RuntimeException implements **ClientException class GetBarNotFoundException extends NotFoundException class GetBarForbiddenException extends ForbiddenException class GetFooNotFoundException extends NotFoundException class GetFooForbiddenException extends ForbddenException

Goal: Be able to easily deal with all exceptions of a given type.

WDYT?

odolbeau avatar Oct 21 '20 14:10 odolbeau

So basically: we should have abstract class for each exception type (400, 404, ...) and extend them in endpoint exceptions ?

Korbeil avatar Nov 03 '20 13:11 Korbeil

Exactly! :) We could even go further with something like :

  • class GetBarNotFoundException extends NotFoundException <= This specific exception is a 404
  • abstract class NotFoundException extends ClientException <= 404 exception exceptions are 4xx exceptions
  • abstract class ClientException extends \RuntimeException <= All 4xx exceptions are runtime exceptions

This is an example with abstract classes but interfaces are fine too if you prefer.

odolbeau avatar Nov 03 '20 13:11 odolbeau