Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BadRequest URLs should serve a nicer error page #774

Open
jameshadfield opened this issue Jan 4, 2024 · 1 comment
Open

BadRequest URLs should serve a nicer error page #774

jameshadfield opened this issue Jan 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jameshadfield
Copy link
Member

jameshadfield commented Jan 4, 2024

Current Behavior

URLs which result in BadRequest errors server side send an overly plain HTML page with "Bad Request" (in production mode) or the stack trace (in dev mode).

As an example, the url path /staging/WNV_NA (which is a bad request because underscores aren't allowed for datasets) looks like so (production nextstrain.org on the left, localhost on the right):
image

Expected behavior

A page more like that for a valid but non-existing (404) dataset, but with content which communicates that the URL was a bad request, why it was, and (if possible) how one might fix the URL.

image

Your environment: if browsing Nextstrain online

Nextstrain.org production server running from 24ba9ee

Additional context

The following patch results in Gatsby's 404 page being sent (as seen in the 2nd screenshot above), which is aesthetically an improvement but it changes the client-facing error to 404 and doesn't communicate the error message.

diff --git a/src/routing/errors.js b/src/routing/errors.js
index da31d741..ce4702a0 100644
--- a/src/routing/errors.js
+++ b/src/routing/errors.js
@@ -2,7 +2,7 @@ import stream from 'stream';
 import { promisify } from 'util';
 import { PRODUCTION } from '../config.js';
 import * as utils from '../utils/index.js';
-import { Forbidden, NotFound, Unauthorized } from '../httpErrors.js';
+import { Forbidden, NotFound, BadRequest, Unauthorized } from '../httpErrors.js';
 import * as endpoints from '../endpoints/index.js';
 import { AuthzDenied } from '../exceptions.js';
 
@@ -113,7 +113,7 @@ export async function setup(app) {
         .end();
     }
 
-    if (err instanceof NotFound) {
+    if (err instanceof NotFound || err instanceof BadRequest ) {
       /* A note about routing: if the current URL path (i.e. req.path) matches a
        * a page known to Gatsby (e.g. via the Gatsby page's "path" or "matchPath"
        * properties), then Gatsby will perform client-side routing to load that
@jameshadfield jameshadfield added the bug Something isn't working label Jan 4, 2024
@jameshadfield
Copy link
Member Author

As an extension of this issue, and one way to solve it:

We sometimes include helpful information in the various HTTP Errors (server-side), such as

NotFound(`Version descriptor ${versionDescriptor} predates available versions`)
BadRequest(`Requested version must be in YYYY-MM-DD format (version descriptor requested: "${versionDescriptor}")`)
BadRequest(`Resource (e.g. dataset and narrative) paths may not include underscores (_); use slashes (/) instead`);

Were the returned Gatsby page able to access this information (and the corresponding error code) we could then communicate this which would be a nice improvement over the current state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant