Skip to content

Commit

Permalink
feat: correct error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoDex committed Sep 2, 2024
1 parent af0e362 commit 629318a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@compolabs/spark-orderbook-ts-sdk",
"version": "1.6.5",
"version": "1.6.6",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
69 changes: 32 additions & 37 deletions src/utils/getHumanReadableError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,50 @@ import {
ValueErrorInput,
} from "src/types/market/MarketContractAbi";

type EnsureArray<T> = T extends any[] ? T : [T];

type HumanReadableMessages = {
[K in keyof AccountErrorInput]: (args: AccountErrorInput[K]) => string;
[K in keyof AccountErrorInput]: string;
} & {
[K in keyof Required<OrderErrorInput>]: (
args: EnsureArray<OrderErrorInput[K]>,
) => string;
[K in keyof Required<OrderErrorInput>]: string;
} & {
[K in keyof Required<MatchErrorInput>]: (
args: EnsureArray<MatchErrorInput[K]>,
) => string;
[K in keyof Required<MatchErrorInput>]: string;
} & {
[K in keyof typeof AssetErrorInput]: () => string;
[K in keyof typeof AssetErrorInput]: string;
} & {
[K in keyof typeof AuthErrorInput]: () => string;
[K in keyof typeof AuthErrorInput]: string;
} & {
[K in keyof Required<ValueErrorInput>]: (
args: EnsureArray<ValueErrorInput[K]>,
) => string;
[K in keyof Required<ValueErrorInput>]: string;
};

const humanReadableMessages: HumanReadableMessages = {
InsufficientBalance: () => `Insufficient balance`,
InvalidAsset: () => `The asset provided is invalid`,
InvalidFeeAsset: () => `The fee asset provided is invalid`,
Unauthorized: () => `You are not authorized to perform this action`,
CantMatch: () => `Can't match`,
CantMatchMany: () => `Cannot match many`,
CantFulfillMany: () => `Cannot fulfill many`,
OrderNotFound: (args) => `Order with ID ${args[0]} was not found`,
PriceTooSmall: () => `The price is too small`,
ZeroOrderAmount: () => `Order amount cannot be zero`,
ZeroLockAmount: () => `Lock amount cannot be zero`,
FailedToRemove: (args) => `Failed to remove order with ID ${args[0]}`,
InvalidAmount: () => `Invalid amount provided`,
InvalidSlippage: () => `Invalid slippage provided`,
InvalidArrayLength: () => `Invalid array length`,
InvalidFeeAmount: () => `Fee is too low`,
InsufficientBalance: "Insufficient balance",
InvalidAsset: "The asset provided is invalid",
InvalidFeeAsset: "The fee asset provided is invalid",
Unauthorized: "You are not authorized to perform this action",
CantMatch: "Can't match",
CantMatchMany: "Cannot match many",
CantFulfillMany: "Cannot fulfill many",
OrderNotFound: "Order was not found",
PriceTooSmall: "The price is too small",
ZeroOrderAmount: "Order amount cannot be zero",
ZeroLockAmount: "Lock amount cannot be zero",
FailedToRemove: "Failed to remove order",
InvalidAmount: "Invalid amount provided",
InvalidSlippage: "Invalid slippage provided",
InvalidArrayLength: "Invalid array length",
InvalidFeeAmount: "Fee is too low",
};

const humanReadableMessagesKeys = Object.keys(humanReadableMessages);

export const getHumanReadableError = <K extends keyof HumanReadableMessages>(
error: Record<K, any[]>,
error: string[],
): string => {
const errorType = Object.keys(error)[0] as K;
const args = error[errorType] ?? [];
const msgFn = humanReadableMessages[errorType]! as unknown as (
args: any[],
) => string;
const errorType = error.find((k) => {
if (typeof k === "object") return;
return humanReadableMessagesKeys.includes(k);
}) as K;

if (!errorType) return "";

return msgFn(args) ?? "";
return humanReadableMessages[errorType];
};

0 comments on commit 629318a

Please sign in to comment.