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

Improve documentation for serving static content #331

Open
jsejcksn opened this issue May 26, 2021 · 1 comment · May be fixed by #332
Open

Improve documentation for serving static content #331

jsejcksn opened this issue May 26, 2021 · 1 comment · May be fixed by #332
Labels
documentation This issue relates to improving the documentation good first issue Good for newcomers help wanted Extra attention is needed

Comments

@jsejcksn
Copy link
Contributor

Ref: #214

After reviewing the source for the send method, it seems that the function is not designed to be used in any place except the tail end of the middleware stack, since it throws (an HttpError with status 404) when there is no match.

This seems rather opinionated (which is fine), but it was not very intuitive for me, and I think it needs to be documented.

It would be nice to provide a configuration option for send (and context.send) so that this behavior can be changed — e.g. to allow invoking next (instead of throwing) when no match is found, so that it can used in other positions in the middleware stack.

@kitsonk kitsonk added documentation This issue relates to improving the documentation good first issue Good for newcomers help wanted Extra attention is needed labels May 26, 2021
@kitsonk
Copy link
Collaborator

kitsonk commented May 26, 2021

It would be nice to provide a configuration option for send (and context.send) so that this behavior can be changed — e.g. to allow invoking next (instead of throwing) when no match is found, so that it can used in other positions in the middleware stack.

The concept is that something that should be caught and acted upon. a try ... catch allows any implementor to choose how to respond to something that you expected to occur but didn't. In the statServer.ts example, there is an example of a middleware that does this:

// 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);
}
}
});

@jsejcksn jsejcksn linked a pull request May 27, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This issue relates to improving the documentation good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants