@payloadcms/db-mongodb 3.49.0-canary.3 → 3.49.0-canary.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
1
|
+
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAa7C,eAAO,MAAM,WAAW,wCAKrB;IACD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CAC9B,UAgCA,CAAA"}
|
@@ -1,16 +1,31 @@
|
|
1
1
|
import { ValidationError } from 'payload';
|
2
|
+
function extractFieldFromMessage(message) {
|
3
|
+
// eslint-disable-next-line regexp/no-super-linear-backtracking
|
4
|
+
const match = message.match(/index:\s*(.*?)_/);
|
5
|
+
if (match && match[1]) {
|
6
|
+
return match[1] // e.g., returns "email" from "index: email_1"
|
7
|
+
;
|
8
|
+
}
|
9
|
+
return null;
|
10
|
+
}
|
2
11
|
export const handleError = ({ collection, error, global, req })=>{
|
3
12
|
if (!error || typeof error !== 'object') {
|
4
13
|
throw error;
|
5
14
|
}
|
6
15
|
// Handle uniqueness error from MongoDB
|
7
|
-
if ('code' in error && error.code === 11000
|
16
|
+
if ('code' in error && error.code === 11000) {
|
17
|
+
let path = null;
|
18
|
+
if ('keyValue' in error && error.keyValue && typeof error.keyValue === 'object') {
|
19
|
+
path = Object.keys(error.keyValue)[0] ?? '';
|
20
|
+
} else if ('message' in error && typeof error.message === 'string') {
|
21
|
+
path = extractFieldFromMessage(error.message);
|
22
|
+
}
|
8
23
|
throw new ValidationError({
|
9
24
|
collection,
|
10
25
|
errors: [
|
11
26
|
{
|
12
27
|
message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',
|
13
|
-
path:
|
28
|
+
path: path ?? ''
|
14
29
|
}
|
15
30
|
],
|
16
31
|
global
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { ValidationError } from 'payload'\n\nexport const handleError = ({\n collection,\n error,\n global,\n req,\n}: {\n collection?: string\n error: unknown\n global?: string\n req?: Partial<PayloadRequest>\n}) => {\n if (!error || typeof error !== 'object') {\n throw error\n }\n\n // Handle uniqueness error from MongoDB\n if (
|
1
|
+
{"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { ValidationError } from 'payload'\n\nfunction extractFieldFromMessage(message: string) {\n // eslint-disable-next-line regexp/no-super-linear-backtracking\n const match = message.match(/index:\\s*(.*?)_/)\n if (match && match[1]) {\n return match[1] // e.g., returns \"email\" from \"index: email_1\"\n }\n return null\n}\n\nexport const handleError = ({\n collection,\n error,\n global,\n req,\n}: {\n collection?: string\n error: unknown\n global?: string\n req?: Partial<PayloadRequest>\n}) => {\n if (!error || typeof error !== 'object') {\n throw error\n }\n\n // Handle uniqueness error from MongoDB\n if ('code' in error && error.code === 11000) {\n let path: null | string = null\n\n if ('keyValue' in error && error.keyValue && typeof error.keyValue === 'object') {\n path = Object.keys(error.keyValue)[0] ?? ''\n } else if ('message' in error && typeof error.message === 'string') {\n path = extractFieldFromMessage(error.message)\n }\n\n throw new ValidationError(\n {\n collection,\n errors: [\n {\n message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',\n path: path ?? '',\n },\n ],\n global,\n },\n req?.t,\n )\n }\n\n // eslint-disable-next-line @typescript-eslint/only-throw-error\n throw error\n}\n"],"names":["ValidationError","extractFieldFromMessage","message","match","handleError","collection","error","global","req","code","path","keyValue","Object","keys","errors","t"],"mappings":"AAEA,SAASA,eAAe,QAAQ,UAAS;AAEzC,SAASC,wBAAwBC,OAAe;IAC9C,+DAA+D;IAC/D,MAAMC,QAAQD,QAAQC,KAAK,CAAC;IAC5B,IAAIA,SAASA,KAAK,CAAC,EAAE,EAAE;QACrB,OAAOA,KAAK,CAAC,EAAE,CAAC,8CAA8C;;IAChE;IACA,OAAO;AACT;AAEA,OAAO,MAAMC,cAAc,CAAC,EAC1BC,UAAU,EACVC,KAAK,EACLC,MAAM,EACNC,GAAG,EAMJ;IACC,IAAI,CAACF,SAAS,OAAOA,UAAU,UAAU;QACvC,MAAMA;IACR;IAEA,uCAAuC;IACvC,IAAI,UAAUA,SAASA,MAAMG,IAAI,KAAK,OAAO;QAC3C,IAAIC,OAAsB;QAE1B,IAAI,cAAcJ,SAASA,MAAMK,QAAQ,IAAI,OAAOL,MAAMK,QAAQ,KAAK,UAAU;YAC/ED,OAAOE,OAAOC,IAAI,CAACP,MAAMK,QAAQ,CAAC,CAAC,EAAE,IAAI;QAC3C,OAAO,IAAI,aAAaL,SAAS,OAAOA,MAAMJ,OAAO,KAAK,UAAU;YAClEQ,OAAOT,wBAAwBK,MAAMJ,OAAO;QAC9C;QAEA,MAAM,IAAIF,gBACR;YACEK;YACAS,QAAQ;gBACN;oBACEZ,SAASM,KAAKO,IAAIP,IAAIO,CAAC,CAAC,6BAA6B;oBACrDL,MAAMA,QAAQ;gBAChB;aACD;YACDH;QACF,GACAC,KAAKO;IAET;IAEA,+DAA+D;IAC/D,MAAMT;AACR,EAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@payloadcms/db-mongodb",
|
3
|
-
"version": "3.49.0-canary.
|
3
|
+
"version": "3.49.0-canary.5",
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
5
5
|
"homepage": "https://payloadcms.com",
|
6
6
|
"repository": {
|
@@ -50,10 +50,10 @@
|
|
50
50
|
"mongodb": "6.16.0",
|
51
51
|
"mongodb-memory-server": "10.1.4",
|
52
52
|
"@payloadcms/eslint-config": "3.28.0",
|
53
|
-
"payload": "3.49.0-canary.
|
53
|
+
"payload": "3.49.0-canary.5"
|
54
54
|
},
|
55
55
|
"peerDependencies": {
|
56
|
-
"payload": "3.49.0-canary.
|
56
|
+
"payload": "3.49.0-canary.5"
|
57
57
|
},
|
58
58
|
"scripts": {
|
59
59
|
"build": "pnpm build:types && pnpm build:swc",
|