-
-
Notifications
You must be signed in to change notification settings - Fork 941
[GraphQL] Adding custom error format support #3063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GraphQL] Adding custom error format support #3063
Conversation
|
@mahmoodbazdar Looks good, I like the change to have clearer validation messages. What do you think about moving the part you added in |
|
@lukasluecke I think its good Idea. Do you think we should apply to these exceptions too? core/src/GraphQl/Action/EntrypointAction.php Lines 77 to 79 in 0354379
If we want to support that we can't move it to |
|
@mahmoodbazdar Good question - I'm not sure. Do you know which Exceptions this catch block actually applies to? However this doesn't change anything - if we want to handle this part as well we need to extract the code somehow anyway. What do you think about a new global service that accepts an |
|
@lukasluecke Its the exceptions that happen during GraphQL schema generation(Before GraphQL library calls for operation). They mainly happen because of user misconfiguration (500 errors).I'm not sure if we want to allow developers to format these kind of errors. We can create this separate service but it should return a Simpler solution will be something like this: $formatter = function (Error $error) {
$formatters = $this->exceptionFormatterFactory->getExceptionFormatters();
usort($formatters, function ($a, $b) {
/**
* @var ExceptionFormatterInterface
* @var ExceptionFormatterInterface $b
*/
if ($a->getPriority() == $b->getPriority()) {
return 0;
}
return ($a->getPriority() > $b->getPriority()) ? -1 : 1;
});
/** @var ExceptionFormatterInterface $exceptionFormatter */
foreach ($formatters as $exceptionFormatter) {
if (null !== $error->getPrevious() && $exceptionFormatter->supports($error->getPrevious())) {
return $exceptionFormatter->format($error);
}
}
// falling back to default GraphQL error formatter
return FormattedError::createFromException($error);
};
try {
$executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation)
->setErrorFormatter($formatter);
} catch (\Exception $e) {
$executionResult = (new ExecutionResult(null, [new Error($e->getMessage(), null, null, null, null, $e)]))
->setErrorFormatter($formatter);
} |
|
@mahmoodbazdar If the service has an |
|
@lukasluecke You are right. I'll do that. |
|
Looks good! Maybe @alanpoulain can chime in on the additional exceptions listed in the issue description? I think it would be good to have more control over these as well 👍 |
97fd40f to
aa6fb01
Compare
|
@lukasluecke Thanks for your help and review.😊 |
aa6fb01 to
bc93d98
Compare
ddda7ea to
dcd104c
Compare
dcd104c to
65755e6
Compare
|
Thank you very much for your work @mahmoodbazdar! After playing with your code, I realized using normalizers was preferable than introducing new interfaces and an exception formatter: it's a lot less code to maintain and it's more in the "API Platform's way" of doing this kind of things. I've kept your code in the first commit and I added mine in the second one. Sorry for doing this, I know it would have been better if I commented your code instead of adding mine, but I needed to play and try some things to understand it correctly and I ended up in this state. I hope you will take this well but please tell me if you don't. Could you also tell me if this approach is correct to you? @lukasluecke could you have a look too? As you suggested, I added more normalizers:
and I changed all the Error exceptions by classical PHP exceptions. |
|
@alanpoulain I think your approach is better. Thanks fo updating the code. |
|
Oh yes you're right! Would you want to take care of it? |
|
@alanpoulain Sure, Should I add tests for other normalizers (to fix code coverage failure)? |
|
I've added some tests for the other normalizers so I don't really understand if there is an issue or not (but I don't think there is). |
65755e6 to
91b025d
Compare
847a803 to
ef7a56e
Compare
ef7a56e to
2c6080b
Compare
|
Merged at last! Thank you very much @mahmoodbazdar, I think this feature is very useful to have. |
* Adding GraphQL custom error format support * Use error normalizers * Changing error format to follow GraphQL spec Co-authored-by: Alan Poulain <[email protected]>
|
@alanpoulain what do you think when this feature will be released? |
|
Hi @CvekCoding this was merged on master, master will be the next minor cycle, for now we're still on 2.5. We don't really have a due date for a new 2.6 release in the meantime you could use our dev-master version directly :/. |
|
@soyuka is it safe to use |
* Adding GraphQL custom error format support * Use error normalizers * Changing error format to follow GraphQL spec Co-authored-by: Alan Poulain <[email protected]> (cherry picked from commit 9920f15)
Allows developer to format exceptions based on exception type. For now I just added ValidationException support built in. I think we can add several others but I thought first its better to receive your feedback and then add them.
These are the exceptions I think its good to add builtin support:
core/src/GraphQl/Resolver/Stage/ReadStage.php
Line 75 in 0354379
core/src/GraphQl/Resolver/Stage/SecurityPostDenormalizeStage.php
Line 61 in 0354379
and here:
core/src/GraphQl/Resolver/Stage/SecurityStage.php
Line 53 in 0354379
core/src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php
Line 109 in 0354379