Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/content/docs/server-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const ping = os
We recommend using [Runtime Context](/docs/context#execution-context) instead of [Initial Context](/docs/context#initial-context) when working with Server Actions.
:::

:::warning
Special errors such as `redirect`, `notFound`, and similar are **only supported in [Next.js](https://nextjs.org/) and [TanStack Start](https://tanstack.com/start/latest)** at the moment.
:::

## Client Side

On the client, import and call your procedure as follows:
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"cookie": "^1.0.2"
},
"devDependencies": {
"@tanstack/router-core": "^1.132.19",
"@types/ws": "^8.18.1",
"crossws": "^0.4.1",
"next": "^15.5.4",
Expand Down
27 changes: 19 additions & 8 deletions packages/server/src/procedure-action.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ORPCError } from '@orpc/client'
import { forbidden, notFound, redirect, unauthorized } from 'next/navigation'
import * as tanstackRouter from '@tanstack/router-core'
import * as next from 'next/navigation'
import { createActionableClient } from './procedure-action'

describe('createActionableClient', () => {
Expand Down Expand Up @@ -37,17 +38,27 @@ describe('createActionableClient', () => {
})

it.each([
[() => redirect('/foo')],
[() => forbidden()],
[() => unauthorized()],
[() => notFound()],
])('should rethrow next.js error', async (error) => {
[() => next.redirect('/foo')],
[() => next.forbidden()],
[() => next.unauthorized()],
[() => next.notFound()],
[() => tanstackRouter.redirect({ to: '.login' })],
[() => tanstackRouter.notFound()],
])('should rethrow next.js or tanstack router error', async (createError) => {
(process as any).env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS = true

let error

client.mockImplementationOnce(() => {
error()
try {
throw createError()
}
catch (e) {
error = e
throw e
}
})

await expect(action('input')).rejects.toThrowError()
await expect(action('input')).rejects.toBe(error)
})
})
10 changes: 10 additions & 0 deletions packages/server/src/procedure-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Client, ORPCError, ORPCErrorJSON } from '@orpc/client'
import type { AnySchema, ErrorFromErrorMap, ErrorMap, InferSchemaInput, InferSchemaOutput } from '@orpc/contract'
import type { ThrowableError } from '@orpc/shared'
import { toORPCError } from '@orpc/client'
import { isObject } from '@orpc/shared'

export type ActionableError<T> = T extends ORPCError<infer U, infer V> ? ORPCErrorJSON<U, V> & { defined: true } : ORPCErrorJSON<string, unknown> & { defined: false }

Expand Down Expand Up @@ -35,6 +36,7 @@ export function createActionableClient<TInput, TOutput, TError>(
return [null, await client(input)]
}
catch (error) {
// special next.js errors
if (
error instanceof Error
&& 'digest' in error
Expand All @@ -44,6 +46,14 @@ export function createActionableClient<TInput, TOutput, TError>(
throw error
}

// special tanstack router errors
if (
(error instanceof Response && 'options' in error && isObject(error.options))
|| (isObject(error) && error.isNotFound === true)
) {
Comment thread
dinwwwh marked this conversation as resolved.
throw error
}

return [toORPCError(error).toJSON(), undefined]
}
}
Expand Down
37 changes: 27 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading