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
NekoChan
NekoChanā€¢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
NekoChan
NekoChanā€¢2y ago
NekoChan
NekoChanā€¢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
NekoChan
NekoChanā€¢2y ago
NekoChan
NekoChanā€¢2y ago
it still send the error to client
Neto
Netoā€¢2y ago
there is a NODE_ENV in your .env.local file?
NekoChan
NekoChanā€¢2y ago
no
NekoChan
NekoChanā€¢2y ago
Neto
Netoā€¢2y ago
try adding NODE_ENV=production and see what happens
NekoChan
NekoChanā€¢2y ago
Solution
Neto
Netoā€¢2y ago
can you remove the stack and the shape.message from the error before returning for the client?
NekoChan
NekoChanā€¢2y ago
wow that's work
NekoChan
NekoChanā€¢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
NekoChan
NekoChanā€¢2y ago
Iā€™m sure this is production, if not I will get prisma log in the terminal.
NekoChan
NekoChanā€¢2y ago
NekoChan
NekoChanā€¢2y ago
in production there's no @tanstack/react-query-devtools and Tailwind Indicator as well
NekoChan
NekoChanā€¢2y ago
tawaliou
tawaliouā€¢2y ago
so you're not in production šŸ˜… . Btw did you set the env variable NODE_ENV to "production" ? ok
NekoChan
NekoChanā€¢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
NekoChan
NekoChanā€¢2y ago
let me create a demo
NekoChan
NekoChanā€¢2y ago
GitHub
GitHub - nekochan0122/t3-db-ip-demo
Contribute to nekochan0122/t3-db-ip-demo development by creating an account on GitHub.
NekoChan
NekoChanā€¢2y ago
NekoChan
NekoChanā€¢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
}
},
})
Want results from more Discord servers?
Add your server