jacksloan / prisma-proxy

Create type-safe proxy servers/clients for Prisma
MIT License
5 stars 2 forks source link

Better error handling for nullish results #4

Closed vladinator1000 closed 2 years ago

vladinator1000 commented 2 years ago

Sometimes when a query goes wrong I get an error that looks like

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Response.json (/home/vlady/code/yupty/api/node_modules/undici/lib/fetch/body.js:302:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Response.json (/home/vlady/code/yupty/api/node_modules/@miniflare/core/src/standards/http.ts:348:18)
    at UserModel.create (/home/vlady/code/yupty/api/server/db/models/User.ts:58:14)

This turned out to be because I was calling something like this, which returns null

prisma.user.findFirst({
    where: {
      email: 'nonexistent@email.com',
    },
})

Wonder if there is a way to better log errors to improve debugability? Or if we could do something to prevent JSON.parse being called on non-json results like this one?

jacksloan commented 2 years ago

PR'd with some minor error handling improvements

Added handling for valid null-ish results in this PR. Added a check for other JSON parse errors. There's still a lot of room overall for improvements in error handling.