@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
108 lines (105 loc) • 3.29 kB
JavaScript
//#region src/errors/createErrorTypeGuard.ts
/**
* Creates a type guard function for any error class.
* The returned function can be called as a standalone function or as a method on an error object.
*
* @example
* ```typescript
* class MyError extends Error {}
* const isMyError = createErrorTypeGuard(MyError);
*
* // As a standalone function
* if (isMyError(error)) { ... }
*
* // As a method (when attached to error object)
* if (error.isMyError()) { ... }
* ```
*/
function createErrorTypeGuard(ErrorClass) {
function typeGuard(error) {
const target = error ?? this;
if (!target) throw new TypeError(`${ErrorClass.kind || ErrorClass.name} type guard requires an error object`);
if (ErrorClass.kind && typeof target === "object" && target !== null && "constructor" in target) {
if (target.constructor?.kind === ErrorClass.kind) return true;
}
return target instanceof ErrorClass;
}
return typeGuard;
}
//#endregion
//#region src/errors/clerkError.ts
var ClerkError = class ClerkError extends Error {
static kind = "ClerkError";
clerkError = true;
code;
longMessage;
docsUrl;
cause;
get name() {
return this.constructor.name;
}
constructor(opts) {
super(new.target.formatMessage(new.target.kind, opts.message, opts.code, opts.docsUrl), { cause: opts.cause });
Object.setPrototypeOf(this, ClerkError.prototype);
this.code = opts.code;
this.docsUrl = opts.docsUrl;
this.longMessage = opts.longMessage;
this.cause = opts.cause;
}
toString() {
return `[${this.name}]\nMessage:${this.message}`;
}
static formatMessage(name, msg, code, docsUrl) {
const prefix = "Clerk:";
const regex = new RegExp(prefix.replace(" ", "\\s*"), "i");
msg = msg.replace(regex, "");
msg = `${prefix} ${msg.trim()}\n\n(code="${code}")\n\n`;
if (docsUrl) msg += `\n\nDocs: ${docsUrl}`;
return msg;
}
};
/**
* Type guard to check if a value is a ClerkError instance.
*/
function isClerkError(val) {
return createErrorTypeGuard(ClerkError)(val) || !!val && typeof val === "object" && "clerkError" in val && val.clerkError === true;
}
//#endregion
//#region src/errors/clerkRuntimeError.ts
/**
* Custom error class for representing Clerk runtime errors.
*
* @class ClerkRuntimeError
*
* @example
* throw new ClerkRuntimeError('An error occurred', { code: 'password_invalid' });
*/
var ClerkRuntimeError = class ClerkRuntimeError extends ClerkError {
static kind = "ClerkRuntimeError";
/**
* @deprecated Use `clerkError` property instead. This property is maintained for backward compatibility.
*/
clerkRuntimeError = true;
constructor(message, options) {
super({
...options,
message
});
Object.setPrototypeOf(this, ClerkRuntimeError.prototype);
}
};
/**
* Type guard to check if an error is a ClerkRuntimeError.
* Can be called as a standalone function or as a method on an error object.
*
* @example
* // As a standalone function
* if (isClerkRuntimeError(error)) { ... }
*
* // As a method (when attached to error object)
* if (error.isClerkRuntimeError()) { ... }
*/
const isClerkRuntimeError = createErrorTypeGuard(ClerkRuntimeError);
//#endregion
export { createErrorTypeGuard as a, isClerkError as i, isClerkRuntimeError as n, ClerkError as r, ClerkRuntimeError as t };
//# sourceMappingURL=clerkRuntimeError-DlesLWqO.mjs.map