Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce json stringify #920

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export async function middleware(
response.writeHead(422, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: `Request URL could not be parsed: ${request.url}`,
}),
);
response.end(`{"error":"Request URL could not be parsed: ${request.url}"}`);
return true;
}

Expand All @@ -55,9 +51,7 @@ export async function middleware(
accept: "application/json",
});
response.end(
JSON.stringify({
error: `Unsupported "Content-Type" header value. Must be "application/json"`,
}),
'{"error":"Unsupported \\"Content-Type\\" header value. Must be \\"application/json\\""}',
);
return true;
}
Expand All @@ -68,11 +62,8 @@ export async function middleware(
response.writeHead(400, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: `Required headers missing: ${missingHeaders}`,
}),
);

response.end(`{"error":"Required headers missing: ${missingHeaders}"}`);

return true;
}
Expand Down Expand Up @@ -113,18 +104,19 @@ export async function middleware(
if (didTimeout) return true;

const err = Array.from(error as WebhookEventHandlerError)[0];
const errorMessage = err.message
? `${err.name}: ${err.message}`
: "Error: An Unspecified error occurred";
response.statusCode = typeof err.status !== "undefined" ? err.status : 500;

options.log.error(error);

response.end(
JSON.stringify({
error: errorMessage,
}),
);
if (err.message) {
response.end(
JSON.stringify({
error: `${err.name}: ${err.message}`,
}),
);
} else {
response.end('{"error": "Error: An Unspecified error occurred"}');
}

return true;
}
Expand Down