forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved HTTP errors for REST API (denoland#444)
This change adds middleware for the REST API that catches HTTP errors, as defined in the Standard Library, and returns non-HTML responses. This will make future REST API development cleaner. Currently, responses are still responding with HTML. Perhaps, the middleware is set up incorrectly.
- Loading branch information
Showing
11 changed files
with
121 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { type MiddlewareHandlerContext, Status } from "$fresh/server.ts"; | ||
import { isHttpError } from "std/http/http_errors.ts"; | ||
|
||
export async function handler( | ||
_req: Request, | ||
ctx: MiddlewareHandlerContext, | ||
) { | ||
try { | ||
return await ctx.next(); | ||
} catch (error) { | ||
return isHttpError(error) | ||
? new Response(error.message, { | ||
status: error.status, | ||
headers: error.headers, | ||
}) | ||
: new Response(error.message, { status: Status.InternalServerError }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { type Handlers, Status } from "$fresh/server.ts"; | ||
import type { Handlers } from "$fresh/server.ts"; | ||
import { getItem } from "@/utils/db.ts"; | ||
import { errors } from "std/http/http_errors.ts"; | ||
|
||
export const handler: Handlers = { | ||
async GET(_req, ctx) { | ||
const item = await getItem(ctx.params.id); | ||
return item === null | ||
? new Response(null, { status: Status.NotFound }) | ||
: Response.json(item); | ||
if (item === null) throw new errors.NotFound("Item not found"); | ||
return Response.json(item); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { Handlers, Status } from "$fresh/server.ts"; | ||
import type { Handlers } from "$fresh/server.ts"; | ||
import { getUser } from "@/utils/db.ts"; | ||
import { errors } from "std/http/http_errors.ts"; | ||
|
||
export const handler: Handlers = { | ||
async GET(_req, ctx) { | ||
const user = await getUser(ctx.params.login); | ||
return user === null | ||
? new Response(null, { status: Status.NotFound }) | ||
: Response.json(user); | ||
if (user === null) throw new errors.NotFound("User not found"); | ||
return Response.json(user); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters