Skip to content

Commit 364918e

Browse files
authored
Document the error thrown by send on no FS match
1 parent a9c56c7 commit 364918e

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
@@ -787,6 +787,45 @@ app.use(async (context) => {
787787
await app.listen({ port: 8000 });
788788
```
789789

790+
When `send()` can't find a matching filesystem entry, it will throw an
791+
`HttpError` with its status set to `404` (`Status.NotFound`). This example
792+
illustrates handling that error in order to use another middleware (a fallback
793+
virtual filesystem) when `send()` can't find a match:
794+
795+
```ts
796+
import {
797+
Application,
798+
HttpError,
799+
Router,
800+
Status,
801+
} from "https://deno.land/x/oak/mod.ts";
802+
803+
const fallbackFilesystem = new Router()
804+
.get("/virtual_file.txt", (context) => {
805+
context.response.body = "Hello world";
806+
});
807+
808+
const app = new Application()
809+
.use(async (context, next) => {
810+
try {
811+
await context.send({
812+
root: `${Deno.cwd()}/examples/static`,
813+
index: "index.html",
814+
});
815+
} catch (ex) {
816+
if (ex instanceof HttpError && ex.status === Status.NotFound) {
817+
// send didn't find a matching filesystem entry
818+
return next();
819+
}
820+
throw ex;
821+
}
822+
})
823+
.use(fallbackFilesystem.routes())
824+
.use(fallbackFilesystem.allowedMethods());
825+
826+
await app.listen({ port: 8000 });
827+
```
828+
790829
`send()` automatically supports features like providing `ETag` and
791830
`Last-Modified` headers in the response as well as processing `If-None-Match`
792831
and `If-Modified-Since` headers in the request. This means when serving up

0 commit comments

Comments
 (0)