Skip to content

Commit

Permalink
Fix ServerAction rejection reason (#63744)
Browse files Browse the repository at this point in the history
Recently the serverActionReducer was updated to no longer use React's
thenable type to carry resolution/rejection information. However the
rejection reason was not updated so now when a server action fails we
were rejecting with `undefined` rather than the rejected reason. This
change updates the reject to use the rejection value.


Closes NEXT-2943
  • Loading branch information
gnoff committed Mar 27, 2024
1 parent 2fc408d commit 7425d51
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function serverActionReducer(
},
(e: any) => {
// When the server action is rejected we don't update the state and instead call the reject handler of the promise.
reject(e.reason)
reject(e)

return state
}
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ createNextDescribe(
await check(() => browser.elementById('count').text(), '3')
})

it('should report errors with bad inputs correctly', async () => {
const browser = await next.browser('/error-handling', {
pushErrorAsConsoleLog: true,
})

await browser.elementByCss('#submit').click()

const logs = await browser.log()
expect(
logs.some((log) =>
log.message.includes(
'Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.'
)
)
).toBe(true)
})

it('should support headers and cookies', async () => {
const browser = await next.browser('/header')

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/actions/app/error-handling/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use server'

export async function action() {
return 'action result'
}
25 changes: 25 additions & 0 deletions test/e2e/app-dir/actions/app/error-handling/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client'

import { action } from './actions'

export default function Page() {
return (
<main>
<p>
This button will call a server action and pass something unserializable
like a class instance. We expect this action to error with a reasonable
message explaning what happened
</p>
<button
id="submit"
onClick={async () => {
await action(new Foo())
}}
>
Submit
</button>
</main>
)
}

class Foo {}

0 comments on commit 7425d51

Please sign in to comment.