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

Allow Custom Error Types in createQueryHook for Enhanced Error Handling #2136

Open
1 task done
SSlime-s opened this issue Feb 4, 2025 · 1 comment · May be fixed by #2147
Open
1 task done

Allow Custom Error Types in createQueryHook for Enhanced Error Handling #2136

SSlime-s opened this issue Feb 4, 2025 · 1 comment · May be fixed by #2147
Labels
enhancement New feature or request swr-openapi Relevant to swr-openapi library

Comments

@SSlime-s
Copy link

SSlime-s commented Feb 4, 2025

Description

Currently, createQueryHook only exposes errors defined in the OpenAPI schema. This means that if the API returns an error defined in the schema (e.g., { message: "Invalid status" }), the error property will be typed accordingly.
However, common errors like network errors thrown by the standard fetch API result in a TypeError, which createQueryHook doesn't expose directly. This discrepancy with useSWR, which would normally capture such errors, can lead to unexpected behavior.

For example:

const { data, error } = useQuery("/pet/findByStatus");
//            ^?: { message: "Invalid status" } | undefined
//                but If a network error occurs: TypeError

This limitation makes it difficult to handle different error scenarios and maintain type safety, especially when dealing with errors not defined in the schema.

Proposal

Add a FetcherError type parameter to createQueryBaseHook:

createQueryBaseHook<Paths extends {}, IMediaType extends MediaType, Prefix extends string, FetcherError = never>

This allows users to specify the error type returned by the fetcher:

createQueryHook<paths, `${string}/${string}`, "<unique-key>", Error>(client, "<unique-key>")

Resulting in:

const { data, error } = useQuery("/pet/findByStatus");
//            ^?: { message: "Invalid status" } | Error | undefined

This is also beneficial for those using custom fetchers or middleware, allowing them to type their custom errors.

Extra

@SSlime-s SSlime-s added enhancement New feature or request swr-openapi Relevant to swr-openapi library labels Feb 4, 2025
@su8ru
Copy link

su8ru commented Feb 8, 2025

This is necessary!
SWR introduces a way to add status code and other information to the error returned by the fetcher as an error handling technique, but currently createQueryHook does not allow this :(

const fetcher = async url => {
  const res = await fetch(url)
 
  // If the status code is not in the range 200-299,
  // we still try to parse and throw it.
  if (!res.ok) {
    const error = new Error('An error occurred while fetching the data.')
    // Attach extra info to the error object.
    error.info = await res.json()
    error.status = res.status
    throw error
  }
 
  return res.json()
}

https://swr.vercel.app/docs/error-handling#status-code-and-error-object

@SSlime-s SSlime-s linked a pull request Feb 9, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request swr-openapi Relevant to swr-openapi library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants