@cloudcome/utils-uni 1.41.0 → 1.43.0
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/dist/_helpers.cjs +18 -0
- package/dist/_helpers.cjs.map +1 -1
- package/dist/_helpers.d.ts +11 -0
- package/dist/_helpers.mjs +13 -1
- package/dist/_helpers.mjs.map +1 -1
- package/dist/client.cjs +1 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.mjs +2 -2
- package/dist/cloud.cjs +1 -0
- package/dist/cloud.d.ts +1 -1
- package/dist/cloud.mjs +2 -2
- package/dist/database/_db.class.d.ts +16 -4
- package/dist/database/error.d.ts +26 -0
- package/dist/database/proxy.d.ts +4 -4
- package/dist/database.cjs +70 -10
- package/dist/database.cjs.map +1 -1
- package/dist/database.d.ts +2 -1
- package/dist/database.mjs +68 -12
- package/dist/database.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/weixin/notice.d.ts +5 -5
- package/dist/weixin/token.d.ts +3 -3
- package/dist/weixin.cjs +11 -11
- package/dist/weixin.cjs.map +1 -1
- package/dist/weixin.mjs +10 -10
- package/dist/weixin.mjs.map +1 -1
- package/package.json +3 -3
package/dist/_helpers.cjs
CHANGED
|
@@ -28,7 +28,25 @@ function parseDatabaseOutput(res) {
|
|
|
28
28
|
}
|
|
29
29
|
return res;
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* 检查给定的未知值是否为 UniError 类型。
|
|
33
|
+
*
|
|
34
|
+
* 该函数作为 TypeScript 的类型守卫(Type Guard),在运行时验证传入的对象是否为 Error 的实例,
|
|
35
|
+
* 并在类型系统中将未知类型(unknown)收窄为 UniError 类型。
|
|
36
|
+
*
|
|
37
|
+
* @param {unknown} err - 需要检查的未知值。
|
|
38
|
+
* @returns {boolean} 如果传入的值是 Error 的实例,则返回 true,否则返回 false。
|
|
39
|
+
*/
|
|
40
|
+
function isUniError(err) {
|
|
41
|
+
return err instanceof Error;
|
|
42
|
+
}
|
|
31
43
|
//#endregion
|
|
44
|
+
Object.defineProperty(exports, "isUniError", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function() {
|
|
47
|
+
return isUniError;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
32
50
|
Object.defineProperty(exports, "parseCloudMethodOutput", {
|
|
33
51
|
enumerable: true,
|
|
34
52
|
get: function() {
|
package/dist/_helpers.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_helpers.cjs","names":[],"sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n"],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"_helpers.cjs","names":[],"sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\nimport type { UniError } from './_types';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n\n/**\n * 检查给定的未知值是否为 UniError 类型。\n *\n * 该函数作为 TypeScript 的类型守卫(Type Guard),在运行时验证传入的对象是否为 Error 的实例,\n * 并在类型系统中将未知类型(unknown)收窄为 UniError 类型。\n *\n * @param {unknown} err - 需要检查的未知值。\n * @returns {boolean} 如果传入的值是 Error 的实例,则返回 true,否则返回 false。\n */\nexport function isUniError(err: unknown): err is UniError {\n return err instanceof Error;\n}\n"],"mappings":";;;;;;;;;;;;AAeA,SAAgB,uBAA0B,QAA8B,uBAAuB,IAAO;CACpG,IAAI,OAAO,SACT,OAAA,GAAA,4BAAA,aAAkB,IAAI,MAAM,OAAO,UAAU,oBAAoB,GAAG,MAAM;CAG5E,OAAO,OAAO;AAChB;;;;;;AAOA,SAAgB,oBAAuB,KAAuD;CAC5F,MAAM,OAAO,OAAO,KAAK,GAAgB;CAIzC,IAFiB,KAAK,WAAW,KAAK,KAAK,OAAO,UAEpC;EACZ,MAAM,EAAE,WAAW;EACnB,IAAI,CAAC,OAAO,SAAS,QAAA,GAAA,6BAAA,YAAkB,QAAQ,CAAC,WAAW,QAAQ,CAAC;EACpE,OAAA,GAAA,4BAAA,aAAkB,IAAI,MAAM,OAAO,MAAM,GAAG,MAAM;CACpD;CAGA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,WAAW,KAA+B;CACxD,OAAO,eAAe;AACxB"}
|
package/dist/_helpers.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CloudMethodOutput } from './cloud';
|
|
2
2
|
import { ClientDatabaseOutput, CloudDatabaseOutput } from './database';
|
|
3
|
+
import { UniError } from './_types';
|
|
3
4
|
/**
|
|
4
5
|
* 解析云对象方法调用的输出结果
|
|
5
6
|
*
|
|
@@ -16,3 +17,13 @@ export declare function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>,
|
|
|
16
17
|
* @returns 处理后的结果
|
|
17
18
|
*/
|
|
18
19
|
export declare function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>): T | Omit<T & import('./_types').UniErrorData, "errCode" | "errMsg">;
|
|
20
|
+
/**
|
|
21
|
+
* 检查给定的未知值是否为 UniError 类型。
|
|
22
|
+
*
|
|
23
|
+
* 该函数作为 TypeScript 的类型守卫(Type Guard),在运行时验证传入的对象是否为 Error 的实例,
|
|
24
|
+
* 并在类型系统中将未知类型(unknown)收窄为 UniError 类型。
|
|
25
|
+
*
|
|
26
|
+
* @param {unknown} err - 需要检查的未知值。
|
|
27
|
+
* @returns {boolean} 如果传入的值是 Error 的实例,则返回 true,否则返回 false。
|
|
28
|
+
*/
|
|
29
|
+
export declare function isUniError(err: unknown): err is UniError;
|
package/dist/_helpers.mjs
CHANGED
|
@@ -28,7 +28,19 @@ function parseDatabaseOutput(res) {
|
|
|
28
28
|
}
|
|
29
29
|
return res;
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* 检查给定的未知值是否为 UniError 类型。
|
|
33
|
+
*
|
|
34
|
+
* 该函数作为 TypeScript 的类型守卫(Type Guard),在运行时验证传入的对象是否为 Error 的实例,
|
|
35
|
+
* 并在类型系统中将未知类型(unknown)收窄为 UniError 类型。
|
|
36
|
+
*
|
|
37
|
+
* @param {unknown} err - 需要检查的未知值。
|
|
38
|
+
* @returns {boolean} 如果传入的值是 Error 的实例,则返回 true,否则返回 false。
|
|
39
|
+
*/
|
|
40
|
+
function isUniError(err) {
|
|
41
|
+
return err instanceof Error;
|
|
42
|
+
}
|
|
31
43
|
//#endregion
|
|
32
|
-
export {
|
|
44
|
+
export { parseCloudMethodOutput as n, parseDatabaseOutput as r, isUniError as t };
|
|
33
45
|
|
|
34
46
|
//# sourceMappingURL=_helpers.mjs.map
|
package/dist/_helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_helpers.mjs","names":[],"sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n"],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"_helpers.mjs","names":[],"sources":["../src/_helpers.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\nimport { objectOmit } from '@cloudcome/utils-core/object';\nimport type { CloudMethodOutput } from './cloud';\nimport type { ClientDatabaseOutput, CloudDatabaseOutput } from './database';\nimport type { UniError } from './_types';\n\n/**\n * 解析云对象方法调用的输出结果\n *\n * @template O - 云对象方法返回数据的类型\n * @param output - 云对象方法调用的输出结果\n * @param fallbackErrorMessage - 当输出中没有错误信息时使用的默认错误消息\n * @returns 云对象方法返回成功时的数据部分\n * @throws 当云对象方法调用失败时,抛出包含错误信息的异常\n */\nexport function parseCloudMethodOutput<O>(output: CloudMethodOutput<O>, fallbackErrorMessage = ''): O {\n if (output.errCode) {\n throw errorAssign(new Error(output.errMsg || fallbackErrorMessage), output);\n }\n\n return output.data;\n}\n\n/**\n * 解析数据库执行结果\n * @param res 客户端、云端响应结果\n * @returns 处理后的结果\n */\nexport function parseDatabaseOutput<T>(res: ClientDatabaseOutput<T> | CloudDatabaseOutput<T>) {\n const keys = Object.keys(res as AnyObject);\n // 客户端 { result: {errCode: 0, errMsg: 'ok'} & 数据 }\n const isClient = keys.length === 1 && keys[0] === 'result';\n\n if (isClient) {\n const { result } = res as ClientDatabaseOutput<T>;\n if (!result.errCode) return objectOmit(result, ['errCode', 'errMsg']);\n throw errorAssign(new Error(result.errMsg), result);\n }\n\n // 云端 数据\n return res as T;\n}\n\n/**\n * 检查给定的未知值是否为 UniError 类型。\n *\n * 该函数作为 TypeScript 的类型守卫(Type Guard),在运行时验证传入的对象是否为 Error 的实例,\n * 并在类型系统中将未知类型(unknown)收窄为 UniError 类型。\n *\n * @param {unknown} err - 需要检查的未知值。\n * @returns {boolean} 如果传入的值是 Error 的实例,则返回 true,否则返回 false。\n */\nexport function isUniError(err: unknown): err is UniError {\n return err instanceof Error;\n}\n"],"mappings":";;;;;;;;;;;;AAeA,SAAgB,uBAA0B,QAA8B,uBAAuB,IAAO;CACpG,IAAI,OAAO,SACT,MAAM,YAAY,IAAI,MAAM,OAAO,UAAU,oBAAoB,GAAG,MAAM;CAG5E,OAAO,OAAO;AAChB;;;;;;AAOA,SAAgB,oBAAuB,KAAuD;CAC5F,MAAM,OAAO,OAAO,KAAK,GAAgB;CAIzC,IAFiB,KAAK,WAAW,KAAK,KAAK,OAAO,UAEpC;EACZ,MAAM,EAAE,WAAW;EACnB,IAAI,CAAC,OAAO,SAAS,OAAO,WAAW,QAAQ,CAAC,WAAW,QAAQ,CAAC;EACpE,MAAM,YAAY,IAAI,MAAM,OAAO,MAAM,GAAG,MAAM;CACpD;CAGA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,WAAW,KAA+B;CACxD,OAAO,eAAe;AACxB"}
|
package/dist/client.cjs
CHANGED
|
@@ -426,6 +426,7 @@ async function querySelectorRects(instance, selector) {
|
|
|
426
426
|
}
|
|
427
427
|
//#endregion
|
|
428
428
|
exports.importCloudObject = importCloudObject;
|
|
429
|
+
exports.isUniError = require__helpers.isUniError;
|
|
429
430
|
exports.parseCloudMethodOutput = require__helpers.parseCloudMethodOutput;
|
|
430
431
|
exports.querySelectorRects = querySelectorRects;
|
|
431
432
|
exports.uniAlert = uniAlert;
|
package/dist/client.d.ts
CHANGED
package/dist/client.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as parseCloudMethodOutput, t as isUniError } from "./_helpers.mjs";
|
|
2
2
|
import { errorAssign } from "@cloudcome/utils-core/error";
|
|
3
3
|
import { _runLifeHook } from "@cloudcome/utils-vue/shared";
|
|
4
4
|
import { onHide, onLoad, onPageHide, onPageShow, onShow, onUnload } from "@dcloudio/uni-app";
|
|
@@ -424,6 +424,6 @@ async function querySelectorRects(instance, selector) {
|
|
|
424
424
|
});
|
|
425
425
|
}
|
|
426
426
|
//#endregion
|
|
427
|
-
export { importCloudObject, parseCloudMethodOutput, querySelectorRects, uniAlert, uniCallback, uniConfirm, uniLoading, uniPromise, uniPrompt, uniSubscribeNotice, uniToast, useAppShow, useAppUpdate, useDatabase, usePageLoad, usePageQuery, usePageShow };
|
|
427
|
+
export { importCloudObject, isUniError, parseCloudMethodOutput, querySelectorRects, uniAlert, uniCallback, uniConfirm, uniLoading, uniPromise, uniPrompt, uniSubscribeNotice, uniToast, useAppShow, useAppUpdate, useDatabase, usePageLoad, usePageQuery, usePageShow };
|
|
428
428
|
|
|
429
429
|
//# sourceMappingURL=client.mjs.map
|
package/dist/cloud.cjs
CHANGED
|
@@ -233,6 +233,7 @@ async function request(options) {
|
|
|
233
233
|
//#endregion
|
|
234
234
|
exports.buildCloudMethodCreator = buildCloudMethodCreator;
|
|
235
235
|
exports.createCloudObjectError = createCloudObjectError;
|
|
236
|
+
exports.isUniError = require__helpers.isUniError;
|
|
236
237
|
exports.parseCloudMethodOutput = require__helpers.parseCloudMethodOutput;
|
|
237
238
|
exports.parseCloudModuleOutput = parseCloudModuleOutput;
|
|
238
239
|
exports.request = request;
|
package/dist/cloud.d.ts
CHANGED
package/dist/cloud.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as parseCloudMethodOutput, t as isUniError } from "./_helpers.mjs";
|
|
2
2
|
import { errorAssign, errorNormalize } from "@cloudcome/utils-core/error";
|
|
3
3
|
import { objectDefaults, objectOmit } from "@cloudcome/utils-core/object";
|
|
4
4
|
import { isFunction } from "@cloudcome/utils-core/type";
|
|
@@ -230,6 +230,6 @@ async function request(options) {
|
|
|
230
230
|
});
|
|
231
231
|
}
|
|
232
232
|
//#endregion
|
|
233
|
-
export { buildCloudMethodCreator, createCloudObjectError, parseCloudMethodOutput, parseCloudModuleOutput, request, respondCloudMethod };
|
|
233
|
+
export { buildCloudMethodCreator, createCloudObjectError, isUniError, parseCloudMethodOutput, parseCloudModuleOutput, request, respondCloudMethod };
|
|
234
234
|
|
|
235
235
|
//# sourceMappingURL=cloud.mjs.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AnyObject, MergeIntersection } from '@cloudcome/utils-core/types';
|
|
2
1
|
import { UniError } from '../_types';
|
|
2
|
+
import { AnyObject, MergeIntersection } from '@cloudcome/utils-core/types';
|
|
3
3
|
import { DbQueryCommand } from './_command.class';
|
|
4
4
|
import { DbCreate, DbForeign, DbOrder, DbQuery, DbRelation, DbSelect, DbUpdate, DbWhere } from './types';
|
|
5
5
|
export type DbOptions = {
|
|
@@ -17,10 +17,10 @@ export type DbOptions = {
|
|
|
17
17
|
_mockDatabase?: any;
|
|
18
18
|
/**
|
|
19
19
|
* 自定义错误处理函数
|
|
20
|
-
* @param error
|
|
21
|
-
* @returns
|
|
20
|
+
* @param error unknown 数据库异常对象
|
|
21
|
+
* @returns 自定义错误对象
|
|
22
22
|
*/
|
|
23
|
-
parseError?: (error:
|
|
23
|
+
parseError?: (error: unknown) => UniError;
|
|
24
24
|
};
|
|
25
25
|
export type DbLookupOptions<RL extends DbRelation, D1, FD1, AS, US extends boolean | undefined | void = undefined> = {
|
|
26
26
|
/**
|
|
@@ -156,6 +156,18 @@ export declare class Db<D1, S1 extends DbSelect<D1> = {}, D2 extends AnyObject =
|
|
|
156
156
|
private _lookupAs;
|
|
157
157
|
private _endAggregate;
|
|
158
158
|
private _endHost;
|
|
159
|
+
/**
|
|
160
|
+
* 将 uniCloud 数据库原始错误包装为 DbError
|
|
161
|
+
* @param err - 原始错误对象,来自 uniCloud DB 操作(Error 实例,含 errMsg、errCode 属性)
|
|
162
|
+
* @returns DbError 实例
|
|
163
|
+
*/
|
|
164
|
+
private _parseDbError;
|
|
165
|
+
/**
|
|
166
|
+
* 统一处理 DB 操作抛出的错误
|
|
167
|
+
* - uniCloud 数据库错误(含 errMsg)→ 包装为 DbError → 经 parseError 回调后抛出
|
|
168
|
+
* - 非数据库错误(如网络中断)→ 原样抛出
|
|
169
|
+
*/
|
|
170
|
+
private _handleDbError;
|
|
159
171
|
/**
|
|
160
172
|
* 执行查询操作
|
|
161
173
|
* @returns 查询结果
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数据库底层异常类
|
|
3
|
+
* 当 uniCloud 数据库操作抛出错误时,统一包装为该异常
|
|
4
|
+
*/
|
|
5
|
+
export declare class DbError extends Error {
|
|
6
|
+
/** 原始错误码,如 'InternalServerError' */
|
|
7
|
+
errCode: string | number;
|
|
8
|
+
/** MongoDB 错误码,如 'E11000'。不匹配则为空字符串 */
|
|
9
|
+
dbCode: string;
|
|
10
|
+
constructor(message: string, extra: {
|
|
11
|
+
errCode: string | number;
|
|
12
|
+
dbCode: string;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 判断错误是否为数据库底层错误
|
|
17
|
+
* @param err - 任意错误对象
|
|
18
|
+
* @returns 是否为 DbError
|
|
19
|
+
*/
|
|
20
|
+
export declare function isDbError(err: unknown): err is DbError;
|
|
21
|
+
/**
|
|
22
|
+
* 从 MongoDB 错误消息中提取错误码
|
|
23
|
+
* @param errMsg - 错误消息字符串,如 'E11000 duplicate key error...'
|
|
24
|
+
* @returns MongoDB 错误码,如 'E11000'
|
|
25
|
+
*/
|
|
26
|
+
export declare function extractMongoCode(errMsg: string): string;
|
package/dist/database/proxy.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { UniError } from '../_types';
|
|
2
1
|
import { Db } from './_db.class';
|
|
3
2
|
import { DbSelect } from './types';
|
|
3
|
+
import { UniError } from '../_types';
|
|
4
4
|
export type DbProxyOptions = {
|
|
5
5
|
/**
|
|
6
6
|
* 自定义错误处理函数
|
|
7
|
-
* @param error
|
|
8
|
-
* @returns
|
|
7
|
+
* @param error unknown 数据库异常对象
|
|
8
|
+
* @returns 自定义错误对象
|
|
9
9
|
*/
|
|
10
|
-
parseError?: (error:
|
|
10
|
+
parseError?: (error: unknown) => UniError;
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
13
|
* 创建一个数据库代理对象,用于延迟实例化数据库操作类
|
package/dist/database.cjs
CHANGED
|
@@ -204,6 +204,41 @@ async function dbEach(table, where, iterator, maxCount = Number.MAX_SAFE_INTEGER
|
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
206
|
//#endregion
|
|
207
|
+
//#region src/database/error.ts
|
|
208
|
+
/**
|
|
209
|
+
* 数据库底层异常类
|
|
210
|
+
* 当 uniCloud 数据库操作抛出错误时,统一包装为该异常
|
|
211
|
+
*/
|
|
212
|
+
var DbError = class extends Error {
|
|
213
|
+
/** 原始错误码,如 'InternalServerError' */
|
|
214
|
+
errCode;
|
|
215
|
+
/** MongoDB 错误码,如 'E11000'。不匹配则为空字符串 */
|
|
216
|
+
dbCode;
|
|
217
|
+
constructor(message, extra) {
|
|
218
|
+
super(message);
|
|
219
|
+
this.name = "DbError";
|
|
220
|
+
this.errCode = extra.errCode;
|
|
221
|
+
this.dbCode = extra.dbCode;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* 判断错误是否为数据库底层错误
|
|
226
|
+
* @param err - 任意错误对象
|
|
227
|
+
* @returns 是否为 DbError
|
|
228
|
+
*/
|
|
229
|
+
function isDbError(err) {
|
|
230
|
+
return err instanceof DbError;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 从 MongoDB 错误消息中提取错误码
|
|
234
|
+
* @param errMsg - 错误消息字符串,如 'E11000 duplicate key error...'
|
|
235
|
+
* @returns MongoDB 错误码,如 'E11000'
|
|
236
|
+
*/
|
|
237
|
+
function extractMongoCode(errMsg) {
|
|
238
|
+
const m = errMsg.match(/^E\d+/);
|
|
239
|
+
return m ? m[0] : "";
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
207
242
|
//#region src/database/paging.ts
|
|
208
243
|
/**
|
|
209
244
|
* 数据库分页查询函数
|
|
@@ -464,6 +499,32 @@ var Db = class Db {
|
|
|
464
499
|
return hostRef;
|
|
465
500
|
}
|
|
466
501
|
/**
|
|
502
|
+
* 将 uniCloud 数据库原始错误包装为 DbError
|
|
503
|
+
* @param err - 原始错误对象,来自 uniCloud DB 操作(Error 实例,含 errMsg、errCode 属性)
|
|
504
|
+
* @returns DbError 实例
|
|
505
|
+
*/
|
|
506
|
+
_parseDbError(err) {
|
|
507
|
+
const errCode = err.errCode || "";
|
|
508
|
+
const message = err.errMsg || err.message;
|
|
509
|
+
const dbCode = extractMongoCode(err.errMsg || err.message);
|
|
510
|
+
if (dbCode) return new DbError(message, {
|
|
511
|
+
errCode,
|
|
512
|
+
dbCode
|
|
513
|
+
});
|
|
514
|
+
else return err;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* 统一处理 DB 操作抛出的错误
|
|
518
|
+
* - uniCloud 数据库错误(含 errMsg)→ 包装为 DbError → 经 parseError 回调后抛出
|
|
519
|
+
* - 非数据库错误(如网络中断)→ 原样抛出
|
|
520
|
+
*/
|
|
521
|
+
_handleDbError(err) {
|
|
522
|
+
if (require__helpers.isUniError(err)) {
|
|
523
|
+
const dbErr = this._parseDbError(err);
|
|
524
|
+
throw this._options.parseError?.(dbErr) || dbErr;
|
|
525
|
+
} else throw this._options.parseError?.(err) || err;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
467
528
|
* 执行查询操作
|
|
468
529
|
* @returns 查询结果
|
|
469
530
|
*/
|
|
@@ -482,8 +543,7 @@ var Db = class Db {
|
|
|
482
543
|
const { data } = require__helpers.parseDatabaseOutput(res);
|
|
483
544
|
return data;
|
|
484
545
|
} catch (err) {
|
|
485
|
-
|
|
486
|
-
throw this._options.parseError?.(dbErr) || dbErr;
|
|
546
|
+
this._handleDbError(err);
|
|
487
547
|
}
|
|
488
548
|
}
|
|
489
549
|
/**
|
|
@@ -525,8 +585,7 @@ var Db = class Db {
|
|
|
525
585
|
const { total } = require__helpers.parseDatabaseOutput(await hostRef.count());
|
|
526
586
|
return total;
|
|
527
587
|
} catch (err) {
|
|
528
|
-
|
|
529
|
-
throw this._options.parseError?.(dbErr) || dbErr;
|
|
588
|
+
this._handleDbError(err);
|
|
530
589
|
}
|
|
531
590
|
}
|
|
532
591
|
/**
|
|
@@ -547,8 +606,7 @@ var Db = class Db {
|
|
|
547
606
|
const { id } = require__helpers.parseDatabaseOutput(await hostRef.add(data));
|
|
548
607
|
return id;
|
|
549
608
|
} catch (err) {
|
|
550
|
-
|
|
551
|
-
throw this._options.parseError?.(dbErr) || dbErr;
|
|
609
|
+
this._handleDbError(err);
|
|
552
610
|
}
|
|
553
611
|
}
|
|
554
612
|
/**
|
|
@@ -570,8 +628,7 @@ var Db = class Db {
|
|
|
570
628
|
const { updated } = require__helpers.parseDatabaseOutput(await hostRef.update((0, _cloudcome_utils_core_object.objectOmit)(_mapCommandRaw(data), ["_id"])));
|
|
571
629
|
return updated;
|
|
572
630
|
} catch (err) {
|
|
573
|
-
|
|
574
|
-
throw this._options.parseError?.(dbErr) || dbErr;
|
|
631
|
+
this._handleDbError(err);
|
|
575
632
|
}
|
|
576
633
|
}
|
|
577
634
|
/**
|
|
@@ -592,8 +649,7 @@ var Db = class Db {
|
|
|
592
649
|
const { deleted } = require__helpers.parseDatabaseOutput(await hostRef.remove());
|
|
593
650
|
return deleted;
|
|
594
651
|
} catch (err) {
|
|
595
|
-
|
|
596
|
-
throw this._options.parseError?.(dbErr) || dbErr;
|
|
652
|
+
this._handleDbError(err);
|
|
597
653
|
}
|
|
598
654
|
}
|
|
599
655
|
};
|
|
@@ -726,6 +782,7 @@ async function dbUnique(db, options) {
|
|
|
726
782
|
};
|
|
727
783
|
}
|
|
728
784
|
//#endregion
|
|
785
|
+
exports.DbError = DbError;
|
|
729
786
|
exports.dbEach = dbEach;
|
|
730
787
|
exports.dbMutate = dbMutate;
|
|
731
788
|
exports.dbPaging = dbPaging;
|
|
@@ -734,6 +791,9 @@ exports.dbQuery = dbQuery;
|
|
|
734
791
|
exports.dbTransaction = dbTransaction;
|
|
735
792
|
exports.dbUnique = dbUnique;
|
|
736
793
|
exports.dbUpsert = dbUpsert;
|
|
794
|
+
exports.extractMongoCode = extractMongoCode;
|
|
795
|
+
exports.isDbError = isDbError;
|
|
796
|
+
exports.isUniError = require__helpers.isUniError;
|
|
737
797
|
exports.parseDatabaseOutput = require__helpers.parseDatabaseOutput;
|
|
738
798
|
|
|
739
799
|
//# sourceMappingURL=database.cjs.map
|