Handler::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
namespace App\Exceptions;
4
5
use App\Constants\TranslationCode;
6
use App\Services\LogService;
7
use Exception;
8
use Illuminate\Auth\Access\AuthorizationException;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Validation\ValidationException;
14
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Exception\HttpException;
17
use Throwable;
18
19
/**
20
 * Class Handler
21
 *
22
 * @package App\Exceptions
23
 */
24
class Handler extends ExceptionHandler
25
{
26
    /**
27
     * A list of the exception types that should not be reported.
28
     *
29
     * @var array
30
     */
31
    protected $dontReport = [
32
        AuthorizationException::class,
33
        HttpException::class,
34
        ModelNotFoundException::class,
35
        ValidationException::class,
36
    ];
37
38
    /**
39
     * Report or log an exception.
40
     *
41
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
42
     *
43
     * @param  Throwable  $throwable
44
     *
45
     * @throws Exception
46
     */
47
    public function report(Throwable $throwable)
48
    {
49
        parent::report($throwable);
50
    }
51
52
    /**
53
     * Render an exception into an HTTP response.
54
     *
55
     * When in production we return same json structure even when error occurred.
56
     *
57
     * @param  Request  $request
58
     * @param  Throwable  $throwable
59
     *
60
     * @return JsonResponse|Response
61
     *
62
     * @throws Throwable
63
     */
64
    public function render($request, Throwable $throwable)
65
    {
66
        if (env('APP_DEBUG')) {
67
            return parent::render($request, $throwable);
68
        }
69
70
        Log::error(LogService::getThrowableTraceAsString($throwable, $request));
71
72
        $response = [
73
            'isError'       => true,
74
            'userFault'     => false,
75
            'errorMessages' => ['application' => TranslationCode::ERROR_APPLICATION]
76
        ];
77
78
        return response()->json($response, Response::HTTP_OK);
79
    }
80
}
81