Feature request: optional error throwing #523
Labels
priority: p3
Desirable enhancement or fix. May not be included in next release.
type: feature request
‘Nice-to-have’ improvement, new feature or different behavior or design.
Hi, I recently came accustomed to using
Gaxios
throughgoogleapis
(specificallydrive_v3
). Seems all Promises are wrapped as GaxiosPromises which is okay but I have a minor nitpick to make.While it's the norm to throw errors in JS I think it's mostly bad practise as this will often just crash the app and you can't type the errors as you could with normal returns.
I myself have been using this utility type:
to wrap all my error-producing code (mostly asynchronous) to gain full type-safety and proper error messages - akin to Go. I can then use them as such:
Now, I'm not suggesting you have to follow this schema (I used to use
ok: boolean
to check for errors but scrapped it once I saw'data' in resp
works as well). But since you are wrapping Promises with your own type which includes both, success and error types, I'd love to be able to use those directly instead of catching the error, typing it manually and then doing my own thing on top of it causing just a lot boilerplate.Ideally, there was a parameter / wrapper / utility method to allow returning an optional type which is either
GaxiosResponse<T> | GaxiosError
. Then I could myself check that for errors and not worry about typings and such.What urged me to write this in the first place was noticing passing expired credentials caused Gaxios to throw an error while an invalid request would instead return normally (although with non 200 code). Which is just inconvenient as hell as I have to do both, wrap the request with try-catch and check the response for non 200 status.
EDIT: Also I must say GaxiosError is pretty awkward to use. Why the stack is not serialized into a regular JSON object? I'm now manually slicing the error message from it.
The text was updated successfully, but these errors were encountered: