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

slimphp / Slim / 22023941967

14 Feb 2026 08:41PM UTC coverage: 96.939% (-0.4%) from 97.317%
22023941967

push

github

web-flow
Update code styles

950 of 980 relevant lines covered (96.94%)

33.68 hits per line

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

88.0
/Slim/Middleware/ExceptionLoggingMiddleware.php
1
<?php
2

3
/**
4
 * Slim Framework (https://slimframework.com)
5
 *
6
 * @license https://github.com/slimphp/Slim/blob/5.x/LICENSE.md (MIT License)
7
 */
8

9
declare(strict_types=1);
10

11
namespace Slim\Middleware;
12

13
use ErrorException;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Psr\Log\LoggerInterface;
19
use Psr\Log\LogLevel;
20
use Throwable;
21

22
final class ExceptionLoggingMiddleware implements MiddlewareInterface
23
{
24
    private LoggerInterface $logger;
25

26
    private bool $logErrorDetails = false;
27

28
    public function __construct(LoggerInterface $logger)
29
    {
30
        $this->logger = $logger;
4✔
31
    }
32

33
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35
        try {
36
            return $handler->handle($request);
4✔
37
        } catch (ErrorException $exception) {
3✔
38
            $errorLevels = [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR];
1✔
39
            $level = in_array($exception->getSeverity(), $errorLevels) ? LogLevel::ERROR : LogLevel::WARNING;
1✔
40

41
            $context = $this->getContext($exception, $request);
1✔
42
            $this->logger->log($level, $exception->getMessage(), $context);
1✔
43

44
            throw $exception;
1✔
45
        } catch (Throwable $exception) {
2✔
46
            $context = $this->getContext($exception, $request);
2✔
47
            $this->logger->error($exception->getMessage(), $context);
2✔
48

49
            throw $exception;
2✔
50
        }
51
    }
52

53
    public function withLogErrorDetails(bool $logErrorDetails): self
54
    {
55
        $clone = clone $this;
3✔
56
        $clone->logErrorDetails = $logErrorDetails;
3✔
57

58
        return $clone;
3✔
59
    }
60

61
    public function withLogger(LoggerInterface $logger): self
62
    {
63
        $clone = clone $this;
×
64
        $clone->logger = $logger;
×
65

66
        return $clone;
×
67
    }
68

69
    /**
70
     * @param Throwable $exception
71
     * @param ServerRequestInterface $request
72
     * @return array{exception?: Throwable, request?: ServerRequestInterface}
73
     */
74
    private function getContext(Throwable $exception, ServerRequestInterface $request): array
75
    {
76
        $context = [];
3✔
77

78
        if ($this->logErrorDetails) {
3✔
79
            $context = [
3✔
80
                'exception' => $exception,
3✔
81
                'request' => $request,
3✔
82
            ];
3✔
83
        }
84

85
        return $context;
3✔
86
    }
87
}
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