Skip to content

Commit 9043b10

Browse files
authored
fix(db-mongodb): incorrect errors logging due to invalid logic in handleError (#10575)
Previously, every error from MongoDB was logged as "Value must be unique", as well the response code should not be `BAD_REQUEST` but `INTERNAL_SERVER_ERROR`. `throw error` preserves the original error so it can be traced.
1 parent ecf0572 commit 9043b10

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

packages/db-mongodb/src/utilities/handleError.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { PayloadRequest } from 'payload'
22

3-
import httpStatus from 'http-status'
4-
import { APIError, ValidationError } from 'payload'
3+
import { ValidationError } from 'payload'
54

65
export const handleError = ({
76
collection,
@@ -10,18 +9,17 @@ export const handleError = ({
109
req,
1110
}: {
1211
collection?: string
13-
error: unknown
12+
error: Error
1413
global?: string
1514
req?: Partial<PayloadRequest>
1615
}) => {
1716
if (!error || typeof error !== 'object') {
1817
throw error
1918
}
2019

21-
const message = req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique'
22-
2320
// Handle uniqueness error from MongoDB
2421
if ('code' in error && error.code === 11000 && 'keyValue' in error && error.keyValue) {
22+
const message = req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique'
2523
throw new ValidationError(
2624
{
2725
collection,
@@ -37,5 +35,5 @@ export const handleError = ({
3735
)
3836
}
3937

40-
throw new APIError(message, httpStatus.BAD_REQUEST)
38+
throw error
4139
}

0 commit comments

Comments
 (0)