• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

piko-framework / piko / 6798093764

08 Nov 2023 12:18PM UTC coverage: 99.738% (-0.3%) from 100.0%
6798093764

push

github

ilhooq
Fix invalid Exception code

4 of 5 new or added lines in 1 file covered. (80.0%)

380 of 381 relevant lines covered (99.74%)

10.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/src/ModularApplication/ErrorHandler.php
1
<?php
2

3
/**
4
 * This file is part of Piko - Web micro framework
5
 *
6
 * @copyright 2019-2022 Sylvain PHILIP
7
 * @license LGPL-3.0; see LICENSE.txt
8
 * @link https://github.com/piko-framework/piko
9
 */
10

11
declare(strict_types=1);
12

13
namespace Piko\ModularApplication;
14

15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use RuntimeException;
19
use Throwable;
20
use Piko\ModularApplication;
21

22
/**
23
 * Error handler class.
24
 * Forwards exception to application's error route (if defined).
25
 *
26
 * @author Sylvain PHILIP <contact@sphilip.com>
27
 */
28
final class ErrorHandler implements RequestHandlerInterface
29
{
30
    /**
31
     * @var ModularApplication
32
     */
33
    private $application;
34

35
    public function __construct(ModularApplication $app)
36
    {
37
        $this->application = $app;
33✔
38
    }
39

40
    /**
41
     * {@inheritDoc}
42
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
43
     */
44
    public function handle(ServerRequestInterface $request): ResponseInterface
45
    {
46
        $exception = $request->getAttribute('exception');
5✔
47

48
        if (!$exception instanceof Throwable) {
5✔
49
            throw new RuntimeException('Exception must be instance of Throwable');
2✔
50
        }
51

52
        $errorRoute = $this->application->errorRoute;
5✔
53

54
        if ($errorRoute === '') {
5✔
55
            throw $exception;
4✔
56
        }
57

58
        list($moduleId, $controllerId, $actionId) = ModularApplication::parseRoute($errorRoute);
1✔
59

60
        $request = $request->withAttribute('route_params', ['exception' => $exception])
1✔
61
                           ->withAttribute('module', $moduleId)
1✔
62
                           ->withAttribute('controller', $controllerId)
1✔
63
                           ->withAttribute('action', $actionId);
1✔
64

65
        $module = $this->application->getModule((string) $moduleId);
1✔
66

67
        $response = $module->handle($request);
1✔
68

69
        $code = $exception->getCode();
1✔
70

71
        if (is_string($code) && !is_numeric($code)) {
1✔
NEW
72
            $code = 500; // Internal server error;
×
73
        } elseif (is_int($code) && ($code < 100 || $code > 599)) {
1✔
74
            $code = 500;
1✔
75
        }
76

77
        return $response->withStatus($code, $exception->getMessage());
1✔
78
    }
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc