Skip to content

Commit

Permalink
fix(client): rejectOnNotFound per action per model (#5183)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamluke4 committed Jan 19, 2021
1 parent b5667e5 commit efdfd92
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 131 deletions.
Expand Up @@ -1126,7 +1126,7 @@ export namespace Prisma {
db?: Datasource
}

export type RejectOnNotFound = boolean | Error | ((error: Error) => Error)
export type RejectOnNotFound = boolean | ((error: Error) => Error)
export type RejectPerModel = { [P in ModelName]?: RejectOnNotFound }
export type RejectPerOperation = { [P in "findUnique" | "findFirst"]?: RejectPerModel | RejectOnNotFound }

Expand All @@ -1139,12 +1139,10 @@ export namespace Prisma {
* \`\`\`
* // Reject on both findUnique/findFirst
* rejectOnNotFound: true
*
* // Reject only on findFirst with a custom error
* rejectOnNotFound: { findFirst: (err) => new Error("Custom Error")}
*
* // Reject on user.findUnique with a custom error
* rejectOnNotFound: { findUnique: {User: new Error("User not found")}}
* rejectOnNotFound: { findUnique: {User: (err) => new Error("User not found")}}
* \`\`\`
*/
rejectOnNotFound?: RejectOnNotFound | RejectPerOperation
Expand Down
@@ -1,178 +1,126 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`rejectOnNotFound | constructor=booleanPerAction | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;

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

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

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

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

exports[`rejectOnNotFound | constructor=booleanPerAction | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;

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

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

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

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

exports[`rejectOnNotFound | constructor=customError | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;
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 Thunk') 1`] = `FindUnique Thunk`;

exports[`rejectOnNotFound | constructor=customError | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique 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 Thunk') 1`] = `FindFirst Thunk`;

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst 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`] = `null`;
exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findFirst=undefined 1`] = `Constructor Custom Error on findFirst:User`;

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

exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
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`] = `null`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=() => new Error('FindFirst Thunk') 1`] = `FindFirst Thunk`;
exports[`rejectOnNotFound | constructor=customErrorPerActionPerModel | findUnique=undefined 1`] = `Constructor Thunk on findUnique:User`;

exports[`rejectOnNotFound | constructor=errorPerAction | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;
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 Thunk') 1`] = `FindUnique Thunk`;

exports[`rejectOnNotFound | constructor=errorPerAction | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
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 Thunk') 1`] = `FindFirst Thunk`;

exports[`rejectOnNotFound | constructor=false | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;
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 Thunk') 1`] = `FindUnique Thunk`;

exports[`rejectOnNotFound | constructor=false | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
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=thunk | findFirst=() => new Error('FindFirst Thunk') 1`] = `FindFirst Thunk`;

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

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

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

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

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

exports[`rejectOnNotFound | constructor=thunk | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
exports[`rejectOnNotFound | constructor=falsePerAction | findUnique=false 1`] = `null`;

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

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

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

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

exports[`rejectOnNotFound | constructor=thunkPerAction | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;

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

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

exports[`rejectOnNotFound | constructor=thunkPerAction | findFirst=undefined 1`] = `Constructor Thunk on findFirst`;

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

exports[`rejectOnNotFound | constructor=thunkPerAction | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
exports[`rejectOnNotFound | constructor=true | findFirst=false 1`] = `null`;

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

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

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

exports[`rejectOnNotFound | constructor=true | findFirst=() => new Error('FindFirst Thunk') 1`] = `FindFirst Thunk`;
exports[`rejectOnNotFound | constructor=true | findUnique=false 1`] = `null`;

exports[`rejectOnNotFound | constructor=true | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;
exports[`rejectOnNotFound | constructor=true | findUnique=true 1`] = `No User found`;

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

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

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

exports[`rejectOnNotFound | constructor=true | findUnique=() => new Error('FindUnique Thunk') 1`] = `FindUnique Thunk`;
exports[`rejectOnNotFound | constructor=truePerAction | findFirst=true 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=true | findUnique=Error: FindUnique Custom Error 1`] = `FindUnique Custom Error`;
exports[`rejectOnNotFound | constructor=truePerAction | findFirst=undefined 1`] = `No User found`;

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

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

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

exports[`rejectOnNotFound | constructor=undefined | findFirst=() => new Error('FindFirst Thunk') 1`] = `FindFirst Thunk`;
exports[`rejectOnNotFound | constructor=truePerAction | findUnique=undefined 1`] = `No User found`;

exports[`rejectOnNotFound | constructor=undefined | findFirst=Error: FindFirst Custom Error 1`] = `FindFirst Custom Error`;
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 Thunk') 1`] = `FindUnique Thunk`;

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

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

Expand Down
@@ -1,44 +1,40 @@
import { isError } from '@prisma/sdk'
import { getTestClient } from '../../../../utils/getTestClient'
const cases = {
constructor: {
customError: new Error('Constructor Custom Error'),
booleanPerAction: {
customError: () => new Error('Constructor Custom Error'),
truePerAction: {
findUnique: true,
findFirst: false,
findFirst: true,
},
thunkPerAction: {
findFirst: () => new Error('Constructor Thunk on findFirst'),
findUnique: (err) => err
falsePerAction: {
findUnique: false,
findFirst: false,
},
errorPerAction: {
findFirst: Error('Constructor Error on findFirst'),
findUnique: Error('Constructor Error on findUnique'),
findFirst: () => new Error('Constructor Error on findFirst'),
findUnique: () => new Error('Constructor Error on findUnique'),
},
customErrorPerActionPerModel: {
findFirst: {
User: new Error('Constructor Custom Error on findFirst:User')
User: () => new Error('Constructor Custom Error on findFirst:User'),
},
findUnique: {
User: () => new Error('Constructor Thunk on findUnique:User')
User: () => new Error('Constructor Thunk on findUnique:User'),
},
},
thunk: () => new Error('Constructor Thunk'),
true: true,
false: false,
undefined: undefined,
},
methods: {
findUnique: {
customError: new Error('FindUnique Custom Error'),
thunk: () => new Error('FindUnique Thunk'),
customError: () => new Error('FindUnique Custom Error'),
true: true,
false: false,
undefined: undefined,
},
findFirst: {
customError: new Error('FindFirst Custom Error'),
thunk: () => new Error('FindFirst Thunk'),
customError: () => new Error('FindFirst Custom Error'),
true: true,
false: false,
undefined: undefined,
Expand Down
Expand Up @@ -17,6 +17,12 @@ const p1 = new PrismaClient({
rejectOnNotFound: 'true',
}),
)
expectError(
new PrismaClient({
rejectOnNotFound: new Error('Error'),
}),
)

expectError(
new PrismaClient({
rejectOnNotFound: {
Expand Down Expand Up @@ -72,5 +78,4 @@ const p1 = new PrismaClient({
},
}),
)

})()
14 changes: 6 additions & 8 deletions src/packages/client/src/__tests__/types/rejectOnNotFound/test.ts
Expand Up @@ -8,23 +8,21 @@ async function main() {
const p1 = new PrismaClient({
rejectOnNotFound: true,
})
const p2 = new PrismaClient({
rejectOnNotFound: new Error('Error'),
})

const p21 = new PrismaClient({
rejectOnNotFound: () => new Error('Error'),
})
const p3 = new PrismaClient({
rejectOnNotFound: {
findUnique: new Error('Error'),
findUnique: () => new Error('Error'),
findFirst: true,
},
})
const p4 = new PrismaClient({
rejectOnNotFound: {
findUnique: {
findUnique: {
User: true,
MachineData: new Error('Error'),
MachineData: () => new Error('Error'),
Post: () => new Error('Error'),
},
findFirst: false,
Expand All @@ -40,7 +38,7 @@ async function main() {
})
p1.user.findUnique({
where: { id: 'anything' },
rejectOnNotFound: new Error('FindUnique Custom Error'),
rejectOnNotFound: () => new Error('FindUnique Custom Error'),
})
p1.user.findUnique({
where: { id: 'anything' },
Expand All @@ -61,7 +59,7 @@ async function main() {
})
p1.user.findFirst({
where: { id: 'anything' },
rejectOnNotFound: new Error('FindUnique Custom Error'),
rejectOnNotFound: () => new Error('FindUnique Custom Error'),
})
p1.user.findFirst({
where: { id: 'anything' },
Expand Down
6 changes: 2 additions & 4 deletions src/packages/client/src/generation/TSClient/PrismaClient.ts
Expand Up @@ -158,7 +158,7 @@ get ${methodName}(): Prisma.${m.model}Delegate;`
public toTS(): string {
return `${new Datasources(this.internalDatasources).toTS()}
export type RejectOnNotFound = boolean | Error | ((error: Error) => Error)
export type RejectOnNotFound = boolean | ((error: Error) => Error)
export type RejectPerModel = { [P in ModelName]?: RejectOnNotFound }
export type RejectPerOperation = { [P in "findUnique" | "findFirst"]?: RejectPerModel | RejectOnNotFound }
Expand All @@ -171,12 +171,10 @@ export interface PrismaClientOptions {
* \`\`\`
* // Reject on both findUnique/findFirst
* rejectOnNotFound: true
*
* // Reject only on findFirst with a custom error
* rejectOnNotFound: { findFirst: (err) => new Error("Custom Error")}
*
* // Reject on user.findUnique with a custom error
* rejectOnNotFound: { findUnique: {User: new Error("User not found")}}
* rejectOnNotFound: { findUnique: {User: (err) => new Error("User not found")}}
* \`\`\`
*/
rejectOnNotFound?: RejectOnNotFound | RejectPerOperation
Expand Down
2 changes: 1 addition & 1 deletion src/packages/client/src/runtime/getPrismaClient.ts
Expand Up @@ -38,7 +38,6 @@ import {
import { clientVersion } from './utils/clientVersion'
import { getOutputTypeName, lowerCase } from './utils/common'
import { deepSet } from './utils/deep-set'
import { fromEntries } from './utils/fromEntries'
import { mssqlPreparedStatement } from './utils/mssqlPreparedStatement'
import { printJsonWithErrors } from './utils/printJsonErrors'
import { printStack } from './utils/printStack'
Expand Down Expand Up @@ -1045,6 +1044,7 @@ new PrismaClient({

const rejectOnNotFound: RejectOnNotFound = getRejectOnNotFound(
action,
typeName,
args,
this._rejectOnNotFound,
)
Expand Down

0 comments on commit efdfd92

Please sign in to comment.