getHeaders() : [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES, ); } /** * Render a validation failure as one envelope entry per message, each carrying the * offending field and the rule that rejected it. */ public static function validation(ValidationException $exception): JsonResponse { $codes = self::validationRuleCodes($exception); $errors = []; foreach ($exception->errors() as $field => $messages) { throw_if(! is_string($field) || ! is_array($messages), UnexpectedValueException::class, 'Laravel returned malformed validation errors.'); foreach ($messages as $key => $message) { throw_if(! is_int($key) || ! is_string($message), UnexpectedValueException::class, 'Laravel returned a malformed validation error message.'); $normalizedField = str_replace('.', '_', $field); $meta = [ 'source_field' => $field, 'rule' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', $codes[$normalizedField][$key] ?? ''), ]; $converted = self::toArray($exception)['errors'][0]; $converted['detail'] = $message; $converted['meta'] = array_merge($converted['meta'] ?? [], $meta); $errors[] = $converted; } } return new JsonResponse(['errors' => $errors], $exception->status); } /** * The envelope for an exception; in debug mode it also carries the source * location and argument-free traces of the exception and everything before it. * * @param ApiErrorOverride $override * @return ApiErrorEnvelope */ public static function toArray(Throwable $e, array $override = []): array { $error = [ 'code' => class_basename($e), 'status' => (string) self::status($e), 'detail' => $e instanceof HttpExceptionInterface || self::mappedStatus($e) !== null ? $e->getMessage() : 'An unexpected error was encountered while processing this request, please try again.', ]; if ($e instanceof ModelNotFoundException || $e->getPrevious() instanceof ModelNotFoundException) { // Show a nicer error message compared to the standard "No query results for model" // response that is normally returned. If we are in debug mode this will get overwritten // with a more specific error message to help narrow down things. $error['detail'] = 'The requested resource could not be found on the server.'; } if (config('app.debug')) { $error = array_merge($error, [ 'detail' => $e->getMessage(), 'source' => [ 'line' => $e->getLine(), 'file' => str_replace(base_path(), '', $e->getFile()), ], 'meta' => [ 'trace' => self::cleanTrace($e), 'previous' => array_map(self::cleanTrace(...), self::extractPrevious($e)), ], ]); } return ['errors' => [array_merge($error, $override)]]; } private static function status(Throwable $e): int { if ($e instanceof HttpExceptionInterface) { return $e->getStatusCode(); } return self::mappedStatus($e) ?? 500; } /** * Exceptions that carry no HTTP status of their own but map to a fixed one. */ private static function mappedStatus(Throwable $e): ?int { return match (true) { $e instanceof AuthenticationException => 401, $e instanceof AuthorizationException => 403, $e instanceof ValidationException, $e instanceof InvalidExtensionException => 422, default => null, }; } /** * Every exception that led to the given one being thrown, outermost first. * * @return list */ private static function extractPrevious(Throwable $e): array { $previous = []; while ($value = $e->getPrevious()) { $previous[] = $value; $e = $value; } return $previous; } /** * @return array> */ private static function validationRuleCodes(ValidationException $exception): array { $codes = []; foreach ($exception->validator->failed() as $field => $reasons) { throw_if(! is_string($field) || ! is_array($reasons), UnexpectedValueException::class, 'Laravel returned malformed failed validation rules.'); $cleaned = []; foreach (array_keys($reasons) as $reason) { throw_unless(is_string($reason), UnexpectedValueException::class, 'Laravel returned a malformed validation rule name.'); $cleaned[] = Str::snake($reason); } $codes[str_replace('.', '_', $field)] = $cleaned; } return $codes; } /** @return list> */ private static function cleanTrace(Throwable $exception): array { $cleaned = []; foreach ($exception->getTrace() as $frame) { $item = ['function' => $frame['function']]; foreach (['file', 'line', 'class', 'type'] as $key) { if (isset($frame[$key])) { $item[$key] = $frame[$key]; } } $cleaned[] = $item; } return $cleaned; } }