Skip to content

fix(db-mongodb): documentDB unique constraint throws incorrect error #4513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/db-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"mongoose-aggregate-paginate-v2": "1.0.6",
"mongoose-paginate-v2": "1.7.22",
"prompts": "2.4.2",
"http-status": "1.6.2",
"uuid": "9.0.0"
},
"devDependencies": {
Expand Down
16 changes: 2 additions & 14 deletions packages/db-mongodb/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import type { Document, PayloadRequest } from 'payload/types'

import type { MongooseAdapter } from '.'

import handleError from './utilities/handleError'
import { withSession } from './withSession'
import { ValidationError } from 'payload/errors'
import { i18nInit } from 'payload/utilities'

export const create: Create = async function create(
this: MongooseAdapter,
Expand All @@ -17,18 +16,7 @@ export const create: Create = async function create(
try {
;[doc] = await Model.create([data], options)
} catch (error) {
// Handle uniqueness error from MongoDB
throw error.code === 11000 && error.keyValue
? new ValidationError(
[
{
field: Object.keys(error.keyValue)[0],
message: req.t('error:valueMustBeUnique'),
},
],
req?.t ?? i18nInit(this.payload.config.i18n).t,
)
: error
handleError(error, req)
}

// 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
Expand Down
17 changes: 2 additions & 15 deletions packages/db-mongodb/src/updateOne.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { UpdateOne } from 'payload/database'
import type { PayloadRequest } from 'payload/types'

import { ValidationError } from 'payload/errors'
import { i18nInit } from 'payload/utilities'

import type { MongooseAdapter } from '.'

import handleError from './utilities/handleError'
import sanitizeInternalFields from './utilities/sanitizeInternalFields'
import { withSession } from './withSession'

Expand All @@ -31,18 +29,7 @@ export const updateOne: UpdateOne = async function updateOne(
try {
result = await Model.findOneAndUpdate(query, data, options)
} catch (error) {
// Handle uniqueness error from MongoDB
throw error.code === 11000 && error.keyValue
? new ValidationError(
[
{
field: Object.keys(error.keyValue)[0],
message: 'Value must be unique',
},
],
req?.t ?? i18nInit(this.payload.config.i18n).t,
)
: error
handleError(error, req)
}

result = JSON.parse(JSON.stringify(result))
Expand Down
23 changes: 23 additions & 0 deletions packages/db-mongodb/src/utilities/handleError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import httpStatus from 'http-status'
import { APIError, ValidationError } from 'payload/errors'

const handleError = (error, req) => {
// Handle uniqueness error from MongoDB
if (error.code === 11000 && error.keyValue) {
throw new ValidationError(
[
{
field: Object.keys(error.keyValue)[0],
message: req.t('error:valueMustBeUnique'),
},
],
req.t,
)
} else if (error.code === 11000) {
throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST)
} else {
throw error
}
}

export default handleError
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.