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

Include the response object when a request fails #21

Merged
merged 2 commits into from
Mar 17, 2023

Conversation

arcxyz
Copy link
Contributor

@arcxyz arcxyz commented Apr 8, 2022

Currently there is no way to access the response when a request fails so I have added it to the error object.

And you can use it in the same way as in "node-fetch":

const EleventyFetch = require("@11ty/eleventy-fetch");

module.exports = async function() {
	try {
		let url = "https://api.github.com/repos/11ty/eleventy";
		return EleventyFetch(url, {
			duration: "1d",
			type: "json"
		});
	} catch(error) {
		let errorBody = await error.response.text();
		console.error(`Error body: ${errorBody}`);
	}
};

Currently there is no way to access the response when a request fails so I have added it to the error object.
@aewshopping
Copy link

I would also find this helpful - got caught out trying to hunt down this elusive error in my project not long ago

@zachleat zachleat added this to the Eleventy Fetch v4.0.0 milestone Mar 17, 2023
@zachleat zachleat merged commit fa2c28e into 11ty:master Mar 17, 2023
@zachleat
Copy link
Member

Thank you!

@zachleat
Copy link
Member

Per the recommendation at #29 I did modify this slightly to use cause instead.

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

Successfully merging this pull request may close these issues.

3 participants