Skip to content

Commit 8128105

Browse files
committed
feat(payload): add missing disableErrors to globals findOne operation
1 parent 77f96a4 commit 8128105

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

packages/payload/src/globals/operations/findOne.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type GlobalFindOneArgs = {
2525
*/
2626
data?: Record<string, unknown>
2727
depth?: number
28+
disableErrors?: boolean
2829
draft?: boolean
2930
globalConfig: SanitizedGlobalConfig
3031
includeLockStatus?: boolean
@@ -42,6 +43,7 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
4243
const {
4344
slug,
4445
depth,
46+
disableErrors,
4547
draft: replaceWithVersion = false,
4648
flattenLocales,
4749
globalConfig,
@@ -79,11 +81,14 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
7981
let accessResult!: AccessResult
8082

8183
if (!overrideAccess) {
82-
accessResult = await executeAccess({ req }, globalConfig.access.read)
84+
accessResult = await executeAccess({ disableErrors, req }, globalConfig.access.read)
8385
}
8486

8587
if (accessResult === false) {
86-
throw new NotFound(req.t)
88+
if (!disableErrors) {
89+
throw new NotFound(req.t)
90+
}
91+
return null!
8792
}
8893

8994
const select = sanitizeSelect({
@@ -108,7 +113,10 @@ export const findOneOperation = async <T extends Record<string, unknown>>(
108113
const hasDoc = docFromDB && Object.keys(docFromDB).length > 0
109114

110115
if (!hasDoc && !args.data && !overrideAccess && accessResult !== true) {
111-
return {} as any
116+
if (!disableErrors) {
117+
return {} as any
118+
}
119+
return null!
112120
}
113121

114122
let doc = (args.data as any) ?? (hasDoc ? docFromDB : null) ?? {}

packages/payload/src/globals/operations/local/findOne.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export type Options<TSlug extends GlobalSlug, TSelect extends SelectType> = {
3636
* [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields.
3737
*/
3838
depth?: number
39+
/**
40+
* When set to `true`, errors will not be thrown.
41+
*/
42+
disableErrors?: boolean
3943
/**
4044
* Whether the document should be queried from the versions table/collection or not. [More](https://payloadcms.com/docs/versions/drafts#draft-api)
4145
*/
@@ -97,6 +101,7 @@ export async function findOneGlobalLocal<
97101
slug: globalSlug,
98102
data,
99103
depth,
104+
disableErrors,
100105
draft = false,
101106
flattenLocales,
102107
includeLockStatus,
@@ -116,6 +121,7 @@ export async function findOneGlobalLocal<
116121
slug: globalSlug as string,
117122
data,
118123
depth,
124+
disableErrors,
119125
draft,
120126
flattenLocales,
121127
globalConfig,

test/globals/int.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ describe('globals', () => {
172172
expect(es).toMatchObject(localized.es)
173173
})
174174

175+
it('should return null when user is unauthorised and using findGlobal with disableErrors: true', async () => {
176+
const doc = await payload.findGlobal({
177+
disableErrors: true,
178+
overrideAccess: false,
179+
slug: accessControlSlug,
180+
})
181+
182+
expect(doc).toBeNull()
183+
})
184+
175185
it('should respect valid access query constraint', async () => {
176186
const emptyGlobal = await payload.findGlobal({
177187
overrideAccess: false,

0 commit comments

Comments
 (0)