aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
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
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')
-rw-r--r--application/container/ContainerBuilder.php5
-rw-r--r--application/front/ShaarliMiddleware.php26
-rw-r--r--application/front/controller/visitor/ErrorController.php45
3 files changed, 51 insertions, 25 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php
index 2e8c1ee3..4a1a6ea7 100644
--- a/application/container/ContainerBuilder.php
+++ b/application/container/ContainerBuilder.php
@@ -9,6 +9,7 @@ use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
10use Shaarli\Feed\FeedBuilder; 10use Shaarli\Feed\FeedBuilder;
11use Shaarli\Formatter\FormatterFactory; 11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\Front\Controller\Visitor\ErrorController;
12use Shaarli\History; 13use Shaarli\History;
13use Shaarli\Http\HttpAccess; 14use Shaarli\Http\HttpAccess;
14use Shaarli\Netscape\NetscapeBookmarkUtils; 15use Shaarli\Netscape\NetscapeBookmarkUtils;
@@ -148,6 +149,10 @@ class ContainerBuilder
148 ); 149 );
149 }; 150 };
150 151
152 $container['errorHandler'] = function (ShaarliContainer $container): ErrorController {
153 return new ErrorController($container);
154 };
155
151 return $container; 156 return $container;
152 } 157 }
153} 158}
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}