Skip to content

Commit 9b05fd5

Browse files
committed
fix(db-mongodb): documentDB unique constraint throws incorrect error
1 parent f83d65e commit 9b05fd5

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

Diff for: packages/db-mongodb/src/create.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import type { Document, PayloadRequest } from 'payload/types'
33

44
import type { MongooseAdapter } from '.'
55

6+
import handleError from './utilities/handleError'
67
import { withSession } from './withSession'
7-
import { ValidationError } from 'payload/errors'
8-
import { i18nInit } from 'payload/utilities'
98

109
export const create: Create = async function create(
1110
this: MongooseAdapter,
@@ -17,18 +16,7 @@ export const create: Create = async function create(
1716
try {
1817
;[doc] = await Model.create([data], options)
1918
} catch (error) {
20-
// Handle uniqueness error from MongoDB
21-
throw error.code === 11000 && error.keyValue
22-
? new ValidationError(
23-
[
24-
{
25-
field: Object.keys(error.keyValue)[0],
26-
message: req.t('error:valueMustBeUnique'),
27-
},
28-
],
29-
req?.t ?? i18nInit(this.payload.config.i18n).t,
30-
)
31-
: error
19+
handleError(error, req)
3220
}
3321

3422
// doc.toJSON does not do stuff like converting ObjectIds to string, or date strings to date objects. That's why we use JSON.parse/stringify here

Diff for: packages/db-mongodb/src/updateOne.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { UpdateOne } from 'payload/database'
22
import type { PayloadRequest } from 'payload/types'
33

4-
import { ValidationError } from 'payload/errors'
5-
import { i18nInit } from 'payload/utilities'
6-
74
import type { MongooseAdapter } from '.'
85

6+
import handleError from './utilities/handleError'
97
import sanitizeInternalFields from './utilities/sanitizeInternalFields'
108
import { withSession } from './withSession'
119

@@ -31,18 +29,7 @@ export const updateOne: UpdateOne = async function updateOne(
3129
try {
3230
result = await Model.findOneAndUpdate(query, data, options)
3331
} catch (error) {
34-
// Handle uniqueness error from MongoDB
35-
throw error.code === 11000 && error.keyValue
36-
? new ValidationError(
37-
[
38-
{
39-
field: Object.keys(error.keyValue)[0],
40-
message: 'Value must be unique',
41-
},
42-
],
43-
req?.t ?? i18nInit(this.payload.config.i18n).t,
44-
)
45-
: error
32+
handleError(error, req)
4633
}
4734

4835
result = JSON.parse(JSON.stringify(result))

Diff for: packages/db-mongodb/src/utilities/handleError.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import httpStatus from 'http-status'
2+
import { APIError, ValidationError } from 'payload/errors'
3+
4+
const handleError = (error, req) => {
5+
// Handle uniqueness error from MongoDB
6+
if (error.code === 11000 && error.keyValue) {
7+
throw new ValidationError(
8+
[
9+
{
10+
field: Object.keys(error.keyValue)[0],
11+
message: req.t('error:valueMustBeUnique'),
12+
},
13+
],
14+
req.t,
15+
)
16+
} else if (error.code === 11000) {
17+
throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST)
18+
} else {
19+
throw error
20+
}
21+
}
22+
23+
export default handleError

Diff for: pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)