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ā€¢2y ago
check the log level but i think in production there will be no stacktrace
Uika
UikaOPā€¢2y ago
you mean this? I didn't touch these before
Neto
Netoā€¢2y ago
try building the app and starting and check the error next build next start
Uika
UikaOPā€¢2y ago
Uika
UikaOPā€¢2y ago
i change my db url to postgresql://postgres:test@test:1234/postgres, so you can see the error with db ip
Neto
Netoā€¢2y 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ā€¢2y ago
you can check dealing with errors on the trpc side of things before they bubble up to the next side of things
Uika
UikaOPā€¢2y ago
Uika
UikaOPā€¢2y ago
it still send the error to client
Neto
Netoā€¢2y ago
there is a NODE_ENV in your .env.local file?
Uika
UikaOPā€¢2y ago
no
Uika
UikaOPā€¢2y ago
Neto
Netoā€¢2y ago
try adding NODE_ENV=production and see what happens
Uika
UikaOPā€¢2y ago
Solution
Neto
Netoā€¢2y ago
can you remove the stack and the shape.message from the error before returning for the client?
Uika
UikaOPā€¢2y ago
wow that's work
Uika
UikaOPā€¢2y ago
thx
Neto
Netoā€¢2y ago
glad to help
tawaliou
tawaliouā€¢2y ago
I think if you're in production you'll not get the stacktrace
Uika
UikaOPā€¢2y ago
Iā€™m sure this is production, if not I will get prisma log in the terminal.
Uika
UikaOPā€¢2y ago
Uika
UikaOPā€¢2y ago
in production there's no @tanstack/react-query-devtools and Tailwind Indicator as well
Uika
UikaOPā€¢2y ago
tawaliou
tawaliouā€¢2y ago
so you're not in production šŸ˜… . Btw did you set the env variable NODE_ENV to "production" ? ok
Uika
UikaOPā€¢2y ago
this is production I removed it i'm showing you if not in production I will get these things
tawaliou
tawaliouā€¢2y ago
Ha
Uika
UikaOPā€¢2y ago
let me create a demo
Uika
UikaOPā€¢2y ago
GitHub
GitHub - nekochan0122/t3-db-ip-demo
Contribute to nekochan0122/t3-db-ip-demo development by creating an account on GitHub.
Uika
UikaOPā€¢2y ago
Uika
UikaOPā€¢2y 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?