Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing status code in exceptions in browser #159

Closed
Shekesh opened this issue May 24, 2019 · 2 comments · Fixed by #327
Closed

missing status code in exceptions in browser #159

Shekesh opened this issue May 24, 2019 · 2 comments · Fixed by #327

Comments

@Shekesh
Copy link

Shekesh commented May 24, 2019

Hello @perry-mitchell,

when I use your library in the browser and an exception for like 404 or 401 is thrown, it's not the exception you're creating in

function handleResponseCode(response) {
const status = parseInt(response.status, 10);
let err;
if (status >= 400) {
err = new Error("Invalid response: " + status + " " + response.statusText);
err.status = status;
throw err;
}
return response;
}

It seems like axios is creating an error beforehand and throws that one.
As a result I don't get the status as a field which I need to have a clean error handling depending on the http response code. And I don't want to depend on parsing the returned error message where might or might not be the code included.

example

try {
	const client = createClient('http://localhost:1234/');
	await client.stat('whatever');
}
catch (error) {
	console.error(error);
}

result

Error: "Request failed with status code 404"
    createError /node_modules/axios/lib/core/createError.js:16
    settle /node_modules/axios/lib/core/settle.js:18
    handleLoad /node_modules/axios/lib/adapters/xhr.js:77

Catching that axios exception and handling it in the already existing handling function would be very helpful.

Thank you very much.

@perry-mitchell
Copy link
Owner

Good catch @Shekesh! Thanks for this 👍

@perry-mitchell
Copy link
Owner

This should be working now with fetch as the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants