exceptionRenderer - string - The class responsible for rendering uncaught exceptions. If you choose a custom class you should place the file for that class in src/Error . This class needs to implement a render() method.
Make sure to only overwrite response if the request is api, otherwise return default render() method, like:
<?php
namespace App\Error;
// use statements
class ApiExceptionRenderer extends ExceptionRenderer
{
/**
* Renders the response for the exception.
*
* @return \Cake\Http\Response The response to be sent.
*/
public function render()
{
if (! $this->request->is('api')) {
return parent::render();
}
// your logic to return json response goes here
}
}
You should add detector when you are in your api class using, so it will be available in your custom exceptionRenderer class:
$this->getRequest()->addDetector('api', function () {
return true;
});
This will pretty much do it, still if you have any problem just let us know.