Pierre-André Vullioud Pierre-André Vullioud - 2 months ago 29
PHP Question

Return JSON for exception with octoberCMS

I have some custom routes

Route::get('/files/', [
'as' => 'read',
'uses' => 'Myname\MyPlugin\Http\Controllers\FilesController@read'
]);


Somewhere in my class I have a function to validate a path

private function getPath()
{
$path = Input::get('path');

if (!$path)
{
throw new MyException('parameter is missing. path required', 400);
}

return base_path().'/'.$path;
}


I have set a custom error handler with a JSOM but it's the error handler of OctoberCMS that render the error in HTML format.

Do you know a way to replace default error handler of OctoberCMS by a custom one ?
Thanks

Answer

Just found the anwser in the documentation : https://octobercms.com/docs/services/error-log#exception-handling

October provide App:error to manage Exception in your plugin.

App::error(function(MyException $exception) {
//do what you want here
}

Don't forger to create a custom Exception for your plugin. If you use the generic Exception, you'll catch all the Exceptions.