@kinecosystem/kin-sdk-js-common
Version:
Kin sdk js common package for web and nodejs
160 lines (145 loc) • 5.85 kB
text/typescript
import * as Error from "../../scripts/src/errors";
import {
AccountExistsError,
AccountNotActivatedError,
AccountNotFoundError,
BadRequestError,
InternalError,
LowBalanceError,
NetworkError,
ResourceNotFoundError,
ServerError
} from "../../scripts/src/errors";
import each from "jest-each";
import {
ChangeTrustResultCode,
CreateAccountResultCode,
HorizonErrorList,
OperationResultCode,
PaymentResultCode,
TransactionErrorList
} from "../../scripts/src/blockchain/errors";
describe("Error", async () => {
beforeAll(async () => {
});
test("Horizon error", async () => {
const obj = {
response: {
"type": "https://stellar.org/horizon-errors/not_acceptable",
"title": "An acceptable response content-type could not be provided for this request",
"status": 406
}
};
expect(Error.ErrorDecoder.translate(obj)).toEqual(new BadRequestError(obj.response));
});
test("Network error", async () => {
const obj = {};
expect(Error.ErrorDecoder.translate(obj)).toEqual(new NetworkError(obj));
});
each([[HorizonErrorList.RATE_LIMIT_EXCEEDED, ServerError],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]]).test(
'returns the result of adding %d to %d',
(errorName, expected) => {
let res = new expected(horizonResponse(errorName).response);
expect(Error.ErrorDecoder.translate(horizonResponse(errorName))).toEqual(res);
},
);
function horizonResponse(horizon: string) {
const response = {
response: {
"type": `https://stellar.org/horizon-errors/${horizon}`,
"title": "An acceptable response content-type could not be provided for this request",
"status": 406
}
};
return response;
}
each([[TransactionErrorList.TOO_EARLY, BadRequestError],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]]).test(
'returns the result of adding %d to %d',
(errorName, expected) => {
let res = new expected(translateResponse(errorName).response);
expect(Error.ErrorDecoder.translate(translateResponse(errorName))).toEqual(res);
},
);
function translateResponse(transaction: string) {
const response = {
response: {
"type": "https://stellar.org/horizon-errors/transaction_failed",
"title": "Transaction Failed",
"status": 400,
"detail": "The transaction failed when submitted to the stellar network. The `extras.result_codes` field on this response contains further details. " +
"Descriptions of each code can be found at: https://www.stellar.org/developers/learn/concepts/list-of-operations.html",
"extras": {
"result_codes": {
"transaction": `${transaction}`
},
"result_xdr": "AAAAAAAAAAD////7AAAAAA=="
}
}
};
return response;
}
each([[OperationResultCode.BAD_AUTH, BadRequestError],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]]).test(
'returns the result of adding %d to %d',
(errorName, expected) => {
let res = new expected(operationsRespons(errorName).response);
expect(Error.ErrorDecoder.translate(operationsRespons(errorName))).toEqual(res);
},
);
function operationsRespons(operation: string) {
const response = {
response: {
"type": "https://stellar.org/horizon-errors/transaction_failed",
"title": "Transaction Failed",
"status": 400,
"detail": "The transaction failed when submitted to the stellar network. The `extras.result_codes` field on this response contains further details. " +
"Descriptions of each code can be found at: https://www.stellar.org/developers/learn/concepts/list-of-operations.html",
"extras": {
"envelope_xdr": "AAAAAK5k1V/ttpM83eWox5KIC46wnDnf+v4pZiBv3CtdRS0iAAAAZAAb9NgAAAABAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAABAAAAAEL0mlpzYxg56N51" +
"pY7zL6BmsgMKVeNO3iAB8gSay3w2AAAAAAADjX6kxPlgAAAAAAAAAAFdRS0iAAAAQBnMtjR93bD8bSNJxwBun30PCT+bm3X635e3rDiHufA0bd5q7hCsT+WIbH8iTVwuAp50fdRW4tPJgBdzQ9VZQgs=",
"result_codes": {
"transaction": "tx_failed",
"operations": [
`${operation}`
]
},
"result_xdr": "AAAAAAAAAGT/////AAAAAQAAAAAAAAAB////+wAAAAA="
}
}
};
return response;
}
});