@keyban/sdk-base
Version:
Keyban Javascript SDK provides core functionalities for the MPC wallet solution, supporting web and Node.js apps with TypeScript, custom storage, and Ethereum blockchain integration.
295 lines (290 loc) • 10.3 kB
JavaScript
// src/errors/KeybanBaseError.ts
var KeybanBaseError = class extends Error {
/**
* A URI reference that identifies the problem type. This property is mandatory and provides a
* machine-readable identifier for the error.
* @example
* ```typescript
* error.type // "https://api.prod.keyban.io/errors/address-invalid"
* ```
*/
type;
/**
* A short, human-readable summary of the problem type. It should remain consistent across
* occurrences of the problem except for localization purposes.
* @example
* ```typescript
* error.title // "Invalid Ethereum Address"
* ```
*/
title;
/**
* A URI reference that identifies the specific occurrence of the problem. This provides a
* unique identifier for the particular instance of the error.
* @example
* ```typescript
* error.instance // "validateAddress-001"
* ```
*/
instance;
/**
* The HTTP status code generated by the origin server for this occurrence of the problem.
* It is a numeric value and is included for the convenience of the client.
* @example
* ```typescript
* error.status // 400
* ```
*/
status;
/**
* A human-readable explanation specific to this occurrence of the problem. This field helps
* the client understand and potentially correct the issue.
* @example
* ```typescript
* error.detail // "The provided Ethereum address is not valid."
* ```
*/
detail;
/**
* A timestamp recording the exact date and time when the exception occurred, in ISO8601 format
* (`YYYY-MM-DDTHH:mm:ss.sssZ`).
* @example
* ```typescript
* error.timestamp // "2024-04-27T12:34:56.789Z"
* ```
*/
timestamp;
/**
* The original error instance, if any. This can be used to trace the root cause of the error.
* @example
* ```typescript
* error.rootError // Original Error instance or additional error details
* ```
*/
rootError;
/**
* Serializes the error instance into a JSON object, including all relevant properties.
* This is useful for logging, debugging, or transmitting error information.
* @returns - A JSON representation of the error.
* @example
* ```typescript
* const errorJson = error.toJSON();
* console.log(errorJson);
* ```
*/
toJSON() {
return {
type: this.type,
title: this.title,
instance: this.instance,
status: this.status,
detail: this.detail,
timestamp: this.timestamp,
rootError: this.rootError
};
}
/**
* Creates an instance of `KeybanBaseError`. It initializes the error with the provided properties,
* sets the error message, and records the timestamp.
* @param params - The parameters to initialize the error.
* @param params.type - The specific error type identifier.
* @param [params.title] - A short summary of the error. Defaults to the `type` if not provided.
* @param params.instance - A unique identifier for this specific error occurrence.
* @param [params.rootError] - The original error, if any.
* @param [params.detail] - A detailed explanation of the error.
* @param [params.status] - The HTTP status code associated with the error.
* @example
* ```typescript
* const error = new KeybanBaseError({
* type: SdkErrorTypes.AddressInvalid,
* instance: "validateAddress-001",
* detail: "The provided Ethereum address is not valid.",
* status: 400,
* });
* ```
*/
constructor({
type,
title,
instance,
rootError,
detail,
status
}) {
super(detail ?? title ?? type);
this.type = type;
this.title = title ?? type;
this.instance = instance;
this.detail = detail;
this.status = status;
this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
this.rootError = rootError;
}
};
// src/errors/CryptoError.ts
var CryptoErrorType = /* @__PURE__ */ ((CryptoErrorType2) => {
CryptoErrorType2["GenerateKey"] = "GenerateKey";
CryptoErrorType2["ExportKey"] = "ExportKey";
CryptoErrorType2["ImportKey"] = "ImportKey";
CryptoErrorType2["Encrypt"] = "Encrypt";
CryptoErrorType2["Decrypt"] = "Decrypt";
return CryptoErrorType2;
})(CryptoErrorType || {});
var CryptoError = class _CryptoError extends KeybanBaseError {
static types = CryptoErrorType;
constructor(type, instance, rootError) {
super({
type,
instance,
rootError,
title: _CryptoError.#getTitle(type)
});
}
static #getTitle(errorType) {
switch (errorType) {
case "GenerateKey" /* GenerateKey */:
return "Error generating a cryptographic key";
case "ExportKey" /* ExportKey */:
return "Error exporting a cryptographic key";
case "ImportKey" /* ImportKey */:
return "Error importing a cryptographic key";
case "Encrypt" /* Encrypt */:
return "Error encrypting data";
case "Decrypt" /* Decrypt */:
return "Error decrypting data";
default:
return `Unknown error type: ${errorType}`;
}
}
};
// src/errors/IFrameApiError.ts
var IFrameApiErrorType = /* @__PURE__ */ ((IFrameApiErrorType2) => {
IFrameApiErrorType2["InvalidOrigin"] = "InvalidOrigin";
IFrameApiErrorType2["InvalidCall"] = "InvalidCall";
return IFrameApiErrorType2;
})(IFrameApiErrorType || {});
var IFrameApiError = class _IFrameApiError extends KeybanBaseError {
static types = IFrameApiErrorType;
constructor(type, instance, rootError) {
super({
type,
instance,
rootError,
title: _IFrameApiError.#getTitle(type)
});
}
static #getTitle(errorType) {
switch (errorType) {
case "InvalidOrigin" /* InvalidOrigin */:
return "The current domain is not allowed for this appId";
case "InvalidCall" /* InvalidCall */:
return "Invalid method call, either the method does not exists or is not allowed";
default:
return `Unknown error type: ${errorType}`;
}
}
};
// src/errors/JwtError.ts
var JwtErrorType = /* @__PURE__ */ ((JwtErrorType2) => {
JwtErrorType2["InvalidToken"] = "InvalidToken";
return JwtErrorType2;
})(JwtErrorType || {});
var JwtError = class _JwtError extends KeybanBaseError {
static types = JwtErrorType;
constructor(type, instance, rootError) {
super({
type,
instance,
rootError,
title: _JwtError.#getTitle(type)
});
}
static #getTitle(errorType) {
switch (errorType) {
case "InvalidToken" /* InvalidToken */:
return "You provided an invalid access token";
default:
return `Unknown error type: ${errorType}`;
}
}
};
// src/errors/SdkError.ts
var SdkErrorTypes = /* @__PURE__ */ ((SdkErrorTypes2) => {
SdkErrorTypes2["UnknownTransactionError"] = "UnknownTransactionError";
SdkErrorTypes2["UnknownIframeApiError"] = "UnknownIframeApiError";
SdkErrorTypes2["AddressInvalid"] = "AddressInvalid";
SdkErrorTypes2["AmountInvalid"] = "AmountInvalid";
SdkErrorTypes2["AmountRequired"] = "AmountRequired";
SdkErrorTypes2["AmountIrrelevant"] = "AmountIrrelevant";
SdkErrorTypes2["RecipientAddressEqualsSender"] = "RecipientAddressEqualsSender";
SdkErrorTypes2["EstimateGasExecution"] = "EstimateGasExecution";
SdkErrorTypes2["InsufficientFunds"] = "InsufficientFunds";
SdkErrorTypes2["InvalidNftStandard"] = "InvalidNftStandard";
SdkErrorTypes2["NftNotFound"] = "NftNotFound";
SdkErrorTypes2["TokenBalanceNotFound"] = "TokenBalanceNotFound";
return SdkErrorTypes2;
})(SdkErrorTypes || {});
var SdkError = class _SdkError extends KeybanBaseError {
/**
* The available SDK error types.
*/
static types = SdkErrorTypes;
/**
* Creates an instance of `SdkError`.
* @param type - The specific type of SDK error.
* @param instance - The identifier of the instance where the error occurred.
* @param [rootError] - The original error that caused this SDK error, if any.
* @throws {SdkError} Throws an error if an unknown `SdkErrorTypes` is provided.
* @example
* ```typescript
* throw new SdkError(SdkErrorTypes.InsufficientFunds, "transferFunction");
* ```
*/
constructor(type, instance, rootError) {
super({
type,
instance,
rootError,
title: _SdkError.#getTitle(type)
});
}
/**
* Retrieves a human-readable title based on the error type.
* @private
* @param errorType - The type of SDK error.
* @returns - A descriptive title for the error.
*/
static #getTitle(errorType) {
switch (errorType) {
case "UnknownTransactionError" /* UnknownTransactionError */:
return "An unknown error occurred with the transaction";
case "UnknownIframeApiError" /* UnknownIframeApiError */:
return "An unknown error occured with iframe API call";
case "AddressInvalid" /* AddressInvalid */:
return "Address is invalid";
case "AmountInvalid" /* AmountInvalid */:
return "The specified amount is invalid";
case "AmountRequired" /* AmountRequired */:
return "An amount is required for this operation";
case "AmountIrrelevant" /* AmountIrrelevant */:
return "The amount provided is irrelevant and should not be included";
case "RecipientAddressEqualsSender" /* RecipientAddressEqualsSender */:
return "Recipient address cannot be the same as the sender address";
case "EstimateGasExecution" /* EstimateGasExecution */:
return "Gas estimation failed";
case "InsufficientFunds" /* InsufficientFunds */:
return "Insufficient funds to complete the transaction";
case "InvalidNftStandard" /* InvalidNftStandard */:
return "Invalid NFT standard. Supported standards are ERC721 and ERC1155";
case "NftNotFound" /* NftNotFound */:
return "NFT not found with the provided contract address and token ID";
case "TokenBalanceNotFound" /* TokenBalanceNotFound */:
return "Token balance not found with the provided contract address";
default:
return `Unknown error type: ${errorType}`;
}
}
};
export { CryptoError, CryptoErrorType, IFrameApiError, IFrameApiErrorType, JwtError, JwtErrorType, KeybanBaseError, SdkError, SdkErrorTypes };
//# sourceMappingURL=chunk-7H2SLR6W.js.map
//# sourceMappingURL=chunk-7H2SLR6W.js.map