Improve debug messages for async webhook handler (#105)

This commit is contained in:
Krzysztof Wolski 2022-11-03 19:44:54 +01:00 committed by GitHub
parent 7abad6a3d8
commit 33d7666c7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -117,22 +117,25 @@ export class SaleorAsyncWebhook<TPayload = unknown> {
*/ */
createHandler(handlerFn: NextWebhookApiHandler<TPayload>): NextApiHandler { createHandler(handlerFn: NextWebhookApiHandler<TPayload>): NextApiHandler {
return async (req, res) => { return async (req, res) => {
debug(`Handler for webhook ${this.name} called`);
await processAsyncSaleorWebhook<TPayload>({ await processAsyncSaleorWebhook<TPayload>({
req, req,
apl: this.apl, apl: this.apl,
allowedEvent: this.asyncEvent, allowedEvent: this.asyncEvent,
}) })
.then(async (context) => { .then(async (context) => {
debug("Call handlerFn"); debug("Incoming request validated. Call handlerFn");
return handlerFn(req, res, context); return handlerFn(req, res, context);
}) })
.catch((e) => { .catch((e) => {
debug(`Unexpected error during processing the webhook ${this.name}`);
if (e instanceof WebhookError) { if (e instanceof WebhookError) {
debug(e.message); debug(`Validation error: ${e.message}`);
res.status(ErrorCodeMap[e.errorType] || 400).end(); res.status(ErrorCodeMap[e.errorType] || 400).end();
return; return;
} }
debug("Unexpected error during processing the webhook %O", e); debug("Unexpected error: %O", e);
res.status(500).end(); res.status(500).end();
}); });
}; };