This should be configurable for sure, and merely applies to Exceptions.
But it’s also worth to think about a common return format of AJAX requests where parts could be automatically forwarded to the configured option (modal, alert, user-flash, etc …).
I use something very similar to what you proposed. But instead of using the HTTP status code (which can be retrieved from the response object itself), I use an explicit success indicator:
{
success: true,
message: 'Ok',
data: [1, 7, 9]
}
{
success: false,
message: 'There was an error handling your request. Please try again in a few minutes.',
data: null
}
We now have the Response object which supports different output formats, including JSON.
For AJAX requests, we recommend using JSON as the data communication format. By setting the response format to be JSON, you can simply return the data to be sent back as the return value of actions. If there’s any problem, you can throw an exception, and the exception will be automatically turned into a JSON result.