spooder 4.2.0 → 4.2.1
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 +25 -0
- package/package.json +1 -1
- package/src/api.d.ts +3 -0
- package/src/api.ts +14 -0
package/README.md
CHANGED
|
@@ -430,6 +430,7 @@ In addition to the information provided by the developer, `spooder` also include
|
|
|
430
430
|
- [`ErrorWithMetadata(message: string, metadata: object)`](#api-error-handling-error-with-metadata)
|
|
431
431
|
- [`caution(err_message_or_obj: string | object, ...err: object[]): Promise<void>`](#api-error-handling-caution)
|
|
432
432
|
- [`panic(err_message_or_obj: string | object, ...err: object[]): Promise<void>`](#api-error-handling-panic)
|
|
433
|
+
- [`safe(fn: Callable): Promise<void>`](#api-error-handling-safe)
|
|
433
434
|
- [API > Content](#api-content)
|
|
434
435
|
- [`parse_template(template: string, replacements: Record<string, string>): string`](#api-content-parse-template)
|
|
435
436
|
- [`generate_hash_subs(length: number, prefix: string): Promise<Record<string, string>>`](#api-content-generate-hash-subs)
|
|
@@ -872,6 +873,30 @@ try {
|
|
|
872
873
|
}
|
|
873
874
|
```
|
|
874
875
|
|
|
876
|
+
<a id="api-error-handling-safe"></a>
|
|
877
|
+
### 🔧 `safe(fn: Callable): Promise<void>`
|
|
878
|
+
|
|
879
|
+
`safe()` is a utility function that wraps a "callable" and calls `caution()` if it throws an error.
|
|
880
|
+
|
|
881
|
+
> ![NOTE]
|
|
882
|
+
> This utility is primarily intended to be used to reduce boilerplate for fire-and-forget functions that you want to be notified about if they fail.
|
|
883
|
+
|
|
884
|
+
```ts
|
|
885
|
+
safe(async (() => {
|
|
886
|
+
// This code will run async and any errors will invoke caution().
|
|
887
|
+
});
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
`safe()` supports both async and sync callables, as well as Promise objects. `safe()` can also used with `await`.
|
|
891
|
+
|
|
892
|
+
```ts
|
|
893
|
+
await safe(() => {
|
|
894
|
+
return new Promise((resolve, reject) => {
|
|
895
|
+
// Do stuff.
|
|
896
|
+
});
|
|
897
|
+
});
|
|
898
|
+
```
|
|
899
|
+
|
|
875
900
|
<a id="api-content"></a>
|
|
876
901
|
## API > Content
|
|
877
902
|
|
package/package.json
CHANGED
package/src/api.d.ts
CHANGED
|
@@ -15,6 +15,9 @@ export declare class ErrorWithMetadata extends Error {
|
|
|
15
15
|
}
|
|
16
16
|
export declare function panic(err_message_or_obj: string | object, ...err: object[]): Promise<void>;
|
|
17
17
|
export declare function caution(err_message_or_obj: string | object, ...err: object[]): Promise<void>;
|
|
18
|
+
type CallableFunction = (...args: any[]) => any;
|
|
19
|
+
type Callable = Promise<any> | CallableFunction;
|
|
20
|
+
export declare function safe(target_fn: Callable): Promise<void>;
|
|
18
21
|
export declare function parse_template(template: string, replacements: Record<string, string | Array<string>>): string;
|
|
19
22
|
export declare function generate_hash_subs(length?: number, prefix?: string): Promise<Record<string, string>>;
|
|
20
23
|
type CookieOptions = {
|
package/src/api.ts
CHANGED
|
@@ -89,6 +89,20 @@ export async function caution(err_message_or_obj: string | object, ...err: objec
|
|
|
89
89
|
await handle_error('caution: ', err_message_or_obj, ...err);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
type CallableFunction = (...args: any[]) => any;
|
|
93
|
+
type Callable = Promise<any> | CallableFunction;
|
|
94
|
+
|
|
95
|
+
export async function safe(target_fn: Callable) {
|
|
96
|
+
try {
|
|
97
|
+
if (target_fn instanceof Promise)
|
|
98
|
+
await target_fn;
|
|
99
|
+
else
|
|
100
|
+
await target_fn();
|
|
101
|
+
} catch (e) {
|
|
102
|
+
caution(e as Error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
92
106
|
export function parse_template(template: string, replacements: Record<string, string | Array<string>>): string {
|
|
93
107
|
let result = '';
|
|
94
108
|
let buffer = '';
|