Comments (2)
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.
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)
- Your .repo-metadata.json file has a problem 🤒
- Your .repo-metadata.json file has a problem 🤒
- Ensuring compliance with SNI Extension requirement on remote host HOT 3
- Support proxy option instead of HTTP_PROXY env variable HOT 4
- Get request with one-item array param, will send the value inside instead the the array HOT 1
- Feature request: optional error throwing HOT 2
- 📦 pack and install: "before all" hook: pack and install for "should run the sample" failed HOT 4
- can i please for more docs about comparison with axios HOT 1
- Uncaught ReferenceError: process is not defined HOT 1
- content-type parsing is performed even if a `text` response type is explicitly requested, causing an unmanageable error HOT 3
- timeout - seconds or milliseconds HOT 1
- feat!: Use Native `cause` in `GaxiosError`
- Using googleapis for direct CSV downloads does not return CSV
- Typo on README.md
- feat: Updated `timeout` Implementation and Documentation HOT 1
- feat: Add `client_secret` to `defaultErrorRedactor` HOT 2
- Warning: a recent release failed HOT 1
- Warning: a recent release failed HOT 1
- Warning: a recent release failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gaxios.