How to hide database ip when using tRPC + Prisma

Solution:
can you remove the stack and the shape.message from the error before returning for the client?
Jump to solution
31 Replies
Neto
Neto•3y ago
check the log level but i think in production there will be no stacktrace
Aaron
AaronOP•3y ago
you mean this? I didn't touch these before
Neto
Neto•3y ago
try building the app and starting and check the error next build next start
Aaron
AaronOP•3y ago
Aaron
AaronOP•3y ago
i change my db url to postgresql://postgres:test@test:1234/postgres, so you can see the error with db ip
Neto
Neto•3y ago
Error Handling | tRPC
Whenever an error occurs in a procedure, tRPC responds to the client with an object that includes an "error" property. This property contains all the information that you need to handle the error in the client.
Neto
Neto•3y ago
you can check dealing with errors on the trpc side of things before they bubble up to the next side of things
Aaron
AaronOP•3y ago
Aaron
AaronOP•3y ago
it still send the error to client
Neto
Neto•3y ago
there is a NODE_ENV in your .env.local file?
Aaron
AaronOP•3y ago
no
Aaron
AaronOP•3y ago
Neto
Neto•3y ago
try adding NODE_ENV=production and see what happens
Aaron
AaronOP•3y ago
Solution
Neto
Neto•3y ago
can you remove the stack and the shape.message from the error before returning for the client?
Aaron
AaronOP•3y ago
wow that's work
Aaron
AaronOP•3y ago
thx
Neto
Neto•3y ago
glad to help
tawaliou
tawaliou•3y ago
I think if you're in production you'll not get the stacktrace
Aaron
AaronOP•3y ago
I’m sure this is production, if not I will get prisma log in the terminal.
Aaron
AaronOP•3y ago
Aaron
AaronOP•3y ago
in production there's no @tanstack/react-query-devtools and Tailwind Indicator as well
Aaron
AaronOP•3y ago
tawaliou
tawaliou•3y ago
so you're not in production šŸ˜… . Btw did you set the env variable NODE_ENV to "production" ? ok
Aaron
AaronOP•3y ago
this is production I removed it i'm showing you if not in production I will get these things
tawaliou
tawaliou•3y ago
Ha
Aaron
AaronOP•3y ago
let me create a demo
Aaron
AaronOP•3y ago
GitHub
GitHub - nekochan0122/t3-db-ip-demo
Contribute to nekochan0122/t3-db-ip-demo development by creating an account on GitHub.
Aaron
AaronOP•3y ago
Aaron
AaronOP•3y ago
a solution by nyx
// src/pages/api/trpc/[trpc].ts

import { createNextApiHandler } from '@trpc/server/adapters/next'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/root'
import { createTRPCContext } from '~/server/api/trpc'

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`āŒ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`
)
}
: ({ error }) => {
if (error.message.includes(`Can't reach database server at`)) {
error.message = `Database is not running` // replace the error message in production
}
},
})
// src/pages/api/trpc/[trpc].ts

import { createNextApiHandler } from '@trpc/server/adapters/next'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/root'
import { createTRPCContext } from '~/server/api/trpc'

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`āŒ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`
)
}
: ({ error }) => {
if (error.message.includes(`Can't reach database server at`)) {
error.message = `Database is not running` // replace the error message in production
}
},
})

Did you find this page helpful?