hg-pyun / axios-logger

Beautify Axios Logging Messages
MIT License
173 stars 33 forks source link

How to log error request body? #142

Closed isaachinman closed 1 year ago

isaachinman commented 1 year ago

We are using axios-logger just for error logging in our API, eg:

instance.interceptors.response.use(res => res, errorLogger)

This is working great, however when we receive 4xx responses from some third parties, relevant details are not passed in the error response. The third party might send error/validation content, but not actually reference the entities involved.

Because of this, we want to be able to also log the request body when a 4xx/5xx error is returned.

I haven't been able to work out how this is possible. Any ideas?

hg-pyun commented 1 year ago

https://github.com/hg-pyun/axios-logger#error

If you want to logging error, pass logging function second argument.

isaachinman commented 1 year ago

Thanks @hg-pyun. Not sure if you understood the question.

Regardless, if anyone comes across this issue in the future, here's how it can be done:

instance.interceptors.response.use(
  res => res,
  error => errorLogger({
    ...error,
    response: {
      ...error.response,
      data: {
        ...error.response.data,
        _request: error.config.data,
      },
    },
  })
)