Skip to content

Commit

Permalink
Merge pull request #2895 from threefoldtech/development_show_better_e…
Browse files Browse the repository at this point in the history
…rrors

Add handling errors from client
  • Loading branch information
MohamedElmdary authored Jun 6, 2024
2 parents 382e944 + 8e77bd1 commit ccab3f0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/playground/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { InsufficientBalanceError } from "@threefold/types";

import type { NodeGPUCardType } from "../utils/filter_nodes";

export function downloadAsFile(name: string, data: string) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ccab3f0

Please sign in to comment.