Skip to content

Commit

Permalink
break the errors classes into multiple files for JSDoc rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotFriend committed May 17, 2024
1 parent b0b93bb commit 403d165
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 99 deletions.
99 changes: 0 additions & 99 deletions src/errors.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/errors/account_requires_memo.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions src/errors/bad_request.ts
Original file line number Diff line number Diff line change
@@ -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";
}
}
24 changes: 24 additions & 0 deletions src/errors/bad_response.ts
Original file line number Diff line number Diff line change
@@ -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";
}
}
5 changes: 5 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./network";
export * from "./not_found";
export * from "./bad_request";
export * from "./bad_response";
export * from "./account_requires_memo";
46 changes: 46 additions & 0 deletions src/errors/network.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

22 changes: 22 additions & 0 deletions src/errors/not_found.ts
Original file line number Diff line number Diff line change
@@ -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";
}
}

0 comments on commit 403d165

Please sign in to comment.