@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
39 lines (38 loc) • 1.66 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { debug } from "./debug-logger.js";
import { isThenable } from "./is.js";
// src/utils/safeCallback.ts
/**
* Lets a `safeCallback` fallback signal "the callback failed" as opposed to "the callback returned `null`",
* so the call site can report the drop as `callback_error`. Always return it, never throw it: a thrown
* sentinel would have to be caught at every boundary, and some of those (e.g. `prepareEvent`) are public.
*/
const CALLBACK_ERROR = Symbol.for("SentryCallbackError");
/**
* Invokes a user-provided callback (e.g. `beforeSend`, `tracesSampler`, an integration hook) so that
* neither a synchronous throw nor a rejected promise escapes into the caller. On failure the error is
* logged and `fallback(error)` supplies the result instead.
*
* Not for `startSpan` bodies: those must re-throw and are handled by `handleCallbackErrors`.
*
* @param message - Logged via `debug.error` together with the error. Pass it as `DEBUG_BUILD ? '...' : ''`
* so the string is tree-shaken from non-debug bundles.
* @param fn - Invokes the callback.
* @param fallback - Produces the result to use when the callback throws or rejects.
*/
function safeCallback(message, fn, fallback) {
let result;
try {
result = fn();
} catch (error) {
return recover(message, error, fallback);
}
if (isThenable(result)) return result.then(void 0, (error) => recover(message, error, fallback));
return result;
}
function recover(message, error, fallback) {
DEBUG_BUILD && debug.error(message, error);
return fallback(error);
}
export { CALLBACK_ERROR, safeCallback };
//# sourceMappingURL=safeCallback.js.map