@augment-vir/core 31.8.1 → 31.9.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.
@@ -1,3 +1,4 @@
1
+ import { Constructor } from 'type-fest';
1
2
  /**
2
3
  * Either returns the input if it's already an Error instance or converts it into an Error instance.
3
4
  *
@@ -15,3 +16,30 @@ export declare function ensureError(maybeError: unknown): Error;
15
16
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
16
17
  */
17
18
  export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error;
19
+ /**
20
+ * Ensures that the given `originalError` is an instance of the given `errorClass`.
21
+ *
22
+ * - If it is, `originalError` is directly returned.
23
+ * - If it is _not_, a new instance of `errorClass` is constructed with the given parameters.
24
+ *
25
+ * @category Error
26
+ * @category Package : @augment-vir/common
27
+ * @example
28
+ *
29
+ * ```ts
30
+ * import {ensureErrorClass, extractErrorMessage} from '@augment-vir/common';
31
+ *
32
+ * class MyCustomError extends Error {
33
+ * public override readonly name = 'MyCustomError';
34
+ * }
35
+ *
36
+ * try {
37
+ * // do some stuff
38
+ * } catch (error) {
39
+ * throw ensureErrorClass(error, MyCustomError, extractErrorMessage(error));
40
+ * }
41
+ * ```
42
+ *
43
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
44
+ */
45
+ export declare function ensureErrorClass<const ErrorClass extends Constructor<Error>>(originalError: unknown, errorClass: ErrorClass, ...params: ConstructorParameters<ErrorClass>): InstanceType<ErrorClass>;
@@ -27,3 +27,37 @@ export function ensureErrorAndPrependMessage(maybeError, prependMessage) {
27
27
  error.message = combineErrorMessages(prependMessage, error.message);
28
28
  return error;
29
29
  }
30
+ /**
31
+ * Ensures that the given `originalError` is an instance of the given `errorClass`.
32
+ *
33
+ * - If it is, `originalError` is directly returned.
34
+ * - If it is _not_, a new instance of `errorClass` is constructed with the given parameters.
35
+ *
36
+ * @category Error
37
+ * @category Package : @augment-vir/common
38
+ * @example
39
+ *
40
+ * ```ts
41
+ * import {ensureErrorClass, extractErrorMessage} from '@augment-vir/common';
42
+ *
43
+ * class MyCustomError extends Error {
44
+ * public override readonly name = 'MyCustomError';
45
+ * }
46
+ *
47
+ * try {
48
+ * // do some stuff
49
+ * } catch (error) {
50
+ * throw ensureErrorClass(error, MyCustomError, extractErrorMessage(error));
51
+ * }
52
+ * ```
53
+ *
54
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
55
+ */
56
+ export function ensureErrorClass(originalError, errorClass, ...params) {
57
+ if (originalError instanceof errorClass) {
58
+ return originalError;
59
+ }
60
+ else {
61
+ return new errorClass(...params);
62
+ }
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/core",
3
- "version": "31.8.1",
3
+ "version": "31.9.1",
4
4
  "description": "Core augment-vir augments. Use @augment-vir/common instead.",
5
5
  "homepage": "https://github.com/electrovir/augment-vir",
6
6
  "bugs": {
@@ -33,7 +33,7 @@
33
33
  "type-fest": "^4.33.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@types/node": "^22.10.10",
36
+ "@types/node": "^22.13.0",
37
37
  "c8": "^10.1.3",
38
38
  "istanbul-smart-text-reporter": "^1.1.5",
39
39
  "typescript": "^5.7.2"