@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
191 lines (189 loc) • 6.02 kB
JavaScript
import {
deprecated
} from "./chunk-KJVJ4CFF.mjs";
// src/error.ts
function isUnauthorizedError(e) {
const status = e?.status;
const code = e?.errors?.[0]?.code;
return code === "authentication_invalid" && status === 401;
}
function is4xxError(e) {
const status = e?.status;
return !!status && status >= 400 && status < 500;
}
function isNetworkError(e) {
const message = (`${e.message}${e.name}` || "").toLowerCase().replace(/\s+/g, "");
return message.includes("networkerror");
}
function isKnownError(error) {
return isClerkAPIResponseError(error) || isMetamaskError(error) || isClerkRuntimeError(error);
}
function isClerkAPIResponseError(err) {
return "clerkError" in err;
}
function isClerkRuntimeError(err) {
return "clerkRuntimeError" in err;
}
function isMetamaskError(err) {
return "code" in err && [4001, 32602, 32603].includes(err.code) && "message" in err;
}
function parseErrors(data = []) {
return data.length > 0 ? data.map(parseError) : [];
}
function parseError(error) {
return {
code: error.code,
message: error.message,
longMessage: error.long_message,
meta: {
paramName: error?.meta?.param_name,
sessionId: error?.meta?.session_id,
emailAddresses: error?.meta?.email_addresses,
identifiers: error?.meta?.identifiers,
zxcvbn: error?.meta?.zxcvbn
}
};
}
var ClerkAPIResponseError = class _ClerkAPIResponseError extends Error {
constructor(message, { data, status }) {
super(message);
this.toString = () => {
return `[${this.name}]
Message:${this.message}
Status:${this.status}
Serialized errors: ${this.errors.map(
(e) => JSON.stringify(e)
)}`;
};
Object.setPrototypeOf(this, _ClerkAPIResponseError.prototype);
this.status = status;
this.message = message;
this.clerkError = true;
this.errors = parseErrors(data);
}
};
var ClerkRuntimeError = class _ClerkRuntimeError extends Error {
constructor(message, { code }) {
super(message);
/**
* Returns a string representation of the error.
*
* @returns {string} A formatted string with the error name and message.
* @memberof ClerkRuntimeError
*/
this.toString = () => {
return `[${this.name}]
Message:${this.message}`;
};
Object.setPrototypeOf(this, _ClerkRuntimeError.prototype);
this.code = code;
this.message = message;
this.clerkRuntimeError = true;
}
};
var MagicLinkError = class _MagicLinkError extends Error {
constructor(code) {
super(code);
this.code = code;
Object.setPrototypeOf(this, _MagicLinkError.prototype);
deprecated("MagicLinkError", "Use `EmailLinkError` instead.");
}
};
var EmailLinkError = class _EmailLinkError extends Error {
constructor(code) {
super(code);
this.code = code;
Object.setPrototypeOf(this, _EmailLinkError.prototype);
}
};
function isMagicLinkError(err) {
deprecated("isMagicLinkError", "Use `isEmailLinkError` instead.");
return err instanceof MagicLinkError;
}
function isEmailLinkError(err) {
return err instanceof EmailLinkError;
}
var _MagicLinkErrorCode = {
Expired: "expired",
Failed: "failed"
};
var MagicLinkErrorCode = new Proxy(_MagicLinkErrorCode, {
get(target, prop, receiver) {
deprecated("MagicLinkErrorCode", "Use `EmailLinkErrorCode` instead.");
return Reflect.get(target, prop, receiver);
}
});
var EmailLinkErrorCode = {
Expired: "expired",
Failed: "failed"
};
var DefaultMessages = Object.freeze({
InvalidFrontendApiErrorMessage: `The frontendApi passed to Clerk is invalid. You can get your Frontend API key at https://dashboard.clerk.com/last-active?path=api-keys. (key={{key}})`,
InvalidProxyUrlErrorMessage: `The proxyUrl passed to Clerk is invalid. The expected value for proxyUrl is an absolute URL or a relative path with a leading '/'. (key={{url}})`,
InvalidPublishableKeyErrorMessage: `The publishableKey passed to Clerk is invalid. You can get your Publishable key at https://dashboard.clerk.com/last-active?path=api-keys. (key={{key}})`,
MissingPublishableKeyErrorMessage: `Missing publishableKey. You can get your key at https://dashboard.clerk.com/last-active?path=api-keys.`
});
function buildErrorThrower({ packageName, customMessages }) {
let pkg = packageName;
const messages = {
...DefaultMessages,
...customMessages
};
function buildMessage(rawMessage, replacements) {
if (!replacements) {
return `${pkg}: ${rawMessage}`;
}
let msg = rawMessage;
const matches = rawMessage.matchAll(/{{([a-zA-Z0-9-_]+)}}/g);
for (const match of matches) {
const replacement = (replacements[match[1]] || "").toString();
msg = msg.replace(`{{${match[1]}}}`, replacement);
}
return `${pkg}: ${msg}`;
}
return {
setPackageName({ packageName: packageName2 }) {
if (typeof packageName2 === "string") {
pkg = packageName2;
}
return this;
},
setMessages({ customMessages: customMessages2 }) {
Object.assign(messages, customMessages2 || {});
return this;
},
throwInvalidPublishableKeyError(params) {
throw new Error(buildMessage(messages.InvalidPublishableKeyErrorMessage, params));
},
throwInvalidFrontendApiError(params) {
throw new Error(buildMessage(messages.InvalidFrontendApiErrorMessage, params));
},
throwInvalidProxyUrl(params) {
throw new Error(buildMessage(messages.InvalidProxyUrlErrorMessage, params));
},
throwMissingPublishableKeyError() {
throw new Error(buildMessage(messages.MissingPublishableKeyErrorMessage));
}
};
}
export {
isUnauthorizedError,
is4xxError,
isNetworkError,
isKnownError,
isClerkAPIResponseError,
isClerkRuntimeError,
isMetamaskError,
parseErrors,
parseError,
ClerkAPIResponseError,
ClerkRuntimeError,
MagicLinkError,
EmailLinkError,
isMagicLinkError,
isEmailLinkError,
MagicLinkErrorCode,
EmailLinkErrorCode,
buildErrorThrower
};
//# sourceMappingURL=chunk-2O574DKN.mjs.map