Skip to content

Commit

Permalink
fix(client): add stacktrace when rejectOnNotFound is enabled (#10645)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
3 people committed Dec 14, 2021
1 parent ec0a018 commit d63c47b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 50 deletions.
Expand Up @@ -2,128 +2,84 @@

exports[`rejectOnNotFound | constructor=customError | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=customError | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=customError | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=customError | findFirst=undefined 1`] = `Constructor Custom Error`;

exports[`rejectOnNotFound | constructor=customError | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=customError | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=customError | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=customError | findUnique=undefined 1`] = `Constructor Custom Error`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=undefined 1`] = `Constructor Custom Error on findFirst:User`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=undefined 1`] = `Constructor Thunk on findUnique:User`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=undefined 1`] = `Constructor Error on findFirst`;

exports[`rejectOnNotFound | constructor=errorPerAction | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=errorPerAction | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=errorPerAction | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=errorPerAction | findUnique=undefined 1`] = `Constructor Error on findUnique`;

exports[`rejectOnNotFound | constructor=false | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=false | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=false | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=false | findFirst=undefined 1`] = `null`;

exports[`rejectOnNotFound | constructor=false | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=false | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=false | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=false | findUnique=undefined 1`] = `null`;

exports[`rejectOnNotFound | constructor=falsePerAction | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=falsePerAction | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=falsePerAction | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=falsePerAction | findFirst=undefined 1`] = `null`;

exports[`rejectOnNotFound | constructor=falsePerAction | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=falsePerAction | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=falsePerAction | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=falsePerAction | findUnique=undefined 1`] = `null`;

exports[`rejectOnNotFound | constructor=true | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=true | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=true | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=true | findFirst=undefined 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=true | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=true | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=true | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=true | findUnique=undefined 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=truePerAction | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=truePerAction | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=truePerAction | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=truePerAction | findFirst=undefined 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=truePerAction | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=truePerAction | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=truePerAction | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=truePerAction | findUnique=undefined 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=undefined | findFirst=() => new Error('FindFirst Custom Error') 1`] = `FindFirst Custom Error`;

exports[`rejectOnNotFound | constructor=undefined | findFirst=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=undefined | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=undefined | findFirst=undefined 1`] = `null`;

exports[`rejectOnNotFound | constructor=undefined | findUnique=() => new Error('FindUnique Custom Error') 1`] = `FindUnique Custom Error`;

exports[`rejectOnNotFound | constructor=undefined | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=undefined | findUnique=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=undefined | findUnique=undefined 1`] = `null`;
Expand Up @@ -49,22 +49,33 @@ for (const constructorKey of Object.keys(cases.constructor)) {
for (const valueKey of Object.keys(currentMethod)) {
const value = currentMethod[valueKey]
test(`rejectOnNotFound | constructor=${constructorKey} | ${method}=${value}`, async () => {
// It should fail or not
expect.assertions(1)
const PrismaClient = await getTestClient()
const prisma = new PrismaClient({
rejectOnNotFound: constructor,
})

// Test Rejection
try {
// This function name is important cause we're searching
// for its name in the stack below
const testRejectionOnNotFound = async () => {
const r = await prisma.user[method]({
where: { id: 'none' },
rejectOnNotFound: value,
})
expect(r).toMatchSnapshot()

// If it got to here, then no error was thrown
// so we check the value if it's equal to null
expect(r).toBeNull()
}

try {
await testRejectionOnNotFound()
} catch (error) {
const { message, stack } = error
expect(stack).toBeDefined()
expect(message).toBeDefined()

expect(error).toMatchSnapshot()
expect(stack).toContain('at testRejectionOnNotFound')
}
await prisma.$disconnect()
})
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/runtime/utils/rejectOnNotFound.ts
Expand Up @@ -11,7 +11,6 @@ export class NotFoundError extends Error {
constructor(message: string) {
super(message)
this.name = 'NotFoundError'
this.stack = undefined
}
}

Expand Down

0 comments on commit d63c47b

Please sign in to comment.