Improve exception handling
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?
So basically: we should have abstract class for each exception type (400, 404, ...) and extend them in endpoint exceptions ?
Exactly! :) We could even go further with something like :
class GetBarNotFoundException extends NotFoundException<= This specific exception is a 404abstract class NotFoundException extends ClientException<= 404 exception exceptions are 4xx exceptionsabstract 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.