@jakubmazanec/error 1.0.1-next.c5848fd → 2.0.0-9cfd7b2

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.
Files changed (48) hide show
  1. package/LICENSE.md +840 -0
  2. package/README.md +97 -0
  3. package/dist/constants.d.ts +3 -0
  4. package/dist/constants.js +4 -2
  5. package/dist/constants.js.map +1 -1
  6. package/dist/createCustomError.d.ts +8 -8
  7. package/dist/createCustomError.js +15 -15
  8. package/dist/createCustomError.js.map +1 -1
  9. package/dist/createCustomErrorWithData.d.ts +14 -0
  10. package/dist/createCustomErrorWithData.js +26 -0
  11. package/dist/createCustomErrorWithData.js.map +1 -0
  12. package/dist/internals/createErrorMessage.d.ts +10 -0
  13. package/dist/internals/createErrorMessage.js +17 -0
  14. package/dist/internals/createErrorMessage.js.map +1 -0
  15. package/dist/internals.d.ts +1 -0
  16. package/dist/internals.js +3 -0
  17. package/dist/internals.js.map +1 -0
  18. package/dist/main.d.ts +2 -0
  19. package/dist/main.js +4 -2
  20. package/dist/main.js.map +1 -1
  21. package/dist/types/CustomError.d.ts +2 -4
  22. package/dist/types/CustomError.js.map +1 -1
  23. package/dist/types/CustomErrorOptions.d.ts +9 -0
  24. package/dist/types/CustomErrorOptions.js +5 -0
  25. package/dist/types/CustomErrorOptions.js.map +1 -0
  26. package/dist/types/CustomErrorWithData.d.ts +10 -0
  27. package/dist/types/CustomErrorWithData.js +3 -0
  28. package/dist/types/CustomErrorWithData.js.map +1 -0
  29. package/dist/types/CustomErrorWithDataOptions.d.ts +11 -0
  30. package/dist/types/CustomErrorWithDataOptions.js +5 -0
  31. package/dist/types/CustomErrorWithDataOptions.js.map +1 -0
  32. package/dist/types/ErrorMessages.d.ts +1 -1
  33. package/dist/types.d.ts +3 -0
  34. package/dist/types.js +5 -2
  35. package/dist/types.js.map +1 -1
  36. package/package.json +9 -6
  37. package/src/constants.ts +3 -0
  38. package/src/createCustomError.ts +22 -47
  39. package/src/createCustomErrorWithData.ts +79 -0
  40. package/src/internals/createErrorMessage.ts +27 -0
  41. package/src/internals.ts +1 -0
  42. package/src/main.ts +2 -0
  43. package/src/types/CustomError.ts +2 -5
  44. package/src/types/CustomErrorOptions.ts +10 -0
  45. package/src/types/CustomErrorWithData.ts +12 -0
  46. package/src/types/CustomErrorWithDataOptions.ts +13 -0
  47. package/src/types.ts +3 -0
  48. package/LICENSE +0 -18
