Giter VIP home page Giter VIP logo

Comments (2)

sofisl avatar sofisl commented on June 21, 2024

Hi @TeemuKoivisto,

I was able to reproduce with a few tweaks on my end, since the code you pasted didn't completely run out of the gate (specifically, it doesn't seem to instantiate the Drive instance anywhere). However, I'm having trouble understanding the bug. It seems to me that if you are requesting for the reply to come back to you in 'arraybuffer', your error would also be in that format. Seems like we could be subverting people's expectations by always returning it as a string. Could you share a little bit of why you think it should be a string, despite explicitly requesting otherwise? Perhaps if there's other patterns that other code follows, that might be worth looking into!

Thanks!

from gaxios.

TeemuKoivisto avatar TeemuKoivisto commented on June 21, 2024

Hi and thanks for the reply! Yeah, sorry about the example - just hacked it together. I guess the problem here lies with the thrown error. Since its contents are in binary as well, it's rather impossible to figure out what went wrong. Here's what I ended up doing:

import { GaxiosError, GaxiosPromise } from "gaxios";

import { Result } from "@focal/types/result";

function sleep(ms: number): Promise<boolean> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, ms);
  });
}

/**
 * Wrap Gaxios promises in order to not to throw errors and instead return handled Result types
 * @param promise
 * @returns
 */
export async function wrapGaxios<T>(
  promise: GaxiosPromise<T>,
  timeout = 10000,
): Promise<Result<T>> {
  try {
    const res = await Promise.race([promise, sleep(timeout)]);
    if (typeof res === "boolean") {
      return { ok: false, err: "Gaxios request timeout", errCode: 400 };
    } else if (res.status >= 400) {
      return {
        ok: false,
        err: ((res.data as any) || "").toString(),
        errCode: res.status,
      };
    }
    return { ok: true, data: res.data };
  } catch (err: any) {
    if (err instanceof GaxiosError) {
      // When "responseType" is "arraybuffer" like in getFile.ts, the error message is also an array buffer
      const message = err.message as string | ArrayBuffer;
      const parsedBuffer =
        message instanceof ArrayBuffer &&
        Buffer.from(message).toString("utf-8");
      const errJson = parsedBuffer && JSON.parse(parsedBuffer);
      const parsedMsg = typeof errJson === "object" && errJson?.error?.message;
      const errCode = parseInt(err.code || "500");
      return { ok: false, err: parsedMsg || message, errCode };
    }
    return { ok: false, err, errCode: err?.status || 500 };
  }
}

I added a manual timeout since Gaxios didn't seem to do it itself. However, as this code became rather messy I actually ended up just using fetch as it's now native in NodeJS v18 and I reuse the code in client as well.

But in conclusion, IMO it would be better if you could just parse the error when the response type is array buffer automatically. Just makes life a lot easier.

from gaxios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.