Radish alpha
r
rad:z4D5UCArafTzTQpDZNQRuqswh3ury
Radicle desktop app
Radicle
Git
Wrap invoke rejection reasons in Error instance
Open did:key:z6Mki9XN...FvWF opened 10 months ago

This way we get stack traces and more informative console logs (instead of “[Object object]”).

1 file changed +25 -2 30710d51 a3295dcb
modified src/lib/invoke.ts
@@ -14,6 +14,18 @@ export async function invoke<T = null>(
  return withTestBackend<T>(tauri.invoke, cmd, args, options);
}

+
/**
+
 * Invoking a Tauri command returned an error with the given message
+
 */
+
class InvokeError extends Error {
+
  name = "InvokeError";
+

+
  constructor(message: string) {
+
    super(message);
+
    Error.captureStackTrace?.(this, InvokeError);
+
  }
+
}
+

async function withTestBackend<T>(
  fn: (
    cmd: string,
@@ -25,7 +37,14 @@ async function withTestBackend<T>(
  options?: tauri.InvokeOptions,
) {
  if (window.__TAURI_INTERNALS__) {
-
    return fn(cmd, args, options);
+
    // 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));
+
      } else {
+
        throw new InvokeError(`Invalid error object: ${error}`);
+
      }
+
    });
  } else {
    return fetch(`http://127.0.0.1:8081/${cmd}`, {
      method: "POST",
@@ -38,7 +57,11 @@ async function withTestBackend<T>(
      }
      const json = await response.json();
      if (!response.ok) {
-
        throw json;
+
        if (typeof json === "object" && json !== null && "message" in json) {
+
          throw new InvokeError(String(json.message));
+
        } else {
+
          throw new InvokeError(`Invalid error object: ${json}`);
+
        }
      }

      return json;