Skip to content

Commit cc4de95

Browse files
authored
Merge pull request #11 from Zheness/master
Improving error messages
2 parents 2348937 + 9196946 commit cc4de95

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/ControllerQueryProvider.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ private function getFieldsByAnnotations(string $annotationName): array
117117

118118
$phpdocType = $typeResolver->resolve((string) $refMethod->getReturnType());
119119

120-
$type = $this->mapType($phpdocType, $refMethod->getDocBlockReturnTypes(), $standardPhpMethod->getReturnType()->allowsNull(), false);
121-
120+
try {
121+
$type = $this->mapType($phpdocType, $refMethod->getDocBlockReturnTypes(), $standardPhpMethod->getReturnType()->allowsNull(), false);
122+
} catch (TypeMappingException $e) {
123+
throw TypeMappingException::wrapWithReturnInfo($e, $refMethod);
124+
}
122125
$queryList[] = new QueryField($methodName, $type, $args, [$this->controller, $methodName], $this->hydrator);
123126
}
124127
}
@@ -174,9 +177,13 @@ private function mapParameters(ReflectionMethod $refMethod, \ReflectionMethod $s
174177
}
175178
$phpdocType = $typeResolver->resolve($type);
176179

177-
$arr = [
178-
'type' => $this->mapType($phpdocType, $parameter->getDocBlockTypes(), $allowsNull, true),
179-
];
180+
try {
181+
$arr = [
182+
'type' => $this->mapType($phpdocType, $parameter->getDocBlockTypes(), $allowsNull, true),
183+
];
184+
} catch (TypeMappingException $e) {
185+
throw TypeMappingException::wrapWithParamInfo($e, $parameter);
186+
}
180187

181188
if ($standardParameter->allowsNull()) {
182189
$arr['default'] = null;
@@ -207,8 +214,7 @@ private function mapType(Type $type, array $docBlockTypes, bool $isNullable, boo
207214
}
208215
$filteredDocBlockTypes = $this->typesWithoutNullable($docBlockTypes);
209216
if (empty($filteredDocBlockTypes)) {
210-
// TODO: improve error message
211-
throw new GraphQLException("Don't know how to handle type ".((string) $type));
217+
throw TypeMappingException::createFromType($type);
212218
} elseif (count($filteredDocBlockTypes) === 1) {
213219
$graphQlType = $this->toGraphQlType($filteredDocBlockTypes[0], $mapToInputType);
214220
} else {

src/TypeMappingException.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQL\Controllers;
5+
6+
7+
use phpDocumentor\Reflection\Type;
8+
use phpDocumentor\Reflection\Types\Array_;
9+
use phpDocumentor\Reflection\Types\Mixed_;
10+
use Roave\BetterReflection\Reflection\ReflectionFunction;
11+
use Roave\BetterReflection\Reflection\ReflectionMethod;
12+
use Roave\BetterReflection\Reflection\ReflectionParameter;
13+
14+
class TypeMappingException extends GraphQLException
15+
{
16+
private $type;
17+
18+
public static function createFromType(Type $type)
19+
{
20+
$e = new self("Don't know how to handle type ".(string) $type);
21+
$e->type = $type;
22+
return $e;
23+
}
24+
25+
public static function wrapWithParamInfo(TypeMappingException $previous, ReflectionParameter $parameter): TypeMappingException
26+
{
27+
if ($previous->type instanceof Array_) {
28+
$message = sprintf('Parameter $%s in %s::%s is type-hinted to array. Please provide an additional @param in the PHPDoc block to further specify the type of the array. For instance: @param string[] $%s.',
29+
$parameter->getName(),
30+
$parameter->getDeclaringClass()->getName(),
31+
$parameter->getDeclaringFunction()->getName(),
32+
$parameter->getName());
33+
} elseif ($previous->type instanceof Mixed_) {
34+
$message = sprintf('Parameter $%s in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint. For instance: "string $%s".',
35+
$parameter->getName(),
36+
$parameter->getDeclaringClass()->getName(),
37+
$parameter->getDeclaringFunction()->getName(),
38+
$parameter->getName());
39+
} else {
40+
throw new GraphQLException("Unexpected type in TypeMappingException");
41+
}
42+
43+
$e = new self($message, 0, $previous);
44+
$e->type = $previous->type;
45+
return $e;
46+
}
47+
48+
public static function wrapWithReturnInfo(TypeMappingException $previous, ReflectionMethod $method): TypeMappingException
49+
{
50+
if ($previous->type instanceof Array_) {
51+
$message = sprintf('Return type in %s::%s is type-hinted to array. Please provide an additional @return in the PHPDoc block to further specify the type of the array. For instance: @return string[]',
52+
$method->getDeclaringClass()->getName(),
53+
$method->getName());
54+
} elseif ($previous->type instanceof Mixed_) {
55+
$message = sprintf('Return type in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint.',
56+
$method->getDeclaringClass()->getName(),
57+
$method->getName());
58+
} else {
59+
throw new GraphQLException("Unexpected type in TypeMappingException");
60+
}
61+
62+
$e = new self($message, 0, $previous);
63+
$e->type = $previous->type;
64+
return $e;
65+
}
66+
}

0 commit comments

Comments
 (0)