Issues (92)

src/Kernel/Traits/HandleExceptions.php (2 issues)

1
<?php
2
3
namespace Nip\Http\Kernel\Traits;
4
5
use Exception;
6
use Nip\Http\Exceptions\Handler;
7
use Nip\Http\Response\Response;
8
use Nip\Request;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Throwable;
12
13
/**
14
 * Trait HandleExceptions
15
 * @package Nip\Http\Kernel\Traits
16
 */
17
trait HandleExceptions
18
{
19
    /**
20
     * Report the exception to the exception handler.
21
     *
22
     * @param Exception $e
23
     * @return void
24
     */
25
    protected function reportException(Throwable $e)
26
    {
27
        app('log')->error($e);
28
    }
29
30
    /**
31
     * @param Request|ServerRequestInterface $request
32
     * @param Exception $e
33
     * @return Response|ResponseInterface
34
     */
35
    protected function renderException($request, Throwable $e)
36
    {
37
        return (new Handler($this->getContainer()))->render($request, $e);
0 ignored issues
show
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        return (new Handler($this->/** @scrutinizer ignore-call */ getContainer()))->render($request, $e);
Loading history...
Bug Best Practice introduced by
The expression return new Nip\Http\Exce...)->render($request, $e) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type Nip\Http\Response\Respon...ssage\ResponseInterface.
Loading history...
38
    }
39
40
    /**
41
     * @param Exception $e
42
     * @param Request|ServerRequestInterface $request
43
     * @return Response
44
     */
45
    protected function handleException($request, Throwable $e)
46
    {
47
        $this->reportException($e);
48
49
        return $this->renderException($request, $e);
50
    }
51
}
52