UNPKG

@lucidcms/core

Version:

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

1 lines 3.67 kB
{"version":3,"file":"lucid-api-error.mjs","names":["constants"],"sources":["../../../src/utils/errors/lucid-api-error.ts"],"sourcesContent":["import type z from \"zod\";\nimport constants from \"../../constants/constants.js\";\nimport { copy, translate } from \"../../libs/i18n/index.js\";\nimport type { ErrorResult, LucidErrorData } from \"../../types/errors.js\";\nimport errorTypeDefaults from \"./error-type-defaults.js\";\n\n/**\n * The LucidAPIError class should be used to throw errors within the API request lifecycle. This will be caught by Hono's error handler and will return a formatted error response. If the error is a Zod error, it will be formatted into a more readable format.\n * @class\n * @extends Error\n * @param {LucidErrorData} error\n * @returns {void}\n * @example\n * throw new LucidAPIError({\n * type: \"basic\",\n * name: \"Fetch User Error\",\n * message: \"Error while fetching user data\",\n * status: 500,\n * });\n * @example\n * throw new LucidAPIError({\n * type: \"validation\",\n * name: \"Validation Error\",\n * message: \"Validation error occurred\",\n * status: 400,\n * errors: {\n * email: {\n * code: \"invalid_email\",\n * message: \"Invalid email address\",\n * },\n * },\n * });\n */\nclass LucidAPIError extends Error {\n\terror: LucidErrorData;\n\tconstructor(error: LucidErrorData) {\n\t\tsuper(translate(error.message) ?? constants.errors.message);\n\t\tthis.error = error;\n\n\t\tif (error.zod !== undefined) {\n\t\t\tthis.error.errors = LucidAPIError.formatZodErrors(\n\t\t\t\terror.zod?.issues || [],\n\t\t\t);\n\t\t}\n\n\t\tconst errorTypeRes = errorTypeDefaults(error);\n\n\t\tthis.error.status = errorTypeRes.status;\n\t\tthis.error.name = errorTypeRes.name;\n\t\tthis.error.message = errorTypeRes.message;\n\t\tthis.name = translate(this.error.name) ?? constants.errors.message;\n\t\tthis.message = translate(this.error.message) ?? constants.errors.message;\n\t}\n\t// static\n\tstatic formatZodErrors(error: z.core.$ZodIssue[]) {\n\t\tconst result: ErrorResult = {};\n\n\t\tfor (const item of error) {\n\t\t\tlet current = result;\n\t\t\tfor (const key of item.path) {\n\t\t\t\tif (typeof key === \"number\") {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t// biome-ignore lint/suspicious/noAssignInExpressions: explanation\n\t\t\t\t\tcurrent = current.children || (current.children = []);\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t// biome-ignore lint/suspicious/noAssignInExpressions: explanation\n\t\t\t\t\tcurrent = current[key] || (current[key] = {});\n\t\t\t\t} else {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t// biome-ignore lint/suspicious/noAssignInExpressions: explanation\n\t\t\t\t\tcurrent = current[key] || (current[key] = {});\n\t\t\t\t}\n\t\t\t}\n\t\t\tcurrent.code = item.code;\n\t\t\tcurrent.message = copy.literal(item.message);\n\t\t}\n\n\t\treturn result ?? undefined;\n\t}\n}\n\nexport default LucidAPIError;\n"],"mappings":"gMAiCA,IAAM,EAAN,MAAM,UAAsB,KAAM,CACjC,MACA,YAAY,EAAuB,CAClC,MAAM,EAAU,EAAM,OAAO,GAAKA,EAAU,OAAO,OAAO,EAC1D,KAAK,MAAQ,EAET,EAAM,MAAQ,IAAA,KACjB,KAAK,MAAM,OAAS,EAAc,gBACjC,EAAM,KAAK,QAAU,CAAC,CACvB,GAGD,IAAM,EAAe,EAAkB,CAAK,EAE5C,KAAK,MAAM,OAAS,EAAa,OACjC,KAAK,MAAM,KAAO,EAAa,KAC/B,KAAK,MAAM,QAAU,EAAa,QAClC,KAAK,KAAO,EAAU,KAAK,MAAM,IAAI,GAAKA,EAAU,OAAO,QAC3D,KAAK,QAAU,EAAU,KAAK,MAAM,OAAO,GAAKA,EAAU,OAAO,OAClE,CAEA,OAAO,gBAAgB,EAA2B,CACjD,IAAM,EAAsB,CAAC,EAE7B,IAAK,IAAM,KAAQ,EAAO,CACzB,IAAI,EAAU,EACd,IAAK,IAAM,KAAO,EAAK,KAClB,OAAO,GAAQ,WAGlB,EAAU,AAAqB,EAAQ,WAAW,CAAC,GAOnD,EAAU,EAAQ,KAAS,EAAQ,GAAO,CAAC,GAG7C,EAAQ,KAAO,EAAK,KACpB,EAAQ,QAAU,EAAK,QAAQ,EAAK,OAAO,CAC5C,CAEA,OAAO,GAAU,IAAA,EAClB,CACD"}