|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ApiServiceProvider.php. |
|
4
|
|
|
* |
|
5
|
|
|
* Description |
|
6
|
|
|
* |
|
7
|
|
|
* @author Milkmeowo <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Milkmeowo\Framework\Dingo\Providers; |
|
11
|
|
|
|
|
12
|
|
|
use Illuminate\Support\ServiceProvider; |
|
13
|
|
|
use Dingo\Api\Exception\ResourceException; |
|
14
|
|
|
use Illuminate\Auth\AuthenticationException; |
|
15
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
16
|
|
|
use Prettus\Validator\Exceptions\ValidatorException; |
|
17
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
|
18
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
19
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
23
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
|
24
|
|
|
|
|
25
|
|
|
class ExceptionHandlerServiceProvider extends ServiceProvider |
|
26
|
|
|
{ |
|
27
|
|
|
public function boot() |
|
28
|
|
|
{ |
|
29
|
|
|
$handler = app('Dingo\Api\Exception\Handler'); |
|
30
|
|
|
|
|
31
|
|
|
$handler->register(function (AuthorizationException $exception) { |
|
32
|
|
|
throw new AccessDeniedHttpException($exception->getMessage()); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
$handler->register(function (OAuthServerException $e) { |
|
36
|
|
|
$message = env('API_DEBUG') ? $e->getMessage() : null; |
|
37
|
|
|
throw new HttpException($e->getHttpStatusCode(), $message, $e, $e->getHttpHeaders()); |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
$handler->register(function (HttpResponseException $e) { |
|
41
|
|
|
$message = env('API_DEBUG') ? $e->getMessage() : null; |
|
42
|
|
|
throw new HttpException($e->getResponse()->getStatusCode(), $message, $e); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$handler->register(function (AuthenticationException $e) { |
|
46
|
|
|
throw new UnauthorizedHttpException(null, $e->getMessage(), $e); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
$handler->register(function (ValidatorException $e) { |
|
50
|
|
|
$messageBag = $e->getMessageBag(); |
|
51
|
|
|
throw new ResourceException($messageBag->first(), $messageBag->all()); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
$handler->register(function (ModelNotFoundException $e) { |
|
|
|
|
|
|
55
|
|
|
throw new NotFoundHttpException('No resources found.'); |
|
56
|
|
|
}); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.