Skip to content

Commit

Permalink
Merge branch 'main' into server-error-ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
papsavas authored Dec 15, 2023
2 parents b2dcc43 + 86e8eb7 commit c25a9dd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/cyan-rings-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@blitzjs/rpc": patch
"blitz": patch
---

Add helpful error message when RPC resolvers have the same path
50 changes: 46 additions & 4 deletions packages/blitz-rpc/src/index-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ function getGlobalObject<T extends Record<string, unknown>>(key: string, default
}

type Resolver = (...args: unknown[]) => Promise<unknown>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver; config?: ResolverConfig}>>
type ResolverFiles = Record<
string,
{
absoluteResolverPath: string
resolver: () => Promise<{default?: Resolver; config?: ResolverConfig}>
}
>
export type ResolverPathOptions = "queries|mutations" | "root" | ((path: string) => string)

// We define `global.__internal_blitzRpcResolverFiles` to ensure we use the same global object.
Expand All @@ -50,10 +56,46 @@ export function loadBlitzRpcResolverFilesWithInternalMechanism() {

export function __internal_addBlitzRpcResolver(
routePath: string,
absoluteResolverPath: string,
resolver: () => Promise<{default?: Resolver; config?: ResolverConfig}>,
) {
g.blitzRpcResolverFilesLoaded = g.blitzRpcResolverFilesLoaded || {}
g.blitzRpcResolverFilesLoaded[routePath] = resolver
const existingResolver = g.blitzRpcResolverFilesLoaded[routePath]
if (existingResolver && existingResolver.absoluteResolverPath !== absoluteResolverPath) {
const logger = new RpcLogger(routePath)
const errorMessage = `\nThe following resolver files resolve to the same path: ${routePath}\n\n1. ${absoluteResolverPath}\n2. ${
existingResolver.absoluteResolverPath
}\n\nPossible Solutions:\n\n1. Remove or rename one of the resolver files. \n2. Set the following in your in next.config.js to load all resolvers using their absolute paths:
\n\n//next.config.js\nblitz:{\n resolverPath: "root",\n},\n
\n${
process.env.NODE_ENV === "production"
? `Resolver in ${absoluteResolverPath} is currently being shadowed by ${existingResolver.absoluteResolverPath}`
: ""
}
`
logger.error(errorMessage)
if (process.env.NODE_ENV !== "production") {
g.blitzRpcResolverFilesLoaded[routePath] = {
absoluteResolverPath,
resolver: async () => {
return {
...(await resolver()),
default: async () => {
const error = new Error(errorMessage)
error.name = "BlitzRPCResolverCollisionError"
error.stack = ""
throw error
},
}
},
}
}
} else {
g.blitzRpcResolverFilesLoaded[routePath] = {
absoluteResolverPath,
resolver,
}
}
return resolver
}

Expand Down Expand Up @@ -164,7 +206,7 @@ export function rpcHandler(config: RpcConfig) {
const resolverName = routePath.replace(/(\/api\/rpc)?\//, "")
const rpcLogger = new RpcLogger(resolverName, config.logging)

const loadableResolver = resolverMap?.[routePath]
const loadableResolver = resolverMap?.[routePath]?.resolver
if (!loadableResolver) {
throw new Error("No resolver for path: " + routePath)
}
Expand Down Expand Up @@ -237,7 +279,7 @@ export function rpcHandler(config: RpcConfig) {
return
} catch (error: any) {
if (error._clearStack) {
delete error.stack
error.stack = ""
}

config.onError?.(error, ctx)
Expand Down
5 changes: 3 additions & 2 deletions packages/blitz-rpc/src/rpc-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ export class RpcLogger {
newLine()
}
public error(e: any) {
if (typeof e === "string") {
if (typeof e === "string" || e instanceof Error) {
this.#logger.error(e)
} else {
this.#logger.error(new Error(e))
}
this.#logger.error(new Error(e))
newLine()
}
public warn(e: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function transformBlitzRpcServer(

const importStrategy = options?.resolversDynamicImport ? "import" : "require"

code += `__internal_addBlitzRpcResolver('${routePath}',() => ${importStrategy}('${slash(
code += `__internal_addBlitzRpcResolver('${routePath}','${resolverFilePath}',() => ${importStrategy}('${slash(
resolverFilePath,
)}'));`
code += "\n"
Expand Down

0 comments on commit c25a9dd

Please sign in to comment.