Skip to content

useRoadizFetchFactory: Error response data lost when catching useRoadizFetchFactory request #454

Description

@timothejoubert

Description

When using useRoadizFetchFactory with $fetch.create, the error object caught in the component does not contain the full server response data.

Code example

// Form.vue
const fetch = useRoadizFetchFactory()
 await fetch(url, {
  method: 'POST',
  body: { action, formData },
}).then(() => {
    isSuccess.value = true
  })
  .catch((submitError: FetchError) => {
    isSuccess.value = false
    error.value = submitError // missing important response data here
  })
  .finally(() => {
    isPending.value = false
  })
// use-roadiz-fetch-factory.ts
export function useRoadizFetchFactory() {
  return $fetch.create({
    // ...
    onResponseError(context) {
      throw createError({
        statusCode: context.response.status,
        message: context.response.statusText,
      })
    },
    // ...
  })
}

Expected server response (example)

{
  "status": 400,
  "errors": "",
  "errorsPerForm": {
    "frc-captcha-response": {
      "frc-captcha-response": "Le Captcha est invalide, veuillez réessayer."
    }
  }
}

Actual response received in catch block

Only a generic FetchError with statusCode and message is available. The detailed server response (errors, errorsPerForm) is lost.

Steps to reproduce

Submit a form with invalid data (e.g., wrong captcha).

Catch the error in the component.

Observe that only statusCode and statusText are available, but not the full server response body.

Problem

Because onResponseError throws a simplified createError, the server’s detailed JSON payload is discarded. This makes it impossible to display form-specific validation messages in the frontend.

Suggestion

Don't use a custom onResponseError function or include context.response._data (or equivalent) when throwing the error in onResponseError, e.g.:

onResponseError(context) {
  throw createError({
    statusCode: context.response.status,
    message: context.response.statusText,
    data: context.response._data, // preserve server payload
  })
}

This way, the caught error will include the original server response (errors, errorsPerForm, etc.), making it accessible in the form component.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions