Skip to content

Commit

Permalink
Ensure superjson registers error classes once (#3982)
Browse files Browse the repository at this point in the history
* bump superjson version & ensure superjson register

* update changeset & use error name as property for check if class has been registered

* refactor
  • Loading branch information
Dillon Raphael authored Nov 22, 2022
1 parent c1e0040 commit b33db08
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
7 changes: 7 additions & 0 deletions .changeset/tender-cooks-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/next": patch
"@blitzjs/rpc": patch
---

Fix ambigious class warning log & upgrade superjson from 1.9.1 to 1.11.0
2 changes: 1 addition & 1 deletion packages/blitz-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"debug": "4.3.3",
"fs-extra": "10.0.1",
"hoist-non-react-statics": "3.3.2",
"superjson": "1.9.1",
"superjson": "1.11.0",
"supports-color": "8.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/blitz-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
"debug": "4.3.3",
"superjson": "1.9.1",
"superjson": "1.11.0",
"supports-color": "8.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/blitz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"resolve-cwd": "3.0.0",
"resolve-from": "5.0.0",
"rimraf": "3.0.2",
"superjson": "1.9.1",
"superjson": "1.11.0",
"supports-color": "8.1.1",
"tar": "6.1.11",
"ts-node": "10.9.1",
Expand Down
64 changes: 33 additions & 31 deletions packages/blitz/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import _SuperJson from "superjson"
import type {UrlObject} from "url"

declare module globalThis {
let _BLITZ_ERROR_CLASS_REGISTERED: boolean
}

const SuperJson: typeof _SuperJson =
"default" in _SuperJson ? (_SuperJson as any).default : _SuperJson

Expand All @@ -19,12 +23,6 @@ export class AuthenticationError extends Error {
return true
}
}
if (process.env.JEST_WORKER_ID === undefined) {
SuperJson.registerClass(AuthenticationError, {
identifier: "BlitzAuthenticationError",
allowProps: errorProps,
})
}

export class CSRFTokenMismatchError extends Error {
name = "CSRFTokenMismatchError"
Expand All @@ -33,12 +31,6 @@ export class CSRFTokenMismatchError extends Error {
return true
}
}
if (process.env.JEST_WORKER_ID === undefined) {
SuperJson.registerClass(CSRFTokenMismatchError, {
identifier: "BlitzCSRFTokenMismatchError",
allowProps: errorProps,
})
}

export class AuthorizationError extends Error {
name = "AuthorizationError"
Expand All @@ -50,12 +42,6 @@ export class AuthorizationError extends Error {
return true
}
}
if (process.env.JEST_WORKER_ID === undefined) {
SuperJson.registerClass(AuthorizationError, {
identifier: "BlitzAuthorizationError",
allowProps: errorProps,
})
}

export class NotFoundError extends Error {
name = "NotFoundError"
Expand All @@ -67,12 +53,6 @@ export class NotFoundError extends Error {
return true
}
}
if (process.env.JEST_WORKER_ID === undefined) {
SuperJson.registerClass(NotFoundError, {
identifier: "BlitzNotFoundError",
allowProps: errorProps,
})
}

export class RedirectError extends Error {
name = "RedirectError"
Expand All @@ -86,12 +66,6 @@ export class RedirectError extends Error {
return true
}
}
if (process.env.JEST_WORKER_ID === undefined) {
SuperJson.registerClass(RedirectError, {
identifier: "BlitzRedirectError",
allowProps: errorProps,
})
}

export class PaginationArgumentError extends Error {
name = "PaginationArgumentError"
Expand All @@ -100,9 +74,37 @@ export class PaginationArgumentError extends Error {
super(message)
}
}
if (process.env.JEST_WORKER_ID === undefined) {

if (process.env.JEST_WORKER_ID === undefined && !globalThis._BLITZ_ERROR_CLASS_REGISTERED) {
SuperJson.registerClass(AuthenticationError, {
identifier: "BlitzAuthenticationError",
allowProps: errorProps,
})

SuperJson.registerClass(CSRFTokenMismatchError, {
identifier: "BlitzCSRFTokenMismatchError",
allowProps: errorProps,
})

SuperJson.registerClass(AuthorizationError, {
identifier: "BlitzAuthorizationError",
allowProps: errorProps,
})

SuperJson.registerClass(NotFoundError, {
identifier: "BlitzNotFoundError",
allowProps: errorProps,
})

SuperJson.registerClass(RedirectError, {
identifier: "BlitzRedirectError",
allowProps: errorProps,
})

SuperJson.registerClass(PaginationArgumentError, {
identifier: "BlitzPaginationArgumentError",
allowProps: errorProps,
})

globalThis._BLITZ_ERROR_CLASS_REGISTERED = true
}
24 changes: 14 additions & 10 deletions pnpm-lock.yaml

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

0 comments on commit b33db08

Please sign in to comment.