-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 216 withContent returning 500 (#217)
Fix to return a 400 instead of a 500 when using the `withContent` middleware and sending no content of invalid JSON in the request body.
- Loading branch information
1 parent
415b2c1
commit 1c4f67b
Showing
2 changed files
with
68 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { IRequest, IRequestStrict } from './Router' | ||
import { StatusError } from './StatusError' | ||
|
||
export type HasContent<ContentType> = { | ||
content: ContentType, | ||
content: ContentType | ||
} & IRequestStrict | ||
|
||
// withContent - embeds any request body as request.content | ||
export const withContent = async (request: IRequest): Promise<void> => { | ||
if (request.headers.get('content-type')?.includes('json')) | ||
request.content = await request.json() | ||
try { | ||
request.content = await request.json() | ||
} catch (e: unknown) { | ||
const se = e as SyntaxError | ||
throw new StatusError(400, se.message) | ||
} | ||
} |