From 8e77bd139565a8b992c54283b44ab875267a3241 Mon Sep 17 00:00:00 2001 From: MohamedElmdary Date: Thu, 6 Jun 2024 13:52:13 +0300 Subject: [PATCH] feat: add handling errors from client --- packages/playground/src/utils/helpers.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/playground/src/utils/helpers.ts b/packages/playground/src/utils/helpers.ts index b44d036627..eb548e70e4 100644 --- a/packages/playground/src/utils/helpers.ts +++ b/packages/playground/src/utils/helpers.ts @@ -1,3 +1,5 @@ +import { InsufficientBalanceError } from "@threefold/types"; + import type { NodeGPUCardType } from "../utils/filter_nodes"; export function downloadAsFile(name: string, data: string) { @@ -25,7 +27,19 @@ export function downloadAsJson(data: object, filename: string) { a.remove(); } +/* prettier-ignore */ +const HANDLED_ERRORS = [ + [InsufficientBalanceError, "Failed to apply Transaction due to account balance is too low."], + /** @Usage [Constructor, "messageError"] */ +] as const; + export function normalizeError(error: any, fallbackError: string): string { + for (const [Ctor, msg] of HANDLED_ERRORS) { + if (error instanceof Ctor) { + return msg; + } + } + return typeof error === "string" ? error : error && typeof error === "object" && "message" in error && typeof error.message === "string"