Skip to content

Throwing 400 in Client Action Throws as 500 #12919

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

Closed
kevinbailey25 opened this issue Jan 30, 2025 · 5 comments
Closed

Throwing 400 in Client Action Throws as 500 #12919

kevinbailey25 opened this issue Jan 30, 2025 · 5 comments

Comments

@kevinbailey25
Copy link

I'm using React Router as a...

framework

Reproduction

Go to https://stackblitz.com/edit/github-ff4pz9xc?file=README.md

I have 4 routes, loader, action, client-loader and client-action.

Each of these do the same thing:

throw data({ wow: true }, { status: 400 })

(exception: the action routes have a form with a button to trigger the action)

I've added a console.log(error) in the error boundary.

View each error in the console, you'll note that all of them are 400 errors, except the clientAction is a 500 error, where the data is the full DataWithResponseInit<{ wow: boolean; }> instead of just { wow: boolean }

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700K
    Memory: 18.97 GB / 31.75 GB
  Binaries:
    Node: 22.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 11.0.0 - C:\Program Files\nodejs\npm.CMD
    bun: 1.1.42 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (131.0.2903.86)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @react-router/dev: ^7.1.4 => 7.1.4
    @react-router/node: ^7.1.4 => 7.1.4
    @react-router/serve: ^7.1.4 => 7.1.4
    react-router: ^7.1.4 => 7.1.4
    vite: ^5.4.11 => 5.4.14

Used Package Manager

npm

Expected Behavior

Throwing a 400 error in a clientAction should be a 400 error, and not a 500 error that wraps my 400 error.

throw data({ wow: true}, { status: 400 })

Should function the same in clientAction as it does in loader, action, and clientLoader

Actual Behavior

The clientAction throws a ErrorResponseImpl with the status of 500, where it's data prop is my thrown data({ wow: true }, { status: 400 }) call

Image

@luisdlopez
Copy link

luisdlopez commented Feb 4, 2025

On a similar note, when a clientLoader returns a call to data() (instead of throwing), like so:

return data({ wow: true }, { status: 400 })

The data returned will be similar to what @kevinbailey25 describes. So, when getting the data inside the component, using useFetcher, we need to go an additional level:

const fetcher = useFetcher<typeof clientAction>(); 
const wow = fetcher.data.data.wow

A big issue, also, is that the typescript definition is wrong, saying we only need to write fetcher.data.wow, which is not the case.

@timdorr
Copy link
Member

timdorr commented Feb 4, 2025

Simple fix: Swap out data for Response.json. I'm showing the correct status code that way.

@timdorr timdorr closed this as completed Feb 4, 2025
@kevinbailey25
Copy link
Author

Simple fix: Swap out data for Response.json. I'm showing the correct status code that way.

Why does data work for clientLoader, loader, action... but doesn't work for clientAction. This is still a bug that needs to be addressed. A work around shouldn't close this ticket.

@kevinbailey25
Copy link
Author

@timdorr the documentation tells you to throw data()

https://reactrouter.com/how-to/error-boundary#3-throw-data-in-loadersactions

I get that there's a work around, but this is still an issue and should be fixed, or documentation should be changed.

@brophdawg11
Copy link
Contributor

Fixed in #13522, will be available in the next release

@brophdawg11 brophdawg11 added the awaiting release This issue has been fixed and will be released soon label May 1, 2025
@brophdawg11 brophdawg11 removed their assignment May 1, 2025
@brophdawg11 brophdawg11 removed the awaiting release This issue has been fixed and will be released soon label May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants