|
| 1 | +<?php |
| 2 | +namespace Yoanm\JsonRpcServer\App\Serialization; |
| 3 | + |
| 4 | +use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface; |
| 5 | + |
| 6 | +/** |
| 7 | + * JsonRpcResponseErrorNormalizer prepares response data for the "unexpected" errors occur during request processing. |
| 8 | + * |
| 9 | + * It handles "internal server error" appearance in the response. |
| 10 | + * Instance of this class should be attached to {@see \Yoanm\JsonRpcServer\App\Serialization\JsonRpcResponseNormalizer} only in "debug" mode, |
| 11 | + * since it will expose vital internal information to the API consumer. |
| 12 | + * |
| 13 | + * @see \Yoanm\JsonRpcServer\App\Serialization\JsonRpcResponseNormalizer::normalizeError() |
| 14 | + */ |
| 15 | +class JsonRpcResponseErrorNormalizer |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var int maximum count of trace lines to be displayed. |
| 19 | + */ |
| 20 | + private $maxTraceSize; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var bool whether to show trace arguments. |
| 24 | + */ |
| 25 | + private $showTraceArguments; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var bool whether to simplify trace arguments representation. |
| 29 | + */ |
| 30 | + private $simplifyTraceArguments; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param int $maxTraceSize maximum count of trace lines to be displayed. |
| 34 | + * @param bool $showTraceArguments whether to show trace arguments. |
| 35 | + * @param bool $simplifyTraceArguments whether to simplify trace arguments representation. |
| 36 | + */ |
| 37 | + public function __construct(int $maxTraceSize = 10, bool $showTraceArguments = true, bool $simplifyTraceArguments = true) |
| 38 | + { |
| 39 | + $this->maxTraceSize = $maxTraceSize; |
| 40 | + $this->showTraceArguments = $showTraceArguments; |
| 41 | + $this->simplifyTraceArguments = $simplifyTraceArguments; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param JsonRpcExceptionInterface $error |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function normalize(JsonRpcExceptionInterface $error) : array |
| 49 | + { |
| 50 | + // `JsonRpcExceptionInterface` has a little value regarding debug error data composition on its own, |
| 51 | + // thus use previous exception, if available: |
| 52 | + return $this->composeDebugErrorData($error->getPrevious() ?? $error); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param \Throwable $error |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + private function composeDebugErrorData(\Throwable $error) : array |
| 60 | + { |
| 61 | + $data = [ |
| 62 | + '_class' => get_class($error), |
| 63 | + '_code' => $error->getCode(), |
| 64 | + '_message' => $error->getMessage(), |
| 65 | + ]; |
| 66 | + |
| 67 | + $trace = $this->filterErrorTrace($error->getTrace()); |
| 68 | + if (!empty($trace)) { |
| 69 | + $data['_trace'] = $trace; |
| 70 | + } |
| 71 | + |
| 72 | + return $data; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array $trace raw exception stack trace. |
| 77 | + * @return array simplified stack trace. |
| 78 | + */ |
| 79 | + private function filterErrorTrace(array $trace): array |
| 80 | + { |
| 81 | + $trace = array_slice($trace, 0, $this->maxTraceSize); |
| 82 | + |
| 83 | + $result = []; |
| 84 | + foreach ($trace as $entry) { |
| 85 | + if (array_key_exists('args', $entry)) { |
| 86 | + if ($this->showTraceArguments) { |
| 87 | + if ($this->simplifyTraceArguments) { |
| 88 | + $entry['args'] = $this->simplifyArguments($entry['args']); |
| 89 | + } |
| 90 | + } else { |
| 91 | + unset($entry['args']); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $result[] = $entry; |
| 96 | + } |
| 97 | + |
| 98 | + return $result; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Converts arguments array to their simplified representation. |
| 103 | + * |
| 104 | + * @param array $args arguments array to be converted. |
| 105 | + * @return string string representation of the arguments array. |
| 106 | + */ |
| 107 | + private function simplifyArguments(array $args) : string |
| 108 | + { |
| 109 | + $count = 0; |
| 110 | + |
| 111 | + $isAssoc = $args !== array_values($args); |
| 112 | + |
| 113 | + foreach ($args as $key => $value) { |
| 114 | + $count++; |
| 115 | + |
| 116 | + if ($count >= 5) { |
| 117 | + if ($count > 5) { |
| 118 | + unset($args[$key]); |
| 119 | + } else { |
| 120 | + $args[$key] = '...'; |
| 121 | + } |
| 122 | + |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + $args[$key] = $this->simplifyArgument($value); |
| 127 | + |
| 128 | + if (is_string($key)) { |
| 129 | + $args[$key] = "'" . $key . "' => " . $args[$key]; |
| 130 | + } elseif ($isAssoc) { |
| 131 | + // contains both numeric and string keys: |
| 132 | + $args[$key] = $key.' => '.$args[$key]; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return implode(', ', $args); |
| 137 | + } |
| 138 | + |
| 139 | + private function simplifyArgument(mixed $value): mixed |
| 140 | + { |
| 141 | + if (is_object($value)) { |
| 142 | + return get_class($value); |
| 143 | + } elseif (is_bool($value)) { |
| 144 | + return $value ? 'true' : 'false'; |
| 145 | + } elseif (is_string($value)) { |
| 146 | + if (strlen($value) > 64) { |
| 147 | + return "'" . substr($value, 0, 64) . "...'"; |
| 148 | + } else { |
| 149 | + return "'" . $value . "'"; |
| 150 | + } |
| 151 | + } elseif (is_array($value)) { |
| 152 | + return '[' . $this->simplifyArguments($value) . ']'; |
| 153 | + } elseif ($value === null) { |
| 154 | + return 'null'; |
| 155 | + } elseif (is_resource($value)) { |
| 156 | + return 'resource'; |
| 157 | + } |
| 158 | + |
| 159 | + return $value; |
| 160 | + } |
| 161 | +} |
0 commit comments