Make 500 exception spit out JSON instead of HTML

Using Ajax, we sometimes have internal errors in the PHP code, making Yii spit out 500. This message is always in HTML, not JSON. Is it possible to make it JSON instead, for certain controller methods?

Something like this (but not manually):

header('Content-Type: application/json');
http_response_code(405);
die(json_encode(['message' => 'Could not save label set.']));

Using the onException event for now.

1 Like

https://www.yiiframework.com/doc/api/1.1/CErrorHandler#errorAction-detail cold be used to make your own handler.