fix: handle empty JSON bodies in content type parser
Fix "Unexpected end of JSON input" error by: - Check for empty/whitespace-only body before parsing - Return empty object for empty bodies instead of failing - Add detailed error logging for debugging JSON parse failures - Prevent crashes on malformed or empty request bodies 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
bf15353dd0
commit
5ede024b4c
@ -97,11 +97,21 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
|
|||||||
{ parseAs: 'string' },
|
{ parseAs: 'string' },
|
||||||
function (req, body, done) {
|
function (req, body, done) {
|
||||||
try {
|
try {
|
||||||
const json = JSON.parse(body as string);
|
const bodyStr = body as string;
|
||||||
|
|
||||||
|
// Handle empty body case
|
||||||
|
if (!bodyStr || bodyStr.trim() === '') {
|
||||||
|
(req as any).rawBody = bodyStr;
|
||||||
|
done(null, {});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = JSON.parse(bodyStr);
|
||||||
// Store raw body for webhook signature verification
|
// Store raw body for webhook signature verification
|
||||||
(req as any).rawBody = body;
|
(req as any).rawBody = bodyStr;
|
||||||
done(null, json);
|
done(null, json);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
log({ module: 'content-parser', level: 'error' }, `JSON parse error: ${err.message}, body: "${body}"`);
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
done(err, undefined);
|
done(err, undefined);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user