@bettergi/utils 0.1.7 → 0.1.8

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/README.md CHANGED
@@ -174,6 +174,20 @@ const d1 = getNextDay4AM();
174
174
  const d2 = getNextMonday4AM();
175
175
  ```
176
176
 
177
+ ### 异常
178
+
179
+ ```ts
180
+ // 重复执行某个可能失败的异步操作,但是发生主机异常(如任务取消)时停止
181
+ for (let i = 0; i < 1000; i++) {
182
+ try {
183
+ await sleep(i);
184
+ } catch (err: any) {
185
+ if (isHostException(err)) throw err;
186
+ log.info(`第 ${i} 次运行失败,错误信息:${err.message}`);
187
+ }
188
+ }
189
+ ```
190
+
177
191
  ### 杂项
178
192
 
179
193
  ```ts
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 判断是否为主机异常
3
+ * @param err 异常对象
4
+ */
5
+ export declare const isHostException: (err: any) => boolean;
6
+ /**
7
+ * 判断是否为任务取消异常
8
+ * @param err 异常对象
9
+ */
10
+ export declare const isTaskCanceledException: (err: any) => any;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 判断是否为主机异常
3
+ * @param err 异常对象
4
+ */
5
+ export const isHostException = (err) => {
6
+ return typeof err === "object" && Object.prototype.hasOwnProperty.call(err, "hostException");
7
+ };
8
+ /**
9
+ * 判断是否为任务取消异常
10
+ * @param err 异常对象
11
+ */
12
+ export const isTaskCanceledException = (err) => {
13
+ return (isHostException(err) &&
14
+ typeof err?.message === "string" &&
15
+ err.message.includes("task was canceled"));
16
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /** 断言 */
2
2
  export * from "./asserts";
3
+ /** 异常 */
4
+ export * from "./exception";
3
5
  /** 文件操作 */
4
6
  export * from "./file";
5
7
  /** 游戏操作 */
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  /** 断言 */
2
2
  export * from "./asserts";
3
+ /** 异常 */
4
+ export * from "./exception";
3
5
  /** 文件操作 */
4
6
  export * from "./file";
5
7
  /** 游戏操作 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",