Skip to content

Commit

Permalink
HandlerException added
Browse files Browse the repository at this point in the history
  • Loading branch information
arkamali committed May 13, 2021
1 parent 7689bc2 commit 702419b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to `hooshid/laravel-utils` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.5] - 2021-05-13
### Added
- HandlerException class.

## [0.3.1] - 2021-05-07
### Added
- php info page.
Expand Down
101 changes: 101 additions & 0 deletions src/HandlerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Hooshid\Utils;

class HandlerException
{
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Throwable
*/
public static function handle($request, $e)
{

// converts errors to JSON when required and when not a validation error
if ($request->expectsJson() && method_exists($e, 'getStatusCode')) {
return response()->json([
'result' => [
'status' => 'error',
'code' => $e->getStatusCode(),
'description' => $e->getMessage(),
],
], $e->getStatusCode());
}

// catch only json requests
if ($request->expectsJson()) {

// 400 Bad Request
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 400,
'description' => 'Bad Request',
],
], 400);
}

// 401 Unauthorized
if ($e instanceof \Illuminate\Auth\AuthenticationException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 401,
'description' => 'Unauthorized',
],
], 401);
}

// 404 Not Found
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 404,
'description' => 'Not Found',
],
], 404);
}

// 422 Unprocessable Entity - Validation Form
if ($e instanceof \Illuminate\Validation\ValidationException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 422,
'description' => 'Unprocessable Entity',
],
'errors' => $e->errors(),
], 422);
}

// 422 Unprocessable Entity
if ($e instanceof \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 422,
'description' => 'Unprocessable Entity',
],
], 422);
}

// 429 Too Many Attempts
if ($e instanceof \Illuminate\Http\Exceptions\ThrottleRequestsException) {
return response()->json([
'result' => [
'status' => 'error',
'code' => 429,
'description' => 'Too Many Attempts.',
],
], 429);
}
}

}
}

0 comments on commit 702419b

Please sign in to comment.