Skip to content

Commit

Permalink
[allsearch-api] Error statuses in JSON format
Browse files Browse the repository at this point in the history
This allows 400 and 500 JSON responses through from the application, and
returns other errors in JSON format, rather than the standard
"Something went wrong" html page.

Closes #4396

For background information on why we'd like to offer JSON errors, see
https://github.com/pulibrary/allsearch_api/blob/main/architecture-decisions/001_error_output.md
  • Loading branch information
sandbergja committed Jun 28, 2024
1 parent 2a957fe commit 9cafb42
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion roles/nginxplus/files/conf/http/allsearch-api_prod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ server {
proxy_cache allsearch-apicache;
}

include /etc/nginx/conf.d/templates/errors.conf;
include /etc/nginx/conf.d/templates/errors-in-json-format.conf;
}
3 changes: 1 addition & 2 deletions roles/nginxplus/files/conf/http/allsearch-api_staging.conf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ server {
deny all;
}

include /etc/nginx/conf.d/templates/errors.conf;

include /etc/nginx/conf.d/templates/errors-in-json-format.conf;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This error handling template is intended for applications
# like allsearch-api, which attempt to respond in json for
# all requests
#
# The application creates descriptive JSON for 400 and 500
# status codes, so those are not specifically handled here.
#
# More info at https://github.com/pulibrary/allsearch_api/blob/main/architecture-decisions/001_error_output.md

error_page 401 402 404 405 406 407 408 409 411 412 413 414 415 416 417 418 421 422 423 424 426 428 431 451 501 502 503 504 505 506 507 508 510 511 /error.json;

error_page 403 /forbidden.json;
error_page 413 /too_large.json;
error_page 429 /ratelimit.json;

location = /error.json {
ssi on;
internal;
root /var/local/www/default;
}

location = /forbidden.json {
ssi on;
internal;
root /var/local/www/default;
}

location = /too_large.json {
ssi on;
internal;
root /var/local/www/default;
}

location = /ratelimit.json {
ssi on;
internal;
root /var/local/www/default;
}
6 changes: 6 additions & 0 deletions roles/nginxplus/files/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"problem": "ERROR",
"message": "We encountered an error."
}
}
6 changes: 6 additions & 0 deletions roles/nginxplus/files/forbidden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"problem": "FORBIDDEN",
"message": "You are not allowed to access this. If you are attempting to reach a staging site, check if you are on VPN."
}
}
6 changes: 6 additions & 0 deletions roles/nginxplus/files/ratelimit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"problem": "TOO_MANY_REQUESTS",
"message": "You are making too many requests. Please find a way to accomplish your task without making so many requests."
}
}
6 changes: 6 additions & 0 deletions roles/nginxplus/files/too_large.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"problem": "CONTENT_TOO_LARGE",
"message": "You are making a request that is too large for us to process. If you are in a browser, check your cookies to make sure you are not sending many large cookies at once."
}
}

0 comments on commit 9cafb42

Please sign in to comment.