Skip to content

Commit

Permalink
Improve error messages for BadMultipartRequestGraphQLException
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed May 16, 2023
1 parent 8184a54 commit 31986ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
8 changes: 3 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ parameters:
- '#.*#'
check:
missingCheckedExceptionInThrows: true

ignoreErrors:
-
message: "#Method .* throws checked exception .* but it's missing from the PHPDoc @throws tag.#"
paths:
- tests/*
- message: "#Method .* throws checked exception .* but it's missing from the PHPDoc @throws tag.#"
paths:
- tests/*
2 changes: 1 addition & 1 deletion src/BadMultipartRequestGraphQLException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BadMultipartRequestGraphQLException extends BadRequestHttpException
public function __construct(string $message, \Throwable $previous = null, int $code = 0, array $headers = [])
{
parent::__construct(
$message . ' Be sure to conform to the GraphQL multipart request specification (https://github.com/jaydenseric/graphql-multipart-request-spec).',
"{$message} Be sure to conform to the GraphQL multipart request specification (https://github.com/jaydenseric/graphql-multipart-request-spec).",
$previous,
$code,
$headers
Expand Down
38 changes: 22 additions & 16 deletions src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,54 +103,60 @@ protected function bodyParams(Request $request): array
*/
protected function inlineFiles(Request $request): array
{
/** @var string|null $mapParam */
$mapParam = $request->post('map');
if (null === $mapParam) {
throw new BadMultipartRequestGraphQLException('Could not find a valid map.');
throw new BadMultipartRequestGraphQLException('Missing parameter map.');
}
if (! is_string($mapParam)) {
$mapParamType = gettype($mapParam);
throw new BadMultipartRequestGraphQLException("Expected parameter map to be a JSON string, got: {$mapParamType}.");
}

/** @var string|null $operationsParam */
$operationsParam = $request->post('operations');
if (null === $operationsParam) {
throw new BadMultipartRequestGraphQLException('Could not find a valid operations.');
throw new BadMultipartRequestGraphQLException('Missing parameter operations.');
}
if (! is_string($operationsParam)) {
$operationsParamType = gettype($operationsParam);
throw new BadMultipartRequestGraphQLException("Expected parameter operations to be a JSON string, got: {$operationsParamType}.");
}

try {
/** Should be array<string, mixed>|array<int, array<string, mixed>>, but it's user input so can be anything */
/** Should be array<string, mixed>|array<int, array<string, mixed>>, but it's user input, so it can be anything. */
$operations = json_decode($operationsParam, true);
} catch (JsonException $e) {
throw new BadMultipartRequestGraphQLException('Parameter operations is not a valid json.', $e);
throw new BadMultipartRequestGraphQLException('Parameter operations is not valid JSON.', $e);
}

if (! is_array($operations)) {
$type = gettype($operations);
throw new BadMultipartRequestGraphQLException("Expected operations to be array, got: {$type}.");
$operationsType = gettype($operations);
throw new BadMultipartRequestGraphQLException("Expected parameter operations to be array, got: {$operationsType}.");
}

try {
/** Should be array<int|string, array<int, string>>, but it's user input so can be anything */
/** Should be array<int|string, array<int, string>>, but it's user input, so it can be anything */
$map = json_decode($mapParam, true);
} catch (JsonException $e) {
throw new BadMultipartRequestGraphQLException('Parameter map is not a valid json.', $e);
throw new BadMultipartRequestGraphQLException('Parameter map is not valid JSON.', $e);
}

if (! is_array($map)) {
$type = gettype($map);
throw new BadMultipartRequestGraphQLException("Expected map to be array, got: {$type}.");
$mapType = gettype($map);
throw new BadMultipartRequestGraphQLException("Expected parameter map to be array, got: {$mapType}.");
}

foreach ($map as $fileKey => $operationsPaths) {
$file = $request->file((string) $fileKey);

if (! is_iterable($operationsPaths)) {
$type = gettype($operationsPaths);
throw new BadMultipartRequestGraphQLException("Expected map to be array of arrays, got: {$type}");
$operationsPathsType = gettype($operationsPaths);
throw new BadMultipartRequestGraphQLException("Expected map to be array of arrays, got: {$operationsPathsType}.");
}

foreach ($operationsPaths as $operationsPath) {
if (! is_string($operationsPath)) {
$type = gettype($operationsPath);
throw new BadMultipartRequestGraphQLException("Expected map to be array of arrays of strings, got {$type}");
$operationsPathType = gettype($operationsPath);
throw new BadMultipartRequestGraphQLException("Expected map to be array of arrays of strings, got {$operationsPathType}.");
}
Arr::set($operations, $operationsPath, $file);
}
Expand Down

0 comments on commit 31986ad

Please sign in to comment.