@naturalcycles/js-lib 14.96.0 → 14.96.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.
@@ -10,20 +10,26 @@ import { AppError } from './app.error';
10
10
  * For convenience, second argument type is non-optional,
11
11
  * so you can use it without `!`. But you SHOULD always check `if (err)` first!
12
12
  *
13
+ * ERR is typed as Error, not `unknown`. While unknown would be more correct,
14
+ * according to recent TypeScript, Error gives more developer convenience.
15
+ * In our code we NEVER throw non-errors.
16
+ * Only possibility of non-error is in the 3rd-party library code, in these cases it
17
+ * can be manually cast to `unknown` for extra safety.
18
+ *
13
19
  * @example
14
20
  *
15
21
  * const [err, v] = _try(() => someFunction())
16
22
  * if (err) ...do something...
17
23
  * v // go ahead and use v
18
24
  */
19
- export declare function _try<ERR = unknown, RETURN = void>(fn: () => RETURN): [err: ERR | null, value: RETURN];
25
+ export declare function _try<ERR = Error, RETURN = void>(fn: () => RETURN): [err: ERR | null, value: RETURN];
20
26
  /**
21
27
  * Like _try, but for Promises.
22
28
  *
23
29
  * Also, intentionally types second return item as non-optional,
24
30
  * but you should check for `err` presense first!
25
31
  */
26
- export declare function pTry<ERR = unknown, RETURN = void>(promise: Promise<RETURN>): Promise<[err: ERR | null, value: Awaited<RETURN>]>;
32
+ export declare function pTry<ERR = Error, RETURN = void>(promise: Promise<RETURN>): Promise<[err: ERR | null, value: Awaited<RETURN>]>;
27
33
  /**
28
34
  * It is thrown when Error was expected, but didn't happen
29
35
  * ("pass" happened instead).
package/dist/error/try.js CHANGED
@@ -12,6 +12,12 @@ const app_error_1 = require("./app.error");
12
12
  * For convenience, second argument type is non-optional,
13
13
  * so you can use it without `!`. But you SHOULD always check `if (err)` first!
14
14
  *
15
+ * ERR is typed as Error, not `unknown`. While unknown would be more correct,
16
+ * according to recent TypeScript, Error gives more developer convenience.
17
+ * In our code we NEVER throw non-errors.
18
+ * Only possibility of non-error is in the 3rd-party library code, in these cases it
19
+ * can be manually cast to `unknown` for extra safety.
20
+ *
15
21
  * @example
16
22
  *
17
23
  * const [err, v] = _try(() => someFunction())
@@ -49,7 +55,7 @@ exports.pTry = pTry;
49
55
  */
50
56
  class UnexpectedPassError extends app_error_1.AppError {
51
57
  constructor() {
52
- super('_expectedError passed unexpectedly');
58
+ super('expected error was not thrown');
53
59
  }
54
60
  }
55
61
  exports.UnexpectedPassError = UnexpectedPassError;
