Skip to content

Commit 244f634

Browse files
committed
Document the error thrown by send on no FS match
1 parent c4fe8b3 commit 244f634

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,45 @@ app.use(async (context, next) => {
729729
await app.listen({ port: 8000 });
730730
```
731731

732+
When `send()` can't find a matching filesystem entry, it will throw an
733+
`HttpError` with its status set to `404` (`Status.NotFound`). This example
734+
illustrates handling that error in order to use another middleware (a fallback
735+
virtual filesystem) when `send()` can't find a match:
736+
737+
```ts
738+
import {
739+
Application,
740+
HttpError,
741+
Router,
742+
Status,
743+
} from "https://deno.land/x/oak/mod.ts";
744+
745+
const fallbackFilesystem = new Router()
746+
.get("/virtual_file.txt", (context) => {
747+
context.response.body = "Hello world";
748+
});
749+
750+
const app = new Application()
751+
.use(async (context, next) => {
752+
try {
753+
await context.send({
754+
root: `${Deno.cwd()}/examples/static`,
755+
index: "index.html",
756+
});
757+
} catch (ex) {
758+
if (ex instanceof HttpError && ex.status === Status.NotFound) {
759+
// send didn't find a matching filesystem entry
760+
return next();
761+
}
762+
throw ex;
763+
}
764+
})
765+
.use(fallbackFilesystem.routes())
766+
.use(fallbackFilesystem.allowedMethods());
767+
768+
await app.listen({ port: 8000 });
769+
```
770+
732771
`send()` automatically supports features like providing `ETag` and
733772
`Last-Modified` headers in the response as well as processing `If-None-Match`
734773
and `If-Modified-Since` headers in the request. This means when serving up

0 commit comments

Comments
 (0)