preserve stacktrace on error, throw HavenoError

This commit is contained in:
woodser 2022-05-31 15:45:17 -04:00
parent 7c630b82cd
commit f9bbee8726
4 changed files with 756 additions and 493 deletions

View File

@ -2,8 +2,8 @@
// import haveno types
import HavenoClient from "./HavenoClient";
import HavenoError from "./utils/HavenoError";
import HavenoUtils from "./utils/HavenoUtils";
import * as grpcWeb from "grpc-web";
import { MarketPriceInfo, NotificationMessage, OfferInfo, TradeInfo, UrlConnection, XmrBalanceInfo } from "./protobuf/grpc_pb"; // TODO (woodser): better names; haveno_grpc_pb, haveno_pb
import { Attachment, DisputeResult, PaymentMethod, PaymentAccount, MoneroNodeSettings } from "./protobuf/pb_pb";
import { XmrDestination, XmrTx, XmrIncomingTransfer, XmrOutgoingTransfer } from "./protobuf/grpc_pb";
@ -1492,7 +1492,7 @@ test("Cannot make or take offer with insufficient unlocked funds", async () => {
await postOffer(charlie, {paymentAccountId: paymentAccount.getId()});
throw new Error("Should have failed making offer with insufficient funds")
} catch (err: any) {
const errTyped = err as grpcWeb.RpcError;
const errTyped = err as HavenoError;
assert.equal(errTyped.code, 2);
assert(err.message.includes("not enough money"), "Unexpected error: " + err.message);
}
@ -1514,7 +1514,7 @@ test("Cannot make or take offer with insufficient unlocked funds", async () => {
await charlie.takeOffer(offer.getId(), paymentAccount.getId());
throw new Error("Should have failed taking offer with insufficient funds")
} catch (err: any) {
const errTyped = err as grpcWeb.RpcError;
const errTyped = err as HavenoError;
assert(errTyped.message.includes("not enough money"), "Unexpected error: " + errTyped.message);
assert.equal(errTyped.code, 2);
}
@ -1523,7 +1523,7 @@ test("Cannot make or take offer with insufficient unlocked funds", async () => {
try {
await charlie.getTrade(offer.getId());
} catch (err: any) {
const errTyped = err as grpcWeb.RpcError;
const errTyped = err as HavenoError;
assert.equal(errTyped.code, 3);
assert(errTyped.message.includes("trade with id '" + offer.getId() + "' not found"));
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,9 @@
import HavenoClient from "./HavenoClient";
import HavenoError from "./utils/HavenoError";
import HavenoUtils from "./utils/HavenoUtils";
export { HavenoClient };
export { HavenoError };
export { HavenoUtils };
export * from "./protobuf/grpc_pb";
export * from "./protobuf/pb_pb";

18
src/utils/HavenoError.ts Normal file
View File

@ -0,0 +1,18 @@
/**
* Haveno error with message and code.
*/
export default class HavenoError extends Error {
code: number | undefined;
/**
* Create the error with a message and code.
*
* @param {string} msg - the error message
* @param {number} code
*/
constructor(msg: string, code?: number) {
super(msg);
this.code = code;
}
}