ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

How to handle errors body

Open MatteoGauthier opened this issue 1 year ago • 1 comments

Hey ! I wanted to get the error response body in json, but the response.json() method give me the following error :

error: Body already used

I'm using onResponseError to display a log on error

  const response = await ofetch(documentUrl, {
    method: "POST",
    body: body,
    async onResponseError({ request, response, options }) {
      // Log error
      console.log(
        "[fetch response error]",
        request,
        response.status,
        response.body,
        response.json() // Error here
      )
    },
  })

MatteoGauthier avatar May 22 '24 09:05 MatteoGauthier

Ah, I think it might be in response._data.

Smef avatar Jun 02 '24 01:06 Smef

You can retrieve the parsed data using response._data.

kricsleo avatar Mar 28 '25 11:03 kricsleo