js-api-client
js-api-client copied to clipboard
Request: More descriptive errors when a failure occurs
Description
Should an error occur (including but not limited to a networking timeout), the client throws a pretty generic error that makes troubleshooting pretty difficult.
See https://github.com/Typeform/js-api-client/blob/master/src/create-client.ts#L39.
Context
I recently worked on a project where I was hitting this error case, and it was very difficult to pinpoint the cause of the issue. Having a more descriptive error would have been really helpful.
Possible Solution
Handle all the axios error cases and form more descriptive errors - something along the lines of the following:
.catch((error: any) => {
if (error && error.response && error.response.data && error.response.data.description) {
throw new Error(`Internal error - ${error.response.data.description}`)
} else if (error.response) {
const resp = error.response
throw new Error(`"status": "${resp.status}", "data": "${resp.data}"`)
} else if (error.request) {
const url = error.config.url
const httpSummary = {
message: error.message,
errno: error.errorSummary,
code: error.code,
syscall: error.syscall,
address: error.address,
port: error.port,
url: url.substr(0, url.indexOf('?')) // Don't log query params
}
throw new Error(JSON.stringify(httpSummary))
} else {
throw new Error(`Internal error - request set up error: ${error.message}`)
}
})
Knowing the specific http request/status causing the error would be really helpful.
Hi Greg 👋 This is indeed great feedback!
Apologies for the confusion on the error handling. Would you be interested to submit a PR for it?
We meet again! I don't foresee finding time at least in the near future to make a "production-ready" change. Have some other stuff on my plate. Hopefully that code snippet can be a good starting point for somebody.
Hello @loeschg @picsoung 👋 Do you think it would be helpful if we introduced a "log level" when calling createClient() and log those errors directly into console instead of building a complex string for error message?
it's been a minute since this has come up and that project has long since passed.
If I recall, we were using this as a part of a backend node service which had its own logging system/conventions separate from console logging. So we would have wanted more explicit control than just console logging.
Apologies for the delay. Me and my team have taken ownership of the repo only recently and would like to keep it maintained again 😄
Would you prefer to pass your own logger function into createClient that could be used to log details of the error instead of console.log?
I think something like that would be useful! Anything that provides some additional transparency of error types for adjustment of use, bug reports, etc.
I'm not an active user of the service anymore, so take all of my suggestions with a grain of salt. Regardless, thanks for making improvements and listening to customers!
:tada: This issue has been resolved in version 1.12.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: