Files
wekan/server/lib/webhookGuard.js
T
Lauri OjansivuandClaude Opus 4.8 8863ec24a7 Fix #1402: outgoing webhook no longer blocks setting card members
Setting a card member creates an Activity, and the Activities.after.insert hook
delivered the outgoing webhook in a way that could throw (e.g. unreachable/slow
endpoint) and unwind the async hook, aborting the member update. Webhook delivery
is now fire-and-forget and wrapped in safeDeliver(), which catches and logs any
error and never rejects, so a failing webhook can't break the member operation.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 19:54:30 +03:00

47 lines
1.7 KiB
JavaScript

/**
* server/lib/webhookGuard.js — non-blocking, error-isolated webhook delivery
*
* Outgoing webhooks are fired from the `Activities.after.insert` hook. If the
* delivery throws (network error, timeout, slow/unreachable endpoint, invalid
* response), that exception must NOT propagate into the activity insert or the
* originating operation (e.g. adding/removing a card member). Otherwise the
* member update is aborted / rolled back and never persists.
*
* `safeDeliver` runs the supplied delivery function and ALWAYS resolves — it
* never rejects. On success it resolves `{ ok: true }`; on any thrown error
* (sync or async) it logs and resolves `{ ok: false, error }`.
*
* This is a pure, dependency-free helper so it can be unit tested without
* Meteor.
*
* Usage:
* import { safeDeliver } from '/server/lib/webhookGuard';
* safeDeliver(() => Meteor.callAsync('outgoingWebhooks', ...));
*/
/**
* Run `deliverFn` and resolve regardless of outcome (never rejects).
*
* @param {Function} deliverFn - function performing the webhook delivery; may
* be synchronous or return a Promise.
* @returns {Promise<{ok: boolean, error?: Error}>}
*/
export async function safeDeliver(deliverFn) {
try {
if (typeof deliverFn !== 'function') {
throw new TypeError('safeDeliver: deliverFn must be a function');
}
await deliverFn();
return { ok: true };
} catch (error) {
// Swallow + log: a failing webhook must never abort the caller.
try {
// eslint-disable-next-line no-console
console.error('[webhookGuard] outgoing webhook delivery failed:', error && error.message ? error.message : error);
} catch (_logErr) {
// ignore logging failures
}
return { ok: false, error };
}
}