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

slimphp / Slim-Skeleton / 5634814243

pending completion
5634814243

push

github

web-flow
Merge pull request #329 from l0gicgate/license

Add LICENSE.md

94 of 158 relevant lines covered (59.49%)

2.04 hits per line

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

63.33
/src/Application/Handlers/HttpErrorHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Application\Handlers;
6

7
use App\Application\Actions\ActionError;
8
use App\Application\Actions\ActionPayload;
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Slim\Exception\HttpBadRequestException;
11
use Slim\Exception\HttpException;
12
use Slim\Exception\HttpForbiddenException;
13
use Slim\Exception\HttpMethodNotAllowedException;
14
use Slim\Exception\HttpNotFoundException;
15
use Slim\Exception\HttpNotImplementedException;
16
use Slim\Exception\HttpUnauthorizedException;
17
use Slim\Handlers\ErrorHandler as SlimErrorHandler;
18
use Throwable;
19

20
class HttpErrorHandler extends SlimErrorHandler
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    protected function respond(): Response
26
    {
27
        $exception = $this->exception;
1✔
28
        $statusCode = 500;
1✔
29
        $error = new ActionError(
1✔
30
            ActionError::SERVER_ERROR,
1✔
31
            'An internal error has occurred while processing your request.'
1✔
32
        );
1✔
33

34
        if ($exception instanceof HttpException) {
1✔
35
            $statusCode = $exception->getCode();
1✔
36
            $error->setDescription($exception->getMessage());
1✔
37

38
            if ($exception instanceof HttpNotFoundException) {
1✔
39
                $error->setType(ActionError::RESOURCE_NOT_FOUND);
1✔
40
            } elseif ($exception instanceof HttpMethodNotAllowedException) {
×
41
                $error->setType(ActionError::NOT_ALLOWED);
×
42
            } elseif ($exception instanceof HttpUnauthorizedException) {
×
43
                $error->setType(ActionError::UNAUTHENTICATED);
×
44
            } elseif ($exception instanceof HttpForbiddenException) {
×
45
                $error->setType(ActionError::INSUFFICIENT_PRIVILEGES);
×
46
            } elseif ($exception instanceof HttpBadRequestException) {
×
47
                $error->setType(ActionError::BAD_REQUEST);
×
48
            } elseif ($exception instanceof HttpNotImplementedException) {
×
49
                $error->setType(ActionError::NOT_IMPLEMENTED);
×
50
            }
51
        }
52

53
        if (
54
            !($exception instanceof HttpException)
1✔
55
            && $exception instanceof Throwable
1✔
56
            && $this->displayErrorDetails
1✔
57
        ) {
58
            $error->setDescription($exception->getMessage());
×
59
        }
60

61
        $payload = new ActionPayload($statusCode, null, $error);
1✔
62
        $encodedPayload = json_encode($payload, JSON_PRETTY_PRINT);
1✔
63

64
        $response = $this->responseFactory->createResponse($statusCode);
1✔
65
        $response->getBody()->write($encodedPayload);
1✔
66

67
        return $response->withHeader('Content-Type', 'application/json');
1✔
68
    }
69
}
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