Radish alpha
r
rad:z4D5UCArafTzTQpDZNQRuqswh3ury
Radicle desktop app
Radicle
Git
Provide `error.code` to `InvokeError`
Open did:key:z6MkkfM3...sVz5 opened 9 months ago

We decide during onboarding to display different things based on the error code received from the backend.

Due to the new InvokeError type we lost this error.code string.

1 file changed +5 -3 b65b6daa 0336da38
modified src/lib/invoke.ts
@@ -20,9 +20,11 @@ export async function invoke<T = null>(
 */
class InvokeError extends Error {
  name = "InvokeError";
+
  code: string;

-
  constructor(message: string) {
+
  constructor(message: string, code = "Unknown Error") {
    super(message);
+
    this.code = code;
    Error.captureStackTrace?.(this, InvokeError);
  }
}
@@ -41,7 +43,7 @@ async function withTestBackend<T>(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return fn(cmd, args, options).catch((error: any) => {
      if (typeof error === "object" && error !== null && "message" in error) {
-
        throw new InvokeError(String(error.message));
+
        throw new InvokeError(String(error.message), String(error.code));
      } else {
        throw new InvokeError(`Invalid error object: ${error}`);
      }
@@ -59,7 +61,7 @@ async function withTestBackend<T>(
      const json = await response.json();
      if (!response.ok) {
        if (typeof json === "object" && json !== null && "message" in json) {
-
          throw new InvokeError(String(json.message));
+
          throw new InvokeError(String(json.message), String(json.code));
        } else {
          throw new InvokeError(`Invalid error object: ${json}`);
        }