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

add error register in rpc handler #4356

Merged
merged 6 commits into from
Aug 5, 2024
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
6 changes: 6 additions & 0 deletions .changeset/empty-pugs-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@blitzjs/rpc": patch
"blitz": patch
---

fix: make sure blitz superjson custom error registers in rpc handler
4 changes: 3 additions & 1 deletion packages/blitz-rpc/src/index-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {assert, Ctx, ResolverConfig} from "blitz"
import {assert, Ctx, ResolverConfig, registerBlitzErrorClasses} from "blitz"
import {NextApiRequest, NextApiResponse} from "next"
import {resolve} from "path"
import {deserialize, parse, serialize as superjsonSerialize} from "superjson"
Expand Down Expand Up @@ -239,6 +239,7 @@ interface RpcConfig {
}

export function rpcHandler(config?: RpcConfig) {
registerBlitzErrorClasses()
return async function handleRpcRequest(req: NextApiRequest, res: NextApiResponse, ctx: Ctx) {
const resolverMap = await getResolverMap()
assert(resolverMap, "No query or mutation resolvers found")
Expand Down Expand Up @@ -362,6 +363,7 @@ export function rpcHandler(config?: RpcConfig) {
type Params = Record<string, unknown>

export function rpcAppHandler(config?: RpcConfig) {
registerBlitzErrorClasses()
async function handleRpcRequest(req: Request, context: {params: Params}, ctx?: Ctx) {
const session = ctx?.session
const resolverMap = await getResolverMap()
Expand Down
9 changes: 7 additions & 2 deletions packages/blitz/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export class PaginationArgumentError extends Error {
}
}

if (isNotInUserTestEnvironment() && !globalThis._BLITZ_ERROR_CLASS_REGISTERED) {
let _blitzErrorClassRegistered = false

export function registerBlitzErrorClasses() {
if (_blitzErrorClassRegistered || !isNotInUserTestEnvironment()) return
SuperJson.registerClass(AuthenticationError, {
identifier: "BlitzAuthenticationError",
allowProps: errorProps,
Expand Down Expand Up @@ -125,5 +128,7 @@ if (isNotInUserTestEnvironment() && !globalThis._BLITZ_ERROR_CLASS_REGISTERED) {
allowProps: errorProps,
})

globalThis._BLITZ_ERROR_CLASS_REGISTERED = true
_blitzErrorClassRegistered = true
}

registerBlitzErrorClasses()
2 changes: 2 additions & 0 deletions packages/blitz/src/index-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PaginationArgumentError,
RedirectError,
OAuthError,
registerBlitzErrorClasses,
} from "./errors"
import type {EventHooks, MiddlewareHooks} from "./types"
export {
Expand All @@ -18,6 +19,7 @@ export {
PaginationArgumentError,
RedirectError,
OAuthError,
registerBlitzErrorClasses,
}
export * from "./utils/enhance-prisma"

Expand Down
Loading