Enable ExceptionStrategy to return json #3944

Closed
wants to merge 3 commits into
from

Projects

None yet

4 participants

@superdweebie

Modifies the ExceptionStrategy to return a JsonViewModel if the Accept applicaton/json header is set. This means when an exception occurs on something such as a rest service, the client will be returned an exception that can be consumed, rather than a html document.

@Freeaqingme
Zend Framework member

@superdweebie Thank you for your pull request. Can you please provide an example of a json output that would now be generated? I'm hoping to review and test this myself later this week fyi.

@Freeaqingme Freeaqingme was assigned Mar 6, 2013
@superdweebie

@Freeaqingme this change will mean that ExceptionStrategy will return a JsonModel rather than a ViewModel if the accept: application/json header is set. Then, if ViewJsonStrategy is also set, it will render the view model variables as json. The variables are exactly the same for the JsonModel and the ViewModel, so, expected json output would be:

{
    message: 
    exception:
    display_exception:
}
@weierophinney
Zend Framework member

My only problem with this is that I'd prefer that the output follow some sort of standard -- e.g., application/api-problem+json or application/vnd.error. As such, I think that the selection of how the JSON output should look will often be very specific to the choices you make for your application and/or API -- and as such, would be a better candidate for a module or a 3rd party library than direct inclusion in the framework.

@superdweebie

@weierophinney at present to output a json error you need to create your own class which listens to the EVENT_DISPATCH_ERROR at a higher priority than ExceptionStrategy. That's a pain for quick and simple things, and has stumped more than one user (see http://stackoverflow.com/questions/14625360/exceptionstrategy-doesnt-work-with-jsonstrategy/14660828#14660828 and http://framework.zend.com/issues/browse/ZF2-379?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab#issue-tabs).

At present something like this happens:

  • I write my api using zf2. I figure out how to use JsonStrategy to return json from my controller and all is good.
  • I start testing some errors and realise that ExceptionStrategy doesn't work with JsonStrategy and returns html.
  • I get frustrated and lost, and end up on help forums.

I see your point about output formats. Would an alternative be allowing the default ExceptionStrategy to be overridden in config? Then provide some optional strategies that can return json in different formats?

@trainerbill

Mark me down as a user who was confused by this, found this pull request, and now understand why. I figured the ViewJsonStrategy option would output JSON if an exception occurred. I implemented the strategy that @superdweebie recommended above and it works. I am very new to ZF and this took a very long time to figure out for such a small issue. Please implement one of the strategies above to make this easier. Thanks.

@superdweebie

@trainerbill here's another exception strategy that outputs json conforming with the application/api-problem+json standard, if it's handy https://github.com/superdweebie/exceptionModule/blob/master/src/Sds/ExceptionModule/JsonExceptionStrategy.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment