]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controller/visitor/ErrorController.php
8da11172ffa52c6c8ae5967760b5a5043c05cca7
[github/shaarli/Shaarli.git] / application / front / controller / visitor / ErrorController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Visitor;
6
7 use Shaarli\Front\Exception\ShaarliFrontException;
8 use Slim\Http\Request;
9 use Slim\Http\Response;
10
11 /**
12 * Controller used to render the error page, with a provided exception.
13 * It is actually used as a Slim error handler.
14 */
15 class ErrorController extends ShaarliVisitorController
16 {
17 public function __invoke(Request $request, Response $response, \Throwable $throwable): Response
18 {
19 // Unknown error encountered
20 $this->container->pageBuilder->reset();
21
22 if ($throwable instanceof ShaarliFrontException) {
23 // Functional error
24 $this->assignView('message', nl2br($throwable->getMessage()));
25
26 $response = $response->withStatus($throwable->getCode());
27 } else {
28 // Internal error (any other Throwable)
29 if ($this->container->conf->get('dev.debug', false)) {
30 $this->assignView('message', $throwable->getMessage());
31 $this->assignView('stacktrace', exception2text($throwable));
32 } else {
33 $this->assignView('message', t('An unexpected error occurred.'));
34 }
35
36 $response = $response->withStatus(500);
37 }
38
39
40 return $response->write($this->render('error'));
41 }
42 }