js-api-client icon indicating copy to clipboard operation
js-api-client copied to clipboard

Request: More descriptive errors when a failure occurs

Open loeschg opened this issue 5 years ago • 2 comments

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.

loeschg avatar Apr 28 '20 16:04 loeschg

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?

picsoung avatar Apr 28 '20 20:04 picsoung

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.

loeschg avatar May 01 '20 19:05 loeschg

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?

mathio avatar Oct 05 '23 15:10 mathio

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.

loeschg avatar Oct 05 '23 16:10 loeschg

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?

mathio avatar Oct 09 '23 14:10 mathio

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!

loeschg avatar Oct 09 '23 15:10 loeschg

:tada: This issue has been resolved in version 1.12.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

typeform-ops-gha avatar Oct 10 '23 11:10 typeform-ops-gha