aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controller/visitor/ErrorController.php
blob: 8da11172ffa52c6c8ae5967760b5a5043c05cca7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Visitor;

use Shaarli\Front\Exception\ShaarliFrontException;
use Slim\Http\Request;
use Slim\Http\Response;

/**
 * Controller used to render the error page, with a provided exception.
 * It is actually used as a Slim error handler.
 */
class ErrorController extends ShaarliVisitorController
{
    public function __invoke(Request $request, Response $response, \Throwable $throwable): Response
    {
        // Unknown error encountered
        $this->container->pageBuilder->reset();

        if ($throwable instanceof ShaarliFrontException) {
            // Functional error
            $this->assignView('message', nl2br($throwable->getMessage()));

            $response = $response->withStatus($throwable->getCode());
        } else {
            // Internal error (any other Throwable)
            if ($this->container->conf->get('dev.debug', false)) {
                $this->assignView('message', $throwable->getMessage());
                $this->assignView('stacktrace', exception2text($throwable));
            } else {
                $this->assignView('message', t('An unexpected error occurred.'));
            }

            $response = $response->withStatus(500);
        }


        return $response->write($this->render('error'));
    }
}