|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +namespace Ibexa\Contracts\Rest\Output\Exceptions; |
| 8 | + |
| 9 | +use Ibexa\Contracts\Rest\Output\Generator; |
| 10 | +use Ibexa\Contracts\Rest\Output\ValueObjectVisitor; |
| 11 | +use Ibexa\Contracts\Rest\Output\Visitor; |
| 12 | +use Ibexa\Core\Base\Translatable; |
| 13 | +use JMS\TranslationBundle\Annotation\Desc; |
| 14 | +use JMS\TranslationBundle\Annotation\Ignore; |
| 15 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 16 | + |
| 17 | +abstract class AbstractExceptionVisitor extends ValueObjectVisitor |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Mapping of HTTP status codes to their respective error messages. |
| 21 | + * |
| 22 | + * @var array<int, string> |
| 23 | + */ |
| 24 | + protected static $httpStatusCodes = [ |
| 25 | + 400 => 'Bad Request', |
| 26 | + 401 => 'Unauthorized', |
| 27 | + 402 => 'Payment Required', |
| 28 | + 403 => 'Forbidden', |
| 29 | + 404 => 'Not Found', |
| 30 | + 405 => 'Method Not Allowed', |
| 31 | + 406 => 'Not Acceptable', |
| 32 | + 407 => 'Proxy Authentication Required', |
| 33 | + 408 => 'Request Time-out', |
| 34 | + 409 => 'Conflict', |
| 35 | + 410 => 'Gone', |
| 36 | + 411 => 'Length Required', |
| 37 | + 412 => 'Precondition Failed', |
| 38 | + 413 => 'Request Entity Too Large', |
| 39 | + 414 => 'Request-URI Too Long', |
| 40 | + 415 => 'Unsupported Media Type', |
| 41 | + 416 => 'Requested range not satisfiable', |
| 42 | + 417 => 'Expectation Failed', |
| 43 | + 418 => "I'm a teapot", |
| 44 | + 421 => 'There are too many connections from your internet address', |
| 45 | + 422 => 'Unprocessable Entity', |
| 46 | + 423 => 'Locked', |
| 47 | + 424 => 'Failed Dependency', |
| 48 | + 425 => 'Unordered Collection', |
| 49 | + 426 => 'Upgrade Required', |
| 50 | + 500 => 'Internal Server Error', |
| 51 | + 501 => 'Not Implemented', |
| 52 | + 502 => 'Bad Gateway', |
| 53 | + 503 => 'Service Unavailable', |
| 54 | + 504 => 'Gateway Time-out', |
| 55 | + 505 => 'HTTP Version not supported', |
| 56 | + 506 => 'Variant Also Negotiates', |
| 57 | + 507 => 'Insufficient Storage', |
| 58 | + 509 => 'Bandwidth Limit Exceeded', |
| 59 | + 510 => 'Not Extended', |
| 60 | + ]; |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns HTTP status code. |
| 64 | + * |
| 65 | + * @return int |
| 66 | + */ |
| 67 | + protected function getStatus() |
| 68 | + { |
| 69 | + return 500; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param \Exception $data |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function visit(Visitor $visitor, Generator $generator, $data) |
| 78 | + { |
| 79 | + $generator->startObjectElement('ErrorMessage'); |
| 80 | + |
| 81 | + $visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage')); |
| 82 | + |
| 83 | + $statusCode = $this->generateErrorCode($generator, $visitor, $data); |
| 84 | + |
| 85 | + $errorMessage = $this->getErrorMessage($data, $statusCode); |
| 86 | + $generator->valueElement('errorMessage', $errorMessage); |
| 87 | + |
| 88 | + $errorDescription = $this->getErrorDescription($data, $statusCode); |
| 89 | + $generator->valueElement('errorDescription', $errorDescription); |
| 90 | + |
| 91 | + if ($this->canDisplayExceptionTrace()) { |
| 92 | + $generator->valueElement('trace', $data->getTraceAsString()); |
| 93 | + $generator->valueElement('file', $data->getFile()); |
| 94 | + $generator->valueElement('line', $data->getLine()); |
| 95 | + } |
| 96 | + |
| 97 | + $previous = $data->getPrevious(); |
| 98 | + if ($previous !== null && $this->canDisplayPreviousException()) { |
| 99 | + $generator->startObjectElement('Previous', 'ErrorMessage'); |
| 100 | + $visitor->visitValueObject($previous); |
| 101 | + $generator->endObjectElement('Previous'); |
| 102 | + } |
| 103 | + |
| 104 | + $generator->endObjectElement('ErrorMessage'); |
| 105 | + } |
| 106 | + |
| 107 | + protected function generateErrorCode(Generator $generator, Visitor $visitor, \Exception $e): int |
| 108 | + { |
| 109 | + $statusCode = $this->getStatus(); |
| 110 | + $visitor->setStatus($statusCode); |
| 111 | + |
| 112 | + $generator->valueElement('errorCode', $statusCode); |
| 113 | + |
| 114 | + return $statusCode; |
| 115 | + } |
| 116 | + |
| 117 | + protected function getErrorMessage(\Exception $data, int $statusCode): string |
| 118 | + { |
| 119 | + return static::$httpStatusCodes[$statusCode] ?? static::$httpStatusCodes[500]; |
| 120 | + } |
| 121 | + |
| 122 | + protected function getErrorDescription(\Exception $data, int $statusCode): string |
| 123 | + { |
| 124 | + $translator = $this->getTranslator(); |
| 125 | + if ($statusCode < 500 || $this->canDisplayExceptionMessage()) { |
| 126 | + $errorDescription = $data instanceof Translatable && $translator |
| 127 | + ? /** @Ignore */ |
| 128 | + $translator->trans($data->getMessageTemplate(), $data->getParameters(), 'ibexa_repository_exceptions') |
| 129 | + : $data->getMessage(); |
| 130 | + } else { |
| 131 | + // Do not leak any file paths and sensitive data on production environments |
| 132 | + $errorDescription = $translator |
| 133 | + ? /** @Desc("An error has occurred. Please try again later or contact your Administrator.") */ |
| 134 | + $translator->trans('non_verbose_error', [], 'ibexa_repository_exceptions') |
| 135 | + : 'An error has occurred. Please try again later or contact your Administrator.'; |
| 136 | + } |
| 137 | + |
| 138 | + return $errorDescription; |
| 139 | + } |
| 140 | + |
| 141 | + protected function getTranslator(): ?TranslatorInterface |
| 142 | + { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + protected function canDisplayExceptionTrace(): bool |
| 147 | + { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + protected function canDisplayPreviousException(): bool |
| 152 | + { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + protected function canDisplayExceptionMessage(): bool |
| 157 | + { |
| 158 | + return false; |
| 159 | + } |
| 160 | +} |
0 commit comments