Skip to content

Commit

Permalink
add error register in rpc handler (#4356)
Browse files Browse the repository at this point in the history
* add error register in rpc handler

* fix

* Create empty-pugs-prove.md

* fixes

* Update packages/blitz/src/errors.ts
  • Loading branch information
siddhsuresh authored Aug 5, 2024
1 parent c80ce51 commit 9a0ba87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
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

0 comments on commit 9a0ba87

Please sign in to comment.