James Fazio
James Fazio

Reputation: 6450

User error_handlers in app.yaml to return HTTP response

To quote the documentation from GCP you can use error_handlers in your app.yaml to configure custom error pages that are returned for different error types. From the documentation there is only mention of .html files being used.

Is there a way to send an HTTP response instead of an HTML page?

As an example, the desired outcome can be achieved in nginx with something like the following:

server {
 error_page                            502 @bad_gateway;
 location @bad_gateway {
  return                               502 '{\n\t"success": false,\n\t"code": -1,\n\t"message": "Server is down",\n\t"status_code": 502,\n\t"status_message": "Bad Gateway",\n\t"detailed": ["Server is down"]\n}';
  add_header                           Content-Type "application/json charset=UTF-8" always;
  internal;
 }
}

Upvotes: 0

Views: 316

Answers (1)

Daniel Ocando
Daniel Ocando

Reputation: 3764

Google App Engine uses the handler script that corresponds to the URL on the app.yaml configuration file when it receives a web request to your app. Here is all the relevant information on how requests are handled on the Google App Engine standard environment. I would put this logic on the script file and send the HTTP response from there, since the error_handlers field is specifically used to configure custom error pages. Check this link as a reference for your app.yaml file.

The error_handlers section exists to serve static files for the following specific error codes with App Engine: over_quota, dos_api_denial, and timeout. You can check that information on the documentation. You could serve for example an error.json file with all the structure provided on your Nginx example instead of a error.html file

Upvotes: 1

Related Questions