@@ -5,7 +5,7 @@ export interface TryCatchOptions {
5
5
  * The value returned from the function will be returned from the wrapped method (!).
6
6
  * onError function may be asynchronous.
7
7
  */
8
- onError?: (err: unknown) => any;
8
+ onError?: (err: Error) => any;
9
9
  /**
10
10
  * @default false
11
11
  */
@@ -30,7 +30,7 @@ function _tryCatch(fn, opt = {}) {
30
30
  }
31
31
  if (onError) {
32
32
  try {
33
- return await onError(err); // eslint-disable-line @typescript-eslint/return-await
33
+ return await onError((0, index_1._anyToError)(err)); // eslint-disable-line @typescript-eslint/return-await
34
34
  }
35
35
  catch { }
36
36
  }
@@ -9,6 +9,12 @@ import { AppError } from './app.error';
9
9
  * For convenience, second argument type is non-optional,
10
10
  * so you can use it without `!`. But you SHOULD always check `if (err)` first!
11
11
  *
12
+ * ERR is typed as Error, not `unknown`. While unknown would be more correct,
13
+ * according to recent TypeScript, Error gives more developer convenience.
14
+ * In our code we NEVER throw non-errors.
15
+ * Only possibility of non-error is in the 3rd-party library code, in these cases it
16
+ * can be manually cast to `unknown` for extra safety.
17
+ *
12
18
  * @example
13
19
  *
14
20
  * const [err, v] = _try(() => someFunction())
@@ -44,7 +50,7 @@ export async function pTry(promise) {
44
50
  */
45
51
  export class UnexpectedPassError extends AppError {
46
52
  constructor() {
47
- super('_expectedError passed unexpectedly');
53
+ super('expected error was not thrown');
48
54
  }
49
55
  }
50
56
  /**
@@ -1,4 +1,4 @@
1
- import { _since, _stringifyAny } from '../index';
1
+ import { _anyToError, _since, _stringifyAny } from '../index';
2
2
  /**
3
3
  * Decorates a function with "try/catch", so it'll never reject/throw.
4
4
  * Only applies to async functions (or, turns sync function into async).
@@ -27,7 +27,7 @@ export function _tryCatch(fn, opt = {}) {
27
27
  }
28
28
  if (onError) {
29
29
  try {
30
- return await onError(err); // eslint-disable-line @typescript-eslint/return-await
30
+ return await onError(_anyToError(err)); // eslint-disable-line @typescript-eslint/return-await
31
31
  }
32
32
  catch (_a) { }
33
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.96.0",
3
+ "version": "14.96.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
package/src/error/try.ts CHANGED
@@ -11,13 +11,19 @@ import { AppError } from './app.error'
11
11
  * For convenience, second argument type is non-optional,
12
12
  * so you can use it without `!`. But you SHOULD always check `if (err)` first!
13
13
  *
14
+ * ERR is typed as Error, not `unknown`. While unknown would be more correct,
15
+ * according to recent TypeScript, Error gives more developer convenience.
16
+ * In our code we NEVER throw non-errors.
17
+ * Only possibility of non-error is in the 3rd-party library code, in these cases it
18
+ * can be manually cast to `unknown` for extra safety.
19
+ *
14
20
  * @example
15
21
  *
16
22
  * const [err, v] = _try(() => someFunction())
17
23
  * if (err) ...do something...
18
24
  * v // go ahead and use v
19
25
  */
20
- export function _try<ERR = unknown, RETURN = void>(
26
+ export function _try<ERR = Error, RETURN = void>(
21
27
  fn: () => RETURN,
22
28
  ): [err: ERR | null, value: RETURN] {
23
29
  try {
@@ -33,7 +39,7 @@ export function _try<ERR = unknown, RETURN = void>(
33
39
  * Also, intentionally types second return item as non-optional,
34
40
  * but you should check for `err` presense first!
35
41
  */
36
- export async function pTry<ERR = unknown, RETURN = void>(
42
+ export async function pTry<ERR = Error, RETURN = void>(
37
43
  promise: Promise<RETURN>,
38
44
  ): Promise<[err: ERR | null, value: Awaited<RETURN>]> {
39
45
  try {
@@ -50,7 +56,7 @@ export async function pTry<ERR = unknown, RETURN = void>(
50
56
  */
51
57
  export class UnexpectedPassError extends AppError {
52
58
  constructor() {
53
- super('_expectedError passed unexpectedly')
59
+ super('expected error was not thrown')
54
60
  }
55
61
  }
56
62
 
@@ -1,4 +1,4 @@
1
- import { _since, _stringifyAny, CommonLogger } from '../index'
1
+ import { _anyToError, _since, _stringifyAny, CommonLogger } from '../index'
2
2
  import { AnyFunction } from '../types'
3
3
 
4
4
  export interface TryCatchOptions {
@@ -6,7 +6,7 @@ export interface TryCatchOptions {
6
6
  * The value returned from the function will be returned from the wrapped method (!).
7
7
  * onError function may be asynchronous.
8
8
  */
9
- onError?: (err: unknown) => any
9
+ onError?: (err: Error) => any
10
10
 
11
11
  /**
12
12
  * @default false
@@ -59,7 +59,7 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
59
59
 
60
60
  if (onError) {
61
61
  try {
62
- return await onError(err) // eslint-disable-line @typescript-eslint/return-await
62
+ return await onError(_anyToError(err)) // eslint-disable-line @typescript-eslint/return-await
63
63
  } catch {}
64
64
  }
65
65
  // returns undefined, but doesn't rethrow