ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

.catch((err) => { console.log(err.data);}) not working

Open hoanghiep1x0 opened this issue 2 years ago • 3 comments

Describe the feature

          const url = "http://localhost:3000/authenticate"
           const payload = {
             email: credentials.email,
             password: credentials.password,
           };

           const result = await ofetch(url, {
             ignoreResponseError: true,
             method: "POST",
             body: payload,
             headers: {
               "Content-Type": "application/json",
               "Accept-Language": "en-US",
             }
           }).catch((err) => {
             console.log(err.data);
           })

console.log(err.data); == undefined

// log(err) return

FetchError: [POST] "http://localhost:3000/authenticate": fetch failed at process.processTicksAndRejections

.catch((err) => { console.log(err.data);}) not working

Additional information

  • [ ] Would you be willing to help implement this feature?

hoanghiep1x0 avatar Oct 30 '23 09:10 hoanghiep1x0

Hi, Would you be able to provide a reproduction 🙏🏻

peterroe avatar Oct 30 '23 17:10 peterroe

The above command is just that. I call the authentication api using post method. I set the api server to not enabled and I can't get an error with http status code 500. I am testing to detect different errors with http status using POST method ​

hoanghiep1x0 avatar Oct 31 '23 01:10 hoanghiep1x0

How about use try/catch

try {
	const result = await ofetch(url, {
		ignoreResponseError: true,
		method: "POST",
		body: payload,
		headers: {
			"Content-Type": "application/json",
			"Accept-Language": "en-US",
		}
	 })
} catch((err) => {
	console.log(err);
})

peterroe avatar Oct 31 '23 10:10 peterroe