Issue 1:
This is more of a FYI kind of issue.

Is your feature request related to a problem? Please describe.

When making a request via Axios, if an error is caught and logged as-is, it will contain the entire request object which contains headers which can include credentials such as API keys.

If someone isn't aware of this and just logs the entire error object or error.toJSON() without sanitising it, they can leak sensitive data and credentials.

Describe the solution you'd like

A config option to not append the request object to thrown errors and keep only the standard error properties from the spec https://nodejs.org/api/errors.html#errors_class_error

Describe alternatives you've considered

I've worked around this by creating a new error object and only copying the standard error properties and the response status to the new error and throwing that instead.

Issue 2:
Is your feature request related to a problem? Please describe.

A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Errors thrown by axios have the full request body (and response body if a response was received before the error was thrown). The shape of errors thrown by axios is shown here: https://github.com/axios/axios/blob/master/index.d.ts#L82

There's no doubt that having this information can be incredibly useful. Especially if using interceptors to do retries or otherwise respond to errors, having access to the original request is incredibly useful.

Unfortunately, this also means that if the original request contained sensitive content, that sensitive content is now on the error being thrown / rejected. This puts the onus on the developer to remember to properly cleanse the error before allowing it to be thrown or logged. And this certainly doesn't always happen. I cringe a little bit thinking about all the usernames / passwords that are being logged to browser consoles around the world when login requests fail, for example.

Describe the solution you'd like

A clear and concise description of what you want to happen.

I'm a big believer in doing the safe thing by default, and allowing that behavior to be overridden intentionally by developers if so desired. What I would prefer here is that axios automatically cleanse the error of the request and response content by default, but allow the developer to pass in a flag to override this behavior.

One possible way this could be achieved is by adding a final response interceptor at the end of any user defined interceptors, that essentially just looks like so

ax.interceptors.response.use(
(response) => response,
(error) => Promise.reject(new Error(error.message))
);

This interceptor could be disabled by the configuration flag.

I believe this would allow developers to utilize the full error object as it is today in any interceptors they define, but would prevent the final error from containing any sensitive content by default.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

The proposed solution is a breaking change. As an alternative, we could keep the existing behavior as the default, and allow a flag to enable the "clean" errors functionality described above. This would at least allow developers an easy way to make sure any errors are sensitive content free. But it still requires the developer to know about the flag and set it to be safe.

Additional context

Add any other context or screenshots about the feature request here.