aboutsummaryrefslogtreecommitdiffhomepage
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
parentbedbb845eec20363b928b424143787dbe988eefe (diff)
downloadShaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.gz
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.tar.zst
Shaarli-0c6fdbe12bbbb336348666b14b82096f24d5858b.zip
Move error handling to dedicated controller instead of middleware
-rw-r--r--application/container/ContainerBuilder.php5
-rw-r--r--application/front/ShaarliMiddleware.php26
-rw-r--r--application/front/controller/visitor/ErrorController.php45
-rw-r--r--tests/front/ShaarliMiddlewareTest.php29
-rw-r--r--tests/front/controller/visitor/ErrorControllerTest.php70
5 files changed, 135 insertions, 40 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}
diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php
index d435f506..05aa34a9 100644
--- a/tests/front/ShaarliMiddlewareTest.php
+++ b/tests/front/ShaarliMiddlewareTest.php
@@ -74,7 +74,8 @@ class ShaarliMiddlewareTest extends TestCase
74 } 74 }
75 75
76 /** 76 /**
77 * Test middleware execution with controller throwing a known front exception 77 * Test middleware execution with controller throwing a known front exception.
78 * The exception should be thrown to be later handled by the error handler.
78 */ 79 */
79 public function testMiddlewareExecutionWithFrontException(): void 80 public function testMiddlewareExecutionWithFrontException(): void
80 { 81 {
@@ -99,16 +100,14 @@ class ShaarliMiddlewareTest extends TestCase
99 }); 100 });
100 $this->container->pageBuilder = $pageBuilder; 101 $this->container->pageBuilder = $pageBuilder;
101 102
102 /** @var Response $result */ 103 $this->expectException(LoginBannedException::class);
103 $result = $this->middleware->__invoke($request, $response, $controller);
104 104
105 static::assertInstanceOf(Response::class, $result); 105 $this->middleware->__invoke($request, $response, $controller);
106 static::assertSame(401, $result->getStatusCode());
107 static::assertContains('error', (string) $result->getBody());
108 } 106 }
109 107
110 /** 108 /**
111 * Test middleware execution with controller throwing a not authorized exception 109 * Test middleware execution with controller throwing a not authorized exception
110 * The middle should send a redirection response to the login page.
112 */ 111 */
113 public function testMiddlewareExecutionWithUnauthorizedException(): void 112 public function testMiddlewareExecutionWithUnauthorizedException(): void
114 { 113 {
@@ -136,9 +135,10 @@ class ShaarliMiddlewareTest extends TestCase
136 } 135 }
137 136
138 /** 137 /**
139 * Test middleware execution with controller throwing a not authorized exception 138 * Test middleware execution with controller throwing a not authorized exception.
139 * The exception should be thrown to be later handled by the error handler.
140 */ 140 */
141 public function testMiddlewareExecutionWithServerExceptionWith(): void 141 public function testMiddlewareExecutionWithServerException(): void
142 { 142 {
143 $request = $this->createMock(Request::class); 143 $request = $this->createMock(Request::class);
144 $request->method('getUri')->willReturnCallback(function (): Uri { 144 $request->method('getUri')->willReturnCallback(function (): Uri {
@@ -148,9 +148,11 @@ class ShaarliMiddlewareTest extends TestCase
148 return $uri; 148 return $uri;
149 }); 149 });
150 150
151 $dummyException = new class() extends \Exception {};
152
151 $response = new Response(); 153 $response = new Response();
152 $controller = function (): void { 154 $controller = function () use ($dummyException): void {
153 throw new \Exception(); 155 throw $dummyException;
154 }; 156 };
155 157
156 $parameters = []; 158 $parameters = [];
@@ -165,12 +167,9 @@ class ShaarliMiddlewareTest extends TestCase
165 }) 167 })
166 ; 168 ;
167 169
168 /** @var Response $result */ 170 $this->expectException(get_class($dummyException));
169 $result = $this->middleware->__invoke($request, $response, $controller);
170 171
171 static::assertSame(500, $result->getStatusCode()); 172 $this->middleware->__invoke($request, $response, $controller);
172 static::assertContains('error', (string) $result->getBody());
173 static::assertSame('An unexpected error occurred.', $parameters['message']);
174 } 173 }
175 174
176 public function testMiddlewareExecutionWithUpdates(): void 175 public function testMiddlewareExecutionWithUpdates(): void
diff --git a/tests/front/controller/visitor/ErrorControllerTest.php b/tests/front/controller/visitor/ErrorControllerTest.php
new file mode 100644
index 00000000..e497bfef
--- /dev/null
+++ b/tests/front/controller/visitor/ErrorControllerTest.php
@@ -0,0 +1,70 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Front\Exception\ShaarliFrontException;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class ErrorControllerTest extends TestCase
13{
14 use FrontControllerMockHelper;
15
16 /** @var ErrorController */
17 protected $controller;
18
19 public function setUp(): void
20 {
21 $this->createContainer();
22
23 $this->controller = new ErrorController($this->container);
24 }
25
26 /**
27 * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code
28 */
29 public function testDisplayFrontExceptionError(): void
30 {
31 $request = $this->createMock(Request::class);
32 $response = new Response();
33
34 $message = 'error message';
35 $errorCode = 418;
36
37 // Save RainTPL assigned variables
38 $assignedVariables = [];
39 $this->assignTemplateVars($assignedVariables);
40
41 $result = ($this->controller)(
42 $request,
43 $response,
44 new class($message, $errorCode) extends ShaarliFrontException {}
45 );
46
47 static::assertSame($errorCode, $result->getStatusCode());
48 static::assertSame($message, $assignedVariables['message']);
49 static::assertArrayNotHasKey('stacktrace', $assignedVariables);
50 }
51
52 /**
53 * Test displaying error with any exception (no debug): only display an error occurred with HTTP 500.
54 */
55 public function testDisplayAnyExceptionErrorNoDebug(): void
56 {
57 $request = $this->createMock(Request::class);
58 $response = new Response();
59
60 // Save RainTPL assigned variables
61 $assignedVariables = [];
62 $this->assignTemplateVars($assignedVariables);
63
64 $result = ($this->controller)($request, $response, new \Exception('abc'));
65
66 static::assertSame(500, $result->getStatusCode());
67 static::assertSame('An unexpected error occurred.', $assignedVariables['message']);
68 static::assertArrayNotHasKey('stacktrace', $assignedVariables);
69 }
70}