@pori15/logixlysia 6.0.15 → 6.0.16

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pori15/logixlysia",
3
- "version": "6.0.15",
3
+ "version": "6.0.16",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "exports": {
package/src/index.ts CHANGED
@@ -123,7 +123,8 @@ export type { Code } from "./error/type";
123
123
  // Utils Exports
124
124
  // ==========================================
125
125
 
126
- export { getErrorCode } from "./utils/get-error-code";
126
+ export type { ErrorMeta } from "./utils/get-error-code";
127
+ export { getErrorCode, getErrorMeta } from "./utils/get-error-code";
127
128
  export { normalizeToProblem } from "./utils/handle-error";
128
129
 
129
130
  // ==========================================
package/src/interfaces.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  } from "pino";
5
5
  import type { ProblemError } from "./error/errors";
6
6
  import type { Code } from "./error/type";
7
+ import type { ErrorMeta } from "./utils/get-error-code";
7
8
 
8
9
  /** Pino Logger 实例类型 */
9
10
  export type Pino = PinoLogger<never, boolean>;
@@ -64,8 +65,8 @@ export interface LogRotationConfig {
64
65
 
65
66
  /** 错误码到 HTTP 响应的映射条目 */
66
67
  export interface ErrorMapping {
67
- /** 错误详情描述 */
68
- detail?: string;
68
+ /** 错误详情,支持静态文案或根据数据库错误元数据动态生成 */
69
+ detail?: string | ((meta: ErrorMeta) => string);
69
70
  /** HTTP 状态码 */
70
71
  status: number;
71
72
  /** 错误标题 */
@@ -21,3 +21,56 @@ export const getErrorCode = (error: unknown): string | undefined => {
21
21
 
22
22
  return undefined;
23
23
  };
24
+
25
+ /** 从数据库错误中提取的结构化元数据 */
26
+ export interface ErrorMeta {
27
+ /** 错误码(如 Postgres "23505") */
28
+ code: string;
29
+ /** 列名(部分驱动提供) */
30
+ column?: string;
31
+ /** 约束名(如 "uk_site_product_slug") */
32
+ constraint?: string;
33
+ /** 数据库原生详情(如 "Key (slug)=(xxx) already exists.") */
34
+ detail?: string;
35
+ /** 表名 */
36
+ table?: string;
37
+ }
38
+
39
+ /**
40
+ * 递归提取数据库错误的元数据
41
+ * 从 Postgres / MySQL 等驱动的错误对象中提取 constraint、table、column、detail 等字段
42
+ */
43
+ export const getErrorMeta = (error: unknown): ErrorMeta | undefined => {
44
+ if (typeof error !== "object" || error === null) return undefined;
45
+
46
+ const obj = error as Record<string, unknown>;
47
+
48
+ if ("code" in obj && typeof obj.code === "string") {
49
+ return {
50
+ code: obj.code,
51
+ column:
52
+ "column" in obj && typeof obj.column === "string"
53
+ ? obj.column
54
+ : undefined,
55
+ constraint:
56
+ "constraint" in obj && typeof obj.constraint === "string"
57
+ ? obj.constraint
58
+ : undefined,
59
+ detail:
60
+ "detail" in obj && typeof obj.detail === "string"
61
+ ? obj.detail
62
+ : undefined,
63
+ table:
64
+ "table_name" in obj && typeof obj.table_name === "string"
65
+ ? obj.table_name
66
+ : "table" in obj && typeof obj.table === "string"
67
+ ? obj.table
68
+ : undefined,
69
+ };
70
+ }
71
+
72
+ if ("cause" in obj) return getErrorMeta(obj.cause);
73
+ if ("error" in obj) return getErrorMeta(obj.error);
74
+
75
+ return undefined;
76
+ };
@@ -17,7 +17,7 @@ import {
17
17
  } from "../error/errors";
18
18
  import type { Code } from "../error/type";
19
19
  import type { ErrorMapping, ErrorResolver } from "../interfaces";
20
- import { getErrorCode } from "./get-error-code";
20
+ import { getErrorCode, getErrorMeta } from "./get-error-code";
21
21
 
22
22
  // Layer 5: Elysia 内置错误码映射
23
23
  const CODE_MAP: Record<
@@ -98,15 +98,19 @@ export const normalizeToProblem = (
98
98
  }
99
99
 
100
100
  // ==========================================
101
- // Layer 4: errorMap 查表(递归 getErrorCode)
101
+ // Layer 4: errorMap 查表(递归 getErrorCode / getErrorMeta
102
102
  // ==========================================
103
103
  if (errorMap) {
104
104
  const errorCode = getErrorCode(error);
105
105
  if (errorCode && errorCode in errorMap) {
106
106
  const mapping = errorMap[errorCode];
107
+ const detail =
108
+ typeof mapping.detail === "function"
109
+ ? mapping.detail(getErrorMeta(error) ?? { code: errorCode })
110
+ : mapping.detail;
107
111
  return createProblem(mapping.status, {
108
112
  title: mapping.title,
109
- detail: mapping.detail,
113
+ detail,
110
114
  type: buildType(errorCode),
111
115
  instance: path,
112
116
  });
File without changes
File without changes