Normalize API errors
When an error occurs the patch demo API has some surprising behavior:
- errors happening before the CORS headers are set would cause a prefetch error
- respond with a 200 status code due to the usage of
die()
- Error messages are emitted in plain text instead of being wrapped in a json payload.
The Gerrit Javascript integration had troubles due to some combinations of the above (T363355 and T367155).
Move the error function from api.php to a new errorutils.php so that we can reuse it from includes.php.
Change the function to respond by default with a 503 (server side error). Introduce bad_request_error() helper which sets the response code to 400 and update callers in api.php to switch to it.
Since we need api.php to always emit json and error() can be called from includes.php, define PATCH_DEMO_JSON_API and make error() to emit plain text when it is not defined, wrap the message in json otherwhise.
Disable display_startup_errors and display_errors for API calls.
Replace most die() calls by our error() wrapper. There is one left for the OAuth signin prompt.
Bug: T367155