@ledgerhq/coin-stellar
Version:
Ledger Stellar Coin integration
67 lines • 2.62 kB
JavaScript
;
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.messageFromHorizonUnknown = messageFromHorizonUnknown;
exports.throwHorizonLedgerOrOperationsError = throwHorizonLedgerOrOperationsError;
const errors_1 = require("@ledgerhq/errors");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const horizonErrorBody_1 = require("./horizonErrorBody");
function messageFromHorizonUnknown(e) {
const body = (0, horizonErrorBody_1.getHorizonErrorBody)(e);
if (body) {
return (0, horizonErrorBody_1.documentationSummaryFromHorizonBody)(body);
}
if (e instanceof Error) {
return e.message;
}
if (typeof e === 'string') {
return e;
}
return '';
}
const HORIZON_STATUS_404 = /status code 404/;
const HORIZON_TOO_MANY_REQUESTS = /too many requests/i;
const HORIZON_STATUS_4XX = /status code 4\d{2}/;
const HORIZON_STATUS_5XX = /status code 5\d{2}/;
const HORIZON_NETWORK_DOWN = /ECONNRESET|ECONNREFUSED|ENOTFOUND|EPIPE|ETIMEDOUT/;
const HORIZON_UNDEFINED_OBJECT = /undefined is not an object/;
function throwHorizonLedgerOrOperationsError(e, notFoundMessage) {
if (e instanceof stellar_sdk_1.NotFoundError) {
throw new Error(notFoundMessage);
}
const body = (0, horizonErrorBody_1.getHorizonErrorBody)(e);
const status = body?.status;
if (status === 404) {
throw new Error(notFoundMessage);
}
if (status === 429) {
throw new errors_1.LedgerAPI4xx('status code 4xx', { status: 429, url: undefined, method: 'GET' });
}
if (status !== undefined && status >= 400 && status < 500) {
throw new errors_1.LedgerAPI4xx();
}
if (status !== undefined && status >= 500 && status < 600) {
throw new errors_1.LedgerAPI5xx();
}
const errorMsg = messageFromHorizonUnknown(e);
if (HORIZON_STATUS_404.exec(errorMsg)) {
throw new Error(notFoundMessage);
}
if (HORIZON_TOO_MANY_REQUESTS.exec(errorMsg)) {
throw new errors_1.LedgerAPI4xx('status code 4xx', { status: 429, url: undefined, method: 'GET' });
}
if (HORIZON_STATUS_4XX.exec(errorMsg)) {
throw new errors_1.LedgerAPI4xx();
}
if (HORIZON_STATUS_5XX.exec(errorMsg)) {
throw new errors_1.LedgerAPI5xx();
}
if (e instanceof stellar_sdk_1.NetworkError ||
HORIZON_NETWORK_DOWN.exec(errorMsg) ||
HORIZON_UNDEFINED_OBJECT.exec(errorMsg)) {
throw new errors_1.NetworkDown();
}
throw e;
}
//# sourceMappingURL=horizonLedgerErrors.js.map