-
Notifications
You must be signed in to change notification settings - Fork 7
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
CloudCreativity\JsonApi\Error\ThrowableError should allow an independent code #13
Comments
Thanks for the feedback! What you've written was exactly my intention for That will change (I believe) in PHP 7 because there's going to be a throwable interface but obviously I'll need to support PHP 5.5/6 for a while longer. So the upshot is that $err = new ErrorObject([
ErrorObject::TITLE => 'Error',
ErrorObject::STATUS => 500,
ErrorObject::CODE => 'custom-error-code',
]);
throw new ErrorException($err); Maybe as a thought it would be best to deprecate The other option you have is that you could use your own Exception class(es) for the JWT authentication and then add an exception renderer that handles those specifically. There's some renderers in the |
I have to agree with you on deprecating I'm following your advices on both issues I opened (#13, #14) and it makes sense from the perspective of my app's structure, however I'm encountering 1 issue I haven't been able to solve and I'd like your opinion on it if you don't mind: Currently I'm using So I need a way to determine which errors should be logged (e.g. JWT creation error) from the non loggable ones (e.g. invalid JWT). I've tried to extend your Am I doing something wrong here? I read the last part of your comment several times but I'm afraid I don't get how to achieve the behavour I want with the exception renderer....could you please give me an example? Thank you very much in advance! |
Several options here! You won't need any of these if you wait for me to add the errors reply helper. That won't work by throwing an exception as it won't be required if the errors helper takes an 1. LoggingIn the application I have using the public function shouldReport(Exception $e)
{
if ($e instanceof ErrorInterface) {
return 499 < $e->getStatus();
} elseif ($e instanceof ErrorsAwareInterface) {
return 499 < $e->getErrors()->getStatus();
}
return parent::shouldReport($e);
} You could do something else with your own logic which could test the status or the 2. Custom Rendering via ConfigYou could implement your own JWT exceptions that do not implement any JSON API interfaces and throw them. In your json-api config file, you could set up an array representation of what the JSON API rendering should be for that specific JWT exception class. This line in the example config shows how: 3. Write Your Own RendererAlternatively have your own JWT exception class, and write a custom exception renderer class. This can be registered in your I.e. you'd need to add something like this: $container->resolving(RendererContainerInterface::class, function (RendererContainerInterface $rendererContainer) use ($container) {
/** @var ResponsesInterface $response */
$responses = $container->make(ResponsesInterface::class);
$errorRenderer = new JWTExceptionRenderer($responses);
$rendererContainer->registerRenderer(JWTException::class, $errorRenderer);
}); Your All you'd need is your own logic in the |
@lindyhopchris hey! thanks so much for your in-depth explanation. I'll play around with the different options you've given me to find the 1 I'm most comfortable with. However I'm more interested in simply using that helper method of yours 👍 If I understood correctly by using the errors reply helper there won't be an exception thrown... so no log will happen? If I can somehow help you with it please let me know I'll be more than willing to help out. Keep up the good work! |
This is available on the It adds an |
Hello @lindyhopchris I'm sorry I took a few days to reply, I've tested the {
"data": null
} Is it for the Either way, I believe you can merge the changes into master now 😄 thanks again! |
The I'll have a look at merging tomorrow. |
Alright 👍 |
@nickelozz I've merged on |
👍 |
I've been playing around with this package using both the
CloudCreativity\JsonApi\Error\ThrowableError
and theCloudCreativity\JsonApi\Error\ErrorObject
classes to try implementing JWT authentication exception and error responses but I noticed a radical difference between both of em.ThrowableError doesn't seem to use a provided "code" key in the array sent to its constructor's first parameter (
CloudCreativity\JsonApi\Error\ThrowableError line: 76
), I can use a separate $code parameter but it allows only null|integer values, which goes against JSON API standard as stated on their specification:code: an application-specific error code, expressed as a string value.
I believe ThrowableError should not use the same code that's being used for the exception code and allow for a different application's specific code to be added which should be treated as a string and rendered on the response.
What do you think?
The text was updated successfully, but these errors were encountered: