Cakephp default error response but i need to json format error response

Hi!! please help me, I want to json response when error trigger in cakephp, but currently i am getting default template response. Let me explain what i want exactly, Suppose when page not found404 or user not found even more any server internal error occurred then cakephp throw default error behaviour, it show default error template page but i want to any kind of error occurred response should be come in json format or xml format.! please help to this guys.
thank you

Do you want the error message always in JSON, or always in XML, or sometimes in one but sometimes the other? If “sometimes”, what would be the deciding factor in which to use?

hi!! thanks for such a great reply sir, but i need only json response when any error throw by cakephp, not want to display default error layout in application. suppose if page not found error occurred then error response come to in default template page but i wann it in json format (content-type: application/json) 404, like its API json response.

So, you want the error response in JSON all the time? You had originally said “in json format or xml format”, so that wasn’t clear.

Next question, are your success responses also always supposed to be JSON, or do they come in HTML?

yes i want to json response always… actually iam developing apis for reactjs so when any error occurred in api so that error should be in json format. You can try with ajax all when i hit the apis via ajax request and if any record not found in table then i received the default cakephp error template… which is not good thing in the security view point. suppose i called /user/getall in the ajax if record not found or any other error occurred then api return me default template response… and if user open the console form the browser & then click on user/getall url if open in new tab and show the cakephp template like Controller not found or template are missing… but i want it just show me only json response, in json response show me error message please let me help this and try to fix this issue in cakephp next version… laravel provide reach functionality regarding like scenario, laravel never return like this response. viewClass return me json response even all errors but i wann without viewJson class

So, you want everything to use JSON, but you don’t want to use the JSON view that’s explicitly for doing this?

There are several ways to address this, I think. You could set an appropriate Accept header in the request, then the responses should always be JSON. You could make your own error handler middleware to catch errors and output in JSON instead. There might be others. This was just discussed at some length recently in the Slack channel, as I recall.

please provide me example with code sample.

As mentioned in the docs CakePHP ships with error renderers for both web and console environments. If however, you would like to replace the logic that renders errors you can create a custom error renderer class which then outputs json.

But you can also try something like GitHub - mixerapi/mixerapi: CakePHP API Plugin for REST + JSON API Development [READ-ONLY] which has

  • Error Handling: Handles exception rendering in XML or JSON.

as you can see in the readme

But if you only need the exception rendering part you can also just use GitHub - mixerapi/exception-render: Handles rendering entity validation errors and other exceptions for your API [READ-ONLY]