diff --git a/src/errors.ts b/src/errors.ts deleted file mode 100644 index 82894b138..000000000 --- a/src/errors.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { HorizonApi } from "./horizon/horizon_api"; - -// For ES5 compatibility (https://stackoverflow.com/a/55066280). -/* tslint:disable:variable-name max-classes-per-file */ - -export class NetworkError extends Error { - public response: { - data?: HorizonApi.ErrorResponseData; - status?: number; - statusText?: string; - url?: string; - }; - public __proto__: NetworkError; - - constructor(message: string, response: any) { - const trueProto = new.target.prototype; - super(message); - this.__proto__ = trueProto; - this.constructor = NetworkError; - this.response = response; - } - - public getResponse() { - return this.response; - } -} - -export class NotFoundError extends NetworkError { - constructor(message: string, response: any) { - const trueProto = new.target.prototype; - super(message, response); - this.__proto__ = trueProto; - this.constructor = NotFoundError; - this.name = "NotFoundError"; - } -} - -export class BadRequestError extends NetworkError { - constructor(message: string, response: any) { - const trueProto = new.target.prototype; - super(message, response); - this.__proto__ = trueProto; - this.constructor = BadRequestError; - this.name = "BadRequestError"; - } -} - -export class BadResponseError extends NetworkError { - constructor(message: string, response: any) { - const trueProto = new.target.prototype; - super(message, response); - this.__proto__ = trueProto; - this.constructor = BadResponseError; - this.name = "BadResponseError"; - } -} - -/** - * AccountRequiresMemoError is raised when a transaction is trying to submit an - * operation to an account which requires a memo. See - * [SEP0029](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md) - * for more information. - * - * This error contains two attributes to help you identify the account requiring - * the memo and the operation where the account is the destination - * - * ``` - * console.log('The following account requires a memo ', err.accountId) - * console.log('The account is used in operation: ', err.operationIndex) - * ``` - * - */ -export class AccountRequiresMemoError extends Error { - public __proto__: AccountRequiresMemoError; - /** - * accountId account which requires a memo. - */ - public accountId: string; - /** - * operationIndex operation where accountId is the destination. - */ - public operationIndex: number; - - /** - * Create an AccountRequiresMemoError - * @param {string} message error message - * @param {string} accountId The account which requires a memo. - * @param {number} operationIndex The index of the operation where `accountId` is the destination. - */ - constructor(message: string, accountId: string, operationIndex: number) { - const trueProto = new.target.prototype; - super(message); - this.__proto__ = trueProto; - this.constructor = AccountRequiresMemoError; - this.name = "AccountRequiresMemoError"; - this.accountId = accountId; - this.operationIndex = operationIndex; - } -} diff --git a/src/errors/account_requires_memo.ts b/src/errors/account_requires_memo.ts new file mode 100644 index 000000000..2b493f4da --- /dev/null +++ b/src/errors/account_requires_memo.ts @@ -0,0 +1,37 @@ +/** + * AccountRequiresMemoError is raised when a transaction is trying to submit an + * operation to an account which requires a memo. See + * [SEP0029](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md) + * for more information. + * + * This error contains two attributes to help you identify the account requiring + * the memo and the operation where the account is the destination + * + * @example + * console.log('The following account requires a memo ', err.accountId) + * console.log('The account is used in operation: ', err.operationIndex) + * + */ +export class AccountRequiresMemoError extends Error { + public __proto__: AccountRequiresMemoError; + public accountId: string; + public operationIndex: number; + + /** + * Create an AccountRequiresMemoError + * @param {string} message Human-readable error message + * @param {string} accountId The account which requires a memo + * @param {number} operationIndex The index of the operation where `accountId` is the destination + */ + constructor(message: string, accountId: string, operationIndex: number) { + const trueProto = new.target.prototype; + super(message); + this.__proto__ = trueProto; + this.constructor = AccountRequiresMemoError; + this.name = "AccountRequiresMemoError"; + /** @type {string} account which requires a memo. */ + this.accountId = accountId; + /** @type {number} operation where accountId is the destination. */ + this.operationIndex = operationIndex; + } +} diff --git a/src/errors/bad_request.ts b/src/errors/bad_request.ts new file mode 100644 index 000000000..d82ef7cd4 --- /dev/null +++ b/src/errors/bad_request.ts @@ -0,0 +1,22 @@ +import { NetworkError } from "./network"; + +/** + * BadRequestError is raised when a request made to Horizon is invalid in some + * way (incorrect timebounds for trade call builders, for example.) + * @extends NetworkError + * @inheritdoc + */ +export class BadRequestError extends NetworkError { + /** + * Create a BadRequestError. + * @param {string} message Human-readable error message + * @param {any} response Response details, received from the Horizon server + */ + constructor(message: string, response: any) { + const trueProto = new.target.prototype; + super(message, response); + this.__proto__ = trueProto; + this.constructor = BadRequestError; + this.name = "BadRequestError"; + } +} diff --git a/src/errors/bad_response.ts b/src/errors/bad_response.ts new file mode 100644 index 000000000..97d0cc68a --- /dev/null +++ b/src/errors/bad_response.ts @@ -0,0 +1,24 @@ +import { NetworkError } from "./network"; + +/** + * BadResponseError is raised when a response from a {@link module:Horizon} or + * {@link module:Federation} is invalid in some way. For example, a federation + * response may exceed the maximum allowed size, or a transaction submission may + * have failed with Horizon. + * @extends NetworkError + * @inheritdoc + */ +export class BadResponseError extends NetworkError { + /** + * Create a BadResponseError. + * @param {string} message Human-readable error message. + * @param {any} response Response details, received from the Horizon server. + */ + constructor(message: string, response: any) { + const trueProto = new.target.prototype; + super(message, response); + this.__proto__ = trueProto; + this.constructor = BadResponseError; + this.name = "BadResponseError"; + } +} diff --git a/src/errors/index.ts b/src/errors/index.ts new file mode 100644 index 000000000..cb4f19458 --- /dev/null +++ b/src/errors/index.ts @@ -0,0 +1,5 @@ +export * from "./network"; +export * from "./not_found"; +export * from "./bad_request"; +export * from "./bad_response"; +export * from "./account_requires_memo"; diff --git a/src/errors/network.ts b/src/errors/network.ts new file mode 100644 index 000000000..f22114408 --- /dev/null +++ b/src/errors/network.ts @@ -0,0 +1,46 @@ +import { HorizonApi } from "../horizon/horizon_api"; + +// For ES5 compatibility (https://stackoverflow.com/a/55066280). + +/** + * NetworkError is raised when an interaction with a Horizon server has caused + * some kind of problem. + */ +export class NetworkError extends Error { + public response: { + data?: HorizonApi.ErrorResponseData; + status?: number; + statusText?: string; + url?: string; + }; + public __proto__: NetworkError; + + /** + * Create a NetworkError. + * @param {string} message Human-readable error message + * @param {any} response Response details, received from the Horizon server + */ + constructor(message: string, response: any) { + const trueProto = new.target.prototype; + super(message); + this.__proto__ = trueProto; + this.constructor = NetworkError; + /** + * @typedef {Object} + * @property {HorizonApi.ErrorResponseData} [data] The data returned by Horizon as part of the error: {@link https://developers.stellar.org/network/horizon/api-reference/errors/response | Error Response} + * @property {number} [status] HTTP status code describing the basic issue with a submitted transaction {@link https://developers.stellar.org/network/horizon/api-reference/errors/http-status-codes/standard | Standard Status Codes} + * @property {string} [statusText] A human-readable description of what the status code means: {@link https://developers.stellar.org/network/horizon/api-reference/errors/http-status-codes/horizon-specific | Horizon-Specific Status Codes} + * @property {string} [url] URL which can provide more information about the problem that occurred. + */ + this.response = response; + } + + /** + * Get the response object that created this error. + * @returns {any} + */ + public getResponse() { + return this.response; + } +} + diff --git a/src/errors/not_found.ts b/src/errors/not_found.ts new file mode 100644 index 000000000..040e60edc --- /dev/null +++ b/src/errors/not_found.ts @@ -0,0 +1,22 @@ +import { NetworkError } from "./network"; + +/** + * NotFoundError is raised when the resource requested from Horizon is + * unavailable. + * @extends NetworkError + * @inheritdoc + */ +export class NotFoundError extends NetworkError { + /** + * Create a NotFoundError. + * @param {string} message Human-readable error message + * @param {any} response Response details, received from the Horizon server + */ + constructor(message: string, response: any) { + const trueProto = new.target.prototype; + super(message, response); + this.__proto__ = trueProto; + this.constructor = NotFoundError; + this.name = "NotFoundError"; + } +}