aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-21 10:50:44 +0200
committerArthurHoaro <arthur@hoa.ro>2020-08-21 10:50:44 +0200
commit0c6fdbe12bbbb336348666b14b82096f24d5858b (patch)
tree8fad2829c55f94022e359fa8914e11f80a2afc2a /application/front
parentbedbb845eec20363b928b424143787dbe988eefe (diff)
downloadShaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.gz
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.zst
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.zip
Move error handling to dedicated controller instead of middleware
Diffstat (limited to 'application/front')
-rw-r--r--application/front/ShaarliMiddleware.php26
-rw-r--r--application/front/controller/visitor/ErrorController.php45
2 files changed, 46 insertions, 25 deletions
diff --git a/application/front/ShaarliMiddleware.php b/application/front/ShaarliMiddleware.php
index a2a3837b..c015c0c6 100644
--- a/application/front/ShaarliMiddleware.php
+++ b/application/front/ShaarliMiddleware.php
@@ -3,7 +3,6 @@
3namespace Shaarli\Front; 3namespace Shaarli\Front;
4 4
5use Shaarli\Container\ShaarliContainer; 5use Shaarli\Container\ShaarliContainer;
6use Shaarli\Front\Exception\ShaarliFrontException;
7use Shaarli\Front\Exception\UnauthorizedException; 6use Shaarli\Front\Exception\UnauthorizedException;
8use Slim\Http\Request; 7use Slim\Http\Request;
9use Slim\Http\Response; 8use Slim\Http\Response;
@@ -53,35 +52,12 @@ class ShaarliMiddleware
53 $this->checkOpenShaarli($request, $response, $next); 52 $this->checkOpenShaarli($request, $response, $next);
54 53
55 return $next($request, $response); 54 return $next($request, $response);
56 } catch (ShaarliFrontException $e) {
57 // Possible functional error
58 $this->container->pageBuilder->reset();
59 $this->container->pageBuilder->assign('message', nl2br($e->getMessage()));
60
61 $response = $response->withStatus($e->getCode());
62
63 return $response->write($this->container->pageBuilder->render('error', $this->container->basePath));
64 } catch (UnauthorizedException $e) { 55 } catch (UnauthorizedException $e) {
65 $returnUrl = urlencode($this->container->environment['REQUEST_URI']); 56 $returnUrl = urlencode($this->container->environment['REQUEST_URI']);
66 57
67 return $response->withRedirect($this->container->basePath . '/login?returnurl=' . $returnUrl); 58 return $response->withRedirect($this->container->basePath . '/login?returnurl=' . $returnUrl);
68 } catch (\Throwable $e) {
69 // Unknown error encountered
70 $this->container->pageBuilder->reset();
71 if ($this->container->conf->get('dev.debug', false)) {
72 $this->container->pageBuilder->assign('message', $e->getMessage());
73 $this->container->pageBuilder->assign(
74 'stacktrace',
75 nl2br(get_class($e) .': '. PHP_EOL . $e->getTraceAsString())
76 );
77 } else {
78 $this->container->pageBuilder->assign('message', t('An unexpected error occurred.'));
79 }
80
81 $response = $response->withStatus(500);
82
83 return $response->write($this->container->pageBuilder->render('error', $this->container->basePath));
84 } 59 }
60 // Other exceptions are handled by ErrorController
85 } 61 }
86 62
87 /** 63 /**
diff --git a/application/front/controller/visitor/ErrorController.php b/application/front/controller/visitor/ErrorController.php
new file mode 100644
index 00000000..10aa84c8
--- /dev/null
+++ b/application/front/controller/visitor/ErrorController.php
@@ -0,0 +1,45 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use Shaarli\Front\Exception\ShaarliFrontException;
8use Slim\Http\Request;
9use 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 */
15class 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(
32 'stacktrace',
33 nl2br(get_class($throwable) .': '. PHP_EOL . $throwable->getTraceAsString())
34 );
35 } else {
36 $this->assignView('message', t('An unexpected error occurred.'));
37 }
38
39 $response = $response->withStatus(500);
40 }
41
42
43 return $response->write($this->render('error'));
44 }
45}