@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 +14 -0
- package/dist/exception.d.ts +10 -0
- package/dist/exception.js +16 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
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,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
package/dist/index.js
CHANGED