Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

HandlesApiErrors::transformAuthException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Exceptions;
4
5
use Exception;
6
use Flugg\Responder\Exceptions\Http\ApiException;
7
use Flugg\Responder\Exceptions\Http\RelationNotFoundException;
8
use Flugg\Responder\Exceptions\Http\ResourceNotFoundException;
9
use Flugg\Responder\Exceptions\Http\UnauthenticatedException;
10
use Flugg\Responder\Exceptions\Http\UnauthorizedException;
11
use Flugg\Responder\Exceptions\Http\ValidationFailedException;
12
use Flugg\Responder\Http\Responses\ErrorResponseBuilder;
13
use Illuminate\Auth\Access\AuthorizationException;
14
use Illuminate\Auth\AuthenticationException;
15
use Illuminate\Database\Eloquent\ModelNotFoundException;
16
use Illuminate\Database\Eloquent\RelationNotFoundException as BaseRelationNotFoundException;
17
use Illuminate\Http\JsonResponse;
18
use Illuminate\Validation\ValidationException;
19
20
/**
21
 * A trait to be used by an exception handler to handle automatic handling of error responses.
22
 *
23
 * @package flugger/laravel-responder
24
 * @author  Alexander Tømmerås <[email protected]>
25
 * @license The MIT License
26
 */
27
trait HandlesApiErrors
28
{
29
    /**
30
     * Convert a Laravel exception to an API exception.
31
     *
32
     * @param  \Exception $exception
33
     * @return void
34
     */
35
    protected function transformException(Exception $exception)
36
    {
37
        $this->transformAuthException($exception);
38
        $this->transformEloquentException($exception);
39
        $this->transformValidationException($exception);
40
    }
41
42
    /**
43
     * Convert a Laravel auth exception to an API exception.
44
     *
45
     * @param  \Exception $exception
46
     * @return void
47
     * @throws UnauthenticatedException
48
     * @throws UnauthorizedException
49
     */
50
    protected function transformAuthException(Exception $exception)
51
    {
52
        if ($exception instanceof AuthenticationException) {
53
            throw new UnauthenticatedException;
54
        }
55
56
        if ($exception instanceof AuthorizationException) {
57
            throw new UnauthorizedException;
58
        }
59
    }
60
61
    /**
62
     * Convert an Eloquent exception to an API exception.
63
     *
64
     * @param  Exception $exception
65
     * @return void
66
     * @throws ResourceNotFoundException
67
     * @throws RelationNotFoundException
68
     */
69
    protected function transformEloquentException(Exception $exception)
70
    {
71
        if ($exception instanceof ModelNotFoundException) {
72
            throw new ResourceNotFoundException;
73
        }
74
75
        if ($exception instanceof BaseRelationNotFoundException) {
76
            throw new RelationNotFoundException;
77
        }
78
    }
79
80
    /**
81
     * Convert a Laravel validation exception to an API exception.
82
     *
83
     * @param  Exception $exception
84
     * @return void
85
     * @throws ValidationFailedException
86
     */
87
    protected function transformValidationException(Exception $exception)
88
    {
89
        if ($exception instanceof ValidationException) {
90
            throw new ValidationFailedException($exception->validator);
91
        }
92
    }
93
94
    /**
95
     * Convert API exceptions to error responses.
96
     *
97
     * @param  \Flugg\Responder\Exceptions\Http\ApiException $exception
98
     * @return \Illuminate\Http\JsonResponse
99
     */
100
    protected function renderApiError(ApiException $exception): JsonResponse
101
    {
102
        return $this->container->make(ErrorResponseBuilder::class)
103
            ->error($exception->errorCode(), $exception->message())
104
            ->data($exception->data())
105
            ->respond($exception->statusCode());
106
    }
107
}