1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Exceptions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\AuthenticationException; |
6
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
7
|
|
|
use Illuminate\Validation\ValidationException; |
8
|
|
|
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ApiHandler. |
14
|
|
|
*/ |
15
|
|
|
class ApiHandler extends ExceptionHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param \Illuminate\Http\Request $request |
19
|
|
|
* @param \Exception|\Throwable $e |
20
|
|
|
* |
21
|
|
|
* @throws \ReflectionException |
22
|
|
|
* |
23
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
24
|
|
|
*/ |
25
|
|
|
public function render($request, $e) |
26
|
|
|
{ |
27
|
|
|
if (true === app()->isDownForMaintenance()) { |
|
|
|
|
28
|
|
|
return response()->json([ |
29
|
|
|
'error' => [ |
30
|
|
|
'code' => 503, |
31
|
|
|
'message' => 'Service is down for scheduled maintenance. Be right back!', |
32
|
|
|
], |
33
|
|
|
], Response::HTTP_SERVICE_UNAVAILABLE); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($e instanceof NotFoundHttpException) { |
37
|
|
|
return response()->json( |
38
|
|
|
[ |
39
|
|
|
'code' => 34, |
40
|
|
|
'message' => 'Not Found', |
41
|
|
|
], |
42
|
|
|
Response::HTTP_NOT_FOUND |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($e instanceof ValidationException) { |
47
|
|
|
return response()->json( |
48
|
|
|
[ |
49
|
|
|
'code' => 215, |
50
|
|
|
'message' => 'Validation failed', |
51
|
|
|
'errors' => $e->validator->errors()->toArray(), |
52
|
|
|
], |
53
|
|
|
Response::HTTP_UNPROCESSABLE_ENTITY |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($e instanceof AuthenticationException) { |
58
|
|
|
return response()->json([ |
59
|
|
|
'error' => [ |
60
|
|
|
'code' => 64, |
61
|
|
|
'message' => 'Forbidden', |
62
|
|
|
], |
63
|
|
|
], Response::HTTP_FORBIDDEN); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($e instanceof ApiInternalCallValidationException) { |
67
|
|
|
$response = response([ |
68
|
|
|
'error' => [ |
69
|
|
|
'code' => 215, |
70
|
|
|
'message' => 'Validation failed', |
71
|
|
|
], |
72
|
|
|
], 400); |
73
|
|
|
|
74
|
|
|
return $response->withException($e); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return (new NappApiHandler($e))->render(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|