@@ -0,0 +1,79 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any -- needed */
2
+ import {type z, type AnyZodObject} from 'zod';
3
+
4
+ import {INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';
5
+ import {createErrorMessage} from './internals.js';
6
+ import {
7
+ type CustomErrorWithData,
8
+ type CustomErrorWithDataOptions,
9
+ type ErrorMessages,
10
+ } from './types.js';
11
+
12
+ /**
13
+ * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies
14
+ * generating error messages via predefined error codes and allows attaching custom data to the error instance.
15
+ *
16
+ * @param errorName Error name.
17
+ * @param errorMessages Object containing all possible error messages.
18
+ * @param errorData Runtime representation of the `ErrorData` type parameter; can be either a class expression, or a Zod schema.
19
+ * @return Class that implements {@linkcode CustomErrorWithData}.
20
+ */
21
+ export function createCustomErrorWithData<ErrorCode extends string, ErrorData extends AnyZodObject>(
22
+ errorName: string,
23
+ errorMessages: ErrorMessages<ErrorCode>,
24
+ errorData: ErrorData,
25
+ ): new (
26
+ code: ErrorCode,
27
+ options?: CustomErrorWithDataOptions<z.infer<ErrorData>>,
28
+ ) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, z.infer<ErrorData>>;
29
+ export function createCustomErrorWithData<
30
+ ErrorCode extends string,
31
+ ErrorData extends abstract new (...args: any) => any,
32
+ >(
33
+ errorName: string,
34
+ errorMessages: ErrorMessages<ErrorCode>,
35
+ errorData: ErrorData,
36
+ ): new (
37
+ code: ErrorCode,
38
+ options?: CustomErrorWithDataOptions<InstanceType<ErrorData>>,
39
+ ) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, InstanceType<ErrorData>>;
40
+ export function createCustomErrorWithData<ErrorCode extends string, ErrorData>(
41
+ errorName: string,
42
+ errorMessages: ErrorMessages<ErrorCode>,
43
+ errorData: ErrorData,
44
+ ): new (code: ErrorCode, options?: CustomErrorWithDataOptions<ErrorData>) => CustomErrorWithData<
45
+ ErrorCode | typeof UNKNOWN_ERROR,
46
+ ErrorData
47
+ > {
48
+ return class InternalCustomError
49
+ extends Error
50
+ implements CustomErrorWithData<ErrorCode, ErrorData>
51
+ {
52
+ code: ErrorCode | typeof UNKNOWN_ERROR;
53
+ data?: ErrorData;
54
+
55
+ constructor(errorCode: ErrorCode, options?: CustomErrorWithDataOptions<ErrorData>) {
56
+ let errorMessage = createErrorMessage(errorCode, errorMessages, options?.messageParameters);
57
+ let errorOptions: ErrorOptions = {};
58
+
59
+ if (typeof options?.cause !== 'undefined') {
60
+ errorOptions.cause = options.cause;
61
+ }
62
+
63
+ super(errorMessage, errorOptions);
64
+
65
+ this.code = errorCode;
66
+ this.name = errorName;
67
+
68
+ if (typeof options?.data !== 'undefined') {
69
+ this.data = options.data;
70
+ }
71
+
72
+ // if the message is empty, we are making this an unknown error
73
+ if (!this.message) {
74
+ this.code = UNKNOWN_ERROR;
75
+ this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);
76
+ }
77
+ }
78
+ };
79
+ }
@@ -0,0 +1,27 @@
1
+ import {ERROR_MESSAGE_TOKEN_PATTERN} from '../constants.js';
2
+ import {type ErrorMessages} from '../types.js';
3
+
4
+ /**
5
+ * A function that generates an error message based on provided error code and message parameters. It is used only internally.
6
+ *
7
+ * @param errorCode Error code.
8
+ * @param errorMessages Object containing all possible error messages.
9
+ * @param messageParameters Array of values that are used as message parameters.
10
+ * @return Error message.
11
+ */
12
+ export function createErrorMessage<T extends string>(
13
+ errorCode: T,
14
+ errorMessages: ErrorMessages<T>,
15
+ messageParameters: unknown[] = [],
16
+ ): string {
17
+ let errorMessage = errorMessages[errorCode];
18
+
19
+ if (!errorMessage) {
20
+ return '';
21
+ }
22
+
23
+ return `${errorCode}: ${errorMessage.replace(
24
+ ERROR_MESSAGE_TOKEN_PATTERN,
25
+ (match: string, index: number) => String(messageParameters[index]),
26
+ )}`;
27
+ }
@@ -0,0 +1 @@
1
+ export * from './internals/createErrorMessage.js';
package/src/main.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './createCustomError.js';
2
+ export * from './createCustomErrorWithData.js';
2
3
  export * from './types.js';
4
+ export {UNKNOWN_ERROR} from './constants.js';
@@ -1,12 +1,9 @@
1
- import type {UNKNOWN_ERROR} from '../constants.js';
1
+ import {type UNKNOWN_ERROR} from '../constants.js';
2
2
 
3
3
  /**
4
4
  * Type representing custom error.
5
5
  */
6
- export type CustomError<ErrorCode extends string, ErrorData> = Error & {
6
+ export type CustomError<ErrorCode extends string> = Error & {
7
7
  /** String that is used to quickly determine the nature of the error and why it could have occurred. */
8
8
  code: ErrorCode | typeof UNKNOWN_ERROR;
9
-
10
- /** Data related to the error that can be used during the error handling process to gain more information or take an action. */
11
- data?: ErrorData;
12
9
  };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomError}.
3
+ */
4
+ export type CustomErrorOptions = {
5
+ /** Array of values that are used as parameters to generate the error message. */
6
+ messageParameters?: unknown[];
7
+
8
+ /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */
9
+ cause?: unknown;
10
+ };
@@ -0,0 +1,12 @@
1
+ import type {UNKNOWN_ERROR} from '../constants.js';
2
+
3
+ /**
4
+ * Type representing custom error that can have an arbitrary data attached.
5
+ */
6
+ export type CustomErrorWithData<ErrorCode extends string, ErrorData> = Error & {
7
+ /** String that is used to quickly determine the nature of the error and why it could have occurred. */
8
+ code: ErrorCode | typeof UNKNOWN_ERROR;
9
+
10
+ /** Data related to the error that can be used during the error handling process to gain more information or take an action. */
11
+ data?: ErrorData;
12
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomErrorWithData}.
3
+ */
4
+ export type CustomErrorWithDataOptions<ErrorData> = {
5
+ /** Array of values that are used as parameters to generate the error message. */
6
+ messageParameters?: unknown[];
7
+
8
+ /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */
9
+ cause?: unknown;
10
+
11
+ /** Custom data that will be attached to the error instance. */
12
+ data?: ErrorData;
13
+ };
package/src/types.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  export * from './types/CustomError.js';
2
+ export * from './types/CustomErrorOptions.js';
3
+ export * from './types/CustomErrorWithData.js';
4
+ export * from './types/CustomErrorWithDataOptions.js';
2
5
  export * from './types/ErrorMessages.js';
package/LICENSE DELETED
@@ -1,18 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Jakub Mazanec
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6
- associated documentation files (the "Software"), to deal in the Software without restriction,
7
- including without limitation the rights to use, copy, modify, merge, publish, distribute,
8
- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9
- furnished to do so, subject to the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be included in all copies or substantial
12
- portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15
- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
17
- OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.