Handle webhook errors with `InvalidWebhookError`
masterThe SDK uses a single unified error class, InvalidWebhookError, for all webhook-related failures (signature mismatch, malformed gzip, malformed base64, or invalid JSON).
You can use err.message combined with the exported InvalidWebhookErrorMessages constants to distinguish between specific failure modes, such as a signature mismatch versus a malformed payload.
const { InvalidWebhookError, InvalidWebhookErrorMessages } = require('stream-chat');
try {
const event = client.verifyAndParseWebhook(req.body, req.headers['x-signature']);
} catch (err) {
if (err instanceof InvalidWebhookError) {
if (err.message === InvalidWebhookErrorMessages.signatureMismatch) {
return res.sendStatus(401);
}
return res.sendStatus(400);
}
throw err;
}