Skip to main content

拒绝未发现更改

从 Prisma ORM 版本 5.0.0 开始,已弃用的参数 rejectOnNotFound 已被删除。根据你的项目是每个查询使用 rejectOnNotFound 还是全局使用 rejectOnNotFound,更新代码的方式将会有所不同。

¥As of Prisma ORM version 5.0.0, the deprecated parameter rejectOnNotFound has been removed. Depending on if your project used rejectOnNotFound per query or globally, there will be be different ways of updating your code.

如果你在每个查询中使用 rejectOnNotFound 参数,请按照我们针对 在查询级别更新代码 的步骤操作。

¥If you are using the rejectOnNotFound parameter on a per-query basis, then follow our steps for updating your code at the query level.

如果你在客户端级别设置了 rejectOnNotFound 参数,则需要遵循 在客户端级别更新代码的步骤

¥If instead you have set up the rejectOnNotFound parameter at the client level, you will need to follow the steps for updating your code at the client level.

Prisma ORM 5 更改的完整列表可以在 在我们的发行说明中 中找到。

¥A full list of Prisma ORM 5 changes can be found in our release notes.

替换在查询级别启用的 rejectOnNotFound

¥Replacing rejectOnNotFound enabled at the query level

如果你之前在每个查询的基础上启用了 rejectOnNotFound,则需要在查询级别替换你的使用情况。你可以使用我们的 *OrThrow 查询变体、findFirstOrThrowfindUniqueOrThrow,而不是向 findFirstfindUnique() 提供参数。

¥If you previously enabled rejectOnNotFound on a per-query basis, you will need to replace your usage at the query level. You can use our *OrThrow query variants, findFirstOrThrow or findUniqueOrThrow instead of supplying the parameter to findFirst and findUnique().

简单的 rejectOnNotFound 用法

¥Simple rejectOnNotFound usage

下面的例子:

¥The following example:

prisma.user.findFirst({
where: { name: 'Alice' },
rejectOnNotFound: true,
})

需要转换为:

¥needs to be converted to:

prisma.user.findFirstOrThrow({
where: { name: 'Alice' },
})

rejectOnNotFound 与自定义错误处理程序的用法

¥rejectOnNotFound usage with custom error handler

如果你使用如下所示的自定义错误处理程序:

¥If you use a custom error handler like the following:

prisma.user.findFirst({
where: { name: 'Alice' },
rejectOnNotFound: () => new UserNotFoundError(),
})

你将需要修改代码以处理 ...OrThrow 方法引发的错误。

¥You will need to modify your code to handle the errors thrown by ...OrThrow methods.

try {
await prisma.user.findFirstOrThrow({
where: { name: 'Alice' },
})
} catch (err) {
if (err.code === 'P2025') {
throw new UserNotFoundError()
}
throw err
}

如果你的错误处理程序在多个地方使用,你还可以创建一个可重用的错误适配器,然后可以在你的函数调用的 .catch() 中使用该适配器。

¥If your error handler is used in multiple places, you can also create a reusable error adapter which could then be used within a .catch() called on your function.

const adaptError = (customThrowFn) => (error) => {
if (error.code === 'P2025') {
throw customThrowFn()
}
throw error
}

const user = await prisma.user.findFirstOrThrow({
where: { name: 'Alice' },
}).catch(adaptError(() => new MyCustomError())

替换在客户端级别启用的 rejectOnNotFound

¥Replacing rejectOnNotFound enabled at the Client level

rejectOnNotFound 通过 Prisma 客户端构造函数

¥rejectOnNotFound via Prisma Client Constructor

如果你之前通过 Prisma Client 构造函数中的配置全局启用了 rejectOnNotFound,如以下示例所示:

¥If you previously enabled rejectOnNotFound globally via configuration in the Prisma Client constructor, like in these examples:

// Example 1
const prisma = new PrismaClient({
rejectOnNotFound: true,
})

// Example 2
const prisma = new PrismaClient({
rejectOnNotFound: {
findUnique: true,
},
})

你将需要更新代码库以使用 findUniqueOrThrowfindFirstOrThrow 而不是 findUnique()findFirst,具体取决于你想要抛出哪些调用。

¥You will need to update your codebase to use findUniqueOrThrow and findFirstOrThrow instead of findUnique() and findFirst, depending on which calls you would like to throw.

rejectOnNotFound 通过 Prisma 客户端构造函数和自定义错误处理程序

¥rejectOnNotFound via Prisma Client Constructor with custom error handler

如果你使用带有 rejectOnNotFound 属性的自定义错误处理程序,如下例所示:

¥If instead you use a custom error handler with the rejectOnNotFound property, like these examples:

// Example 3
const prisma = new PrismaClient({
rejectOnNotFound: (err) => new Error('something'),
})

// Example 4
const prisma = new PrismaClient({
rejectOnNotFound: {
findUnique: (err) => new Error('something'),
},
})

// Example 5
const prisma = new PrismaClient({
rejectOnNotFound: {
findFirst: {
User: (err) => new Error('User error'),
Post: (err) => new Error('Post error'),
},
findUnique: {
User: (err) => new Error('User error'),
Post: (err) => new Error('Post error'),
},
},
})

你需要将方法用法更新为 ...OrThrow,然后使用 客户端扩展 才能获得相同的行为。

¥You will need to update your method usage to ...OrThrow and then use a Client Extension in order to get the same behavior.

例如,以下扩展将在 Prisma ORM 5 中提供与 Example 5 在 Prisma ORM 4 及更低版本中提供的相同行为。

¥As an example, the following extension would give the same behavior in Prisma ORM 5 that Example 5 gave in Prisma ORM 4 and lower.

import { PrismaClient } from '@prisma/client';

const customErrorFunc = async (model, query, args) => {
try {
await query(args)
} catch (error: any) {
if (error.code === 'P2025') {
throw new Error(`${model} error`)
}
throw error;
}
}

const prisma = (new PrismaClient()).$extends({
query: {
user: {
async findFirstOrThrow({ model, query, args }) {
return await customErrorFunc(model, query, args)
},
async findUniqueOrThrow({ model, query, args }) {
return await customErrorFunc(model, query, args)
},
},
post: {
async findFirstOrThrow({ model, query, args }) {
return await customErrorFunc(model, query, args)
},
async findUniqueOrThrow({ model, query, args }) {
return await customErrorFunc(model, query, args)
},
},
},
})