What exceptions custom exceptions should I throw in general for an API?
I've built a custom exception handler for a Saas API that will be consumed by my Vue SPA:
**Handler.php**
public function render($request, Throwable $exception)
{
$response = $this->handleException($request, $exception);
return $response;
}
public function handleException($request, Throwable $exception)
{
if ($exception instanceof \Stripe\Exception\ApiErrorException) {
return response()->json([
'message' => $exception->getMessage(),
], 500);
}
if ($exception instanceof MethodNotAllowedHttpException) {
return response()->json([
'message' => 'The specified method for the request is invalid']
, 405);
}
if ($exception instanceof NotFoundHttpException) {
return response()->json([
'message' => 'The specified URL cannot be found']
, 404);
}
if ($exception instanceof HttpException) {
return response()->json([
'message' => $exception->getMessage()]
, $exception->getStatusCode());
}
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'message' => 'Entry for '.str_replace('App\\', '', $exception->getModel()).' not found']
, 404);
}
if (config('app.debug')) {
return parent::render($request, $exception);
}
return response()->json([
'message' => 'Unexpected Exception. Try later']
, 500);
}
What I'm wondering is what other generalized exceptions should I worry about throwing? Is there any that I am missing? Where would I import these other exception handlers from?
I haven't seen much on this. I've only really seen tutorials on how to handle exceptions and not ones you should customize for an API by default.
Thanks for the help. I'm new to API development.
https://redd.it/he17hk@r_php