Skip to content

useFetch should reject with an Error object #113

Closed
@artdent

Description

@artdent

Right now, if there's a request error, parseResponse from useFetch rejects with the response object instead of with an Error object. This causes two difficulties:

  • rejecting with a non-Error means you don't have a full stack trace
  • it contradicts the AsyncState type, so if you try to use state.error in TypeScript you have to cast it through unknown, i.e. ((error as unknown) as Response). (The intermediate step is necessary because Error and Response are unrelated types.)

The best fix is probably to change useFetch to throw an Error object that has a handle to the underlying response, which is unfortunately a backward-incompatible change. The other possibility I can think of is to widen or parameterize the error type, but I'm not sure it's a good idea to make the signature more complicated to support something that's not a great practice (i.e. throwing non-errors).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions