UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.59 kB
{"version":3,"file":"decode-error.mjs","names":["constants"],"sources":["../../../src/utils/errors/decode-error.ts"],"sourcesContent":["import constants from \"../../constants/constants.js\";\nimport { isTranslatableCopy, translate } from \"../../libs/i18n/index.js\";\nimport type { PublicErrorData } from \"../../types.js\";\nimport { LucidAPIError } from \"./index.js\";\n\nconst translateNestedErrorCopy = (value: unknown): unknown => {\n\tif (isTranslatableCopy(value)) return translate(value);\n\tif (Array.isArray(value)) return value.map(translateNestedErrorCopy);\n\tif (value && typeof value === \"object\") {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(value).map(([key, nested]) => [\n\t\t\t\tkey,\n\t\t\t\ttranslateNestedErrorCopy(nested),\n\t\t\t]),\n\t\t);\n\t}\n\treturn value;\n};\n\nconst decodeError = (error: Error): PublicErrorData => {\n\tif (error instanceof LucidAPIError) {\n\t\treturn {\n\t\t\tname: error.error.name\n\t\t\t\t? translate(error.error.name)\n\t\t\t\t: constants.errors.name,\n\t\t\tmessage: error.error.message\n\t\t\t\t? translate(error.error.message)\n\t\t\t\t: constants.errors.message,\n\t\t\tstatus: error.error.status,\n\t\t\terrors: translateNestedErrorCopy(\n\t\t\t\terror.error.errors,\n\t\t\t) as PublicErrorData[\"errors\"],\n\t\t\tcode: error.error.code,\n\t\t\tkey: error.error.key,\n\t\t};\n\t}\n\n\t// @ts-expect-error\n\tif (error?.statusCode === 429) {\n\t\treturn {\n\t\t\tcode: \"rate_limit\",\n\t\t\tname: translate(\"server:core.rate.limit.error.name\"),\n\t\t\tmessage: error.message || constants.errors.message,\n\t\t\tstatus: 429,\n\t\t};\n\t}\n\n\treturn {\n\t\tname: constants.errors.name,\n\t\tmessage: error.message || constants.errors.message,\n\t\tstatus: constants.errors.status,\n\t\terrors: constants.errors.errors,\n\t\tcode: constants.errors.code,\n\t};\n};\n\nexport default decodeError;\n"],"mappings":"0MAKA,MAAM,EAA4B,GAC7B,EAAmB,CAAK,EAAU,EAAU,CAAK,EACjD,MAAM,QAAQ,CAAK,EAAU,EAAM,IAAI,CAAwB,EAC/D,GAAS,OAAO,GAAU,SACtB,OAAO,YACb,OAAO,QAAQ,CAAK,CAAC,CAAC,KAAK,CAAC,EAAK,KAAY,CAC5C,EACA,EAAyB,CAAM,CAChC,CAAC,CACF,EAEM,EAGF,EAAe,GAChB,aAAiB,EACb,CACN,KAAM,EAAM,MAAM,KACf,EAAU,EAAM,MAAM,IAAI,EAC1BA,EAAU,OAAO,KACpB,QAAS,EAAM,MAAM,QAClB,EAAU,EAAM,MAAM,OAAO,EAC7BA,EAAU,OAAO,QACpB,OAAQ,EAAM,MAAM,OACpB,OAAQ,EACP,EAAM,MAAM,MACb,EACA,KAAM,EAAM,MAAM,KAClB,IAAK,EAAM,MAAM,GAClB,EAIG,GAAO,aAAe,IAClB,CACN,KAAM,aACN,KAAM,EAAU,mCAAmC,EACnD,QAAS,EAAM,SAAWA,EAAU,OAAO,QAC3C,OAAQ,GACT,EAGM,CACN,KAAMA,EAAU,OAAO,KACvB,QAAS,EAAM,SAAWA,EAAU,OAAO,QAC3C,OAAQA,EAAU,OAAO,OACzB,OAAQA,EAAU,OAAO,OACzB,KAAMA,EAAU,OAAO,IACxB"}