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

Document the error thrown by send on no FS match #332

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jsejcksn
Copy link
Contributor

Resolves #331

This documents the specific error thrown by send() when no filesystem entry is matched to the request path, and adds an example of how to handle the error in order to use send() in other positions in the middleware stack besides the very end.

@kitsonk
Copy link
Collaborator

kitsonk commented May 29, 2021

Why not use an error middleware, like in the examples/staticServer.ts?

// Error handler middleware
app.use(async (context, next) => {
try {
await next();
} catch (e) {
if (e instanceof HttpError) {
// deno-lint-ignore no-explicit-any
context.response.status = e.status as any;
if (e.expose) {
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>${e.status} - ${e.message}</h1>
</body>
</html>`;
} else {
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>${e.status} - ${Status[e.status]}</h1>
</body>
</html>`;
}
} else if (e instanceof Error) {
context.response.status = 500;
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>500 - Internal Server Error</h1>
</body>
</html>`;
console.log("Unhandled Error:", red(bold(e.message)));
console.log(e.stack);
}
}
});

That is the more common pattern of handling errors in a middleware application.

@jsejcksn
Copy link
Contributor Author

Why not use an error middleware

@kitsonk I'm not sure what you mean. Do you mean adding a general purpose error handling middleware function in addition to the stack in the example?

The purpose of adding this documentation is:

  • to point out that send() throws when a match isn't found, and
  • to detail the specifics of the exception thrown:
    • The exception is an instance of HttpError (ex instanceof HttpError)
    • exception.status is equal to 404 (ex.status === Status.NotFound)

@jsejcksn
Copy link
Contributor Author

Hi @kitsonk

Do you have more feedback for showing users how to use send() first in their middleware stack so that filesystem has priority over other middleware?

I'd love to make any changes needed so that this documentation can be merged and others won't have to go through the same confusion that I did before doing a review of this middleware source code.

@CLAassistant
Copy link

CLAassistant commented Jun 29, 2021

CLA assistant check
All committers have signed the CLA.

@twhitbeck
Copy link

fwiw I found this PR extremely helpful. I also went through a bit of confusion when using send. I figured send itself could be configured to either terminate with 404 or fall through to the next middleware (like https://github.com/expressjs/serve-static#fallthrough).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve documentation for serving static content
4 participants