reslib 2.0.1 → 2.0.3

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.
@@ -69,10 +69,6 @@ export declare class BaseException<TDetails = unknown, TCause = unknown> extends
69
69
  */
70
70
  readonly timestamp: Date;
71
71
  readonly success: boolean;
72
- /**
73
- * The validation error that caused this exception.
74
- */
75
- validationError?: ValidatorError | ValidatorClassError | ValidatorBulkError;
76
72
  /**
77
73
  * Creates a new BaseException instance.
78
74
  *
@@ -813,8 +809,331 @@ export declare class BaseException<TDetails = unknown, TCause = unknown> extends
813
809
  * @see {@link parseErrorDetails} - Override for custom detail extraction only
814
810
  */
815
811
  protected static createFromError<TDetails = unknown, TException extends BaseException<TDetails> = BaseException<TDetails>, TCause = unknown>(this: BaseExceptionConstructor<TDetails, TException>, error: unknown, options?: BaseExceptionOptions<TDetails, TCause>): TException;
816
- static isValidationError(error: unknown): error is ValidatorError;
817
- static getValidationError(error: unknown): ValidatorError | ValidatorClassError | ValidatorBulkError | null;
812
+ /**
813
+ * Type guard to check if an error is a ValidatorClassError.
814
+ *
815
+ * ValidatorClassError represents validation failures for class/object validation,
816
+ * containing field-level errors with detailed information about which properties
817
+ * failed validation and why.
818
+ *
819
+ * @param error - The value to check
820
+ * @returns True if the error is a ValidatorClassError, false otherwise
821
+ *
822
+ * @example
823
+ * ```typescript
824
+ * try {
825
+ * await validateUserInput(data);
826
+ * } catch (error) {
827
+ * if (BaseException.isValidatorClassError(error)) {
828
+ * // TypeScript now knows error is ValidatorClassError
829
+ * console.log('Field errors:', error.fieldErrors);
830
+ * error.fieldErrors.forEach(fieldError => {
831
+ * console.log(`${fieldError.field}: ${fieldError.message}`);
832
+ * });
833
+ * }
834
+ * }
835
+ * ```
836
+ *
837
+ * @see {@link ValidatorClassError} - The class validation error type
838
+ * @see {@link isValidatorError} - Check for any validator error type
839
+ * @see {@link getValidatorError} - Extract validation error from exceptions
840
+ */
841
+ static isValidatorClassError(error: unknown): error is ValidatorClassError;
842
+ /**
843
+ * Type guard to check if an error is a ValidatorBulkError.
844
+ *
845
+ * ValidatorBulkError represents validation failures for bulk/array validation,
846
+ * containing errors for multiple items in an array with index information.
847
+ *
848
+ * @param error - The value to check
849
+ * @returns True if the error is a ValidatorBulkError, false otherwise
850
+ *
851
+ * @example
852
+ * ```typescript
853
+ * try {
854
+ * await validateBulkUsers(userArray);
855
+ * } catch (error) {
856
+ * if (BaseException.isValidatorBulkError(error)) {
857
+ * // TypeScript now knows error is ValidatorBulkError
858
+ * console.log(`${error.itemCount} items failed validation`);
859
+ * error.itemErrors.forEach(itemError => {
860
+ * console.log(`Item[${itemError.index}]: ${itemError.message}`);
861
+ * });
862
+ * }
863
+ * }
864
+ * ```
865
+ *
866
+ * @see {@link ValidatorBulkError} - The bulk validation error type
867
+ * @see {@link isValidatorError} - Check for any validator error type
868
+ * @see {@link getValidatorError} - Extract validation error from exceptions
869
+ */
870
+ static isValidatorBulkError(error: unknown): error is ValidatorBulkError;
871
+ /**
872
+ * Type guard to check if an error is a ValidatorError (single field/value validation).
873
+ *
874
+ * ValidatorError represents validation failures for individual field/value validation,
875
+ * containing information about a single validation rule failure.
876
+ *
877
+ * @param error - The value to check
878
+ * @returns True if the error is a ValidatorError, false otherwise
879
+ *
880
+ * @example
881
+ * ```typescript
882
+ * try {
883
+ * await validateEmail(email);
884
+ * } catch (error) {
885
+ * if (BaseException.isValidatorError(error)) {
886
+ * // TypeScript now knows error is ValidatorError
887
+ * console.log('Validation failed:', error.message);
888
+ * console.log('Field:', error.field);
889
+ * console.log('Rule:', error.rule);
890
+ * }
891
+ * }
892
+ * ```
893
+ *
894
+ * @see {@link ValidatorError} - The single field validation error type
895
+ * @see {@link isValidatorClassError} - Check for class validation errors
896
+ * @see {@link isValidatorBulkError} - Check for bulk validation errors
897
+ * @see {@link getValidatorError} - Extract validation error from exceptions
898
+ */
899
+ static isValidatorError(error: unknown): error is ValidatorError;
900
+ /**
901
+ * Type guard to check if an error is any type of validator error.
902
+ *
903
+ * This is a convenience method that checks if the error is a `ValidatorError`,
904
+ * `ValidatorClassError`, or `ValidatorBulkError`. Use this when you want to
905
+ * handle any validation error generically without caring about the specific type.
906
+ *
907
+ * **Note**: This method is an alias for `Validator.isAnyError()`.
908
+ * Both methods provide identical functionality and can be used interchangeably.
909
+ *
910
+ * **When to use this vs specific type guards:**
911
+ * - Use `isAnyValidatorError()` when you want to handle ALL validator errors the same way
912
+ * - Use `isValidatorError()`, `isValidatorClassError()`, or `isValidatorBulkError()`
913
+ * when you need type-specific handling
914
+ *
915
+ * @param error - The value to check
916
+ * @returns True if the error is any type of validator error, false otherwise
917
+ *
918
+ * @example
919
+ * ```typescript
920
+ * // Generic validation error handling
921
+ * try {
922
+ * await processData(data);
923
+ * } catch (error) {
924
+ * if (BaseException.isAnyValidatorError(error)) {
925
+ * // Handle any validation error
926
+ * console.log('Validation failed:', error.message);
927
+ * return { success: false, type: 'validation', error };
928
+ * }
929
+ * // Handle other error types
930
+ * console.error('Unexpected error:', error);
931
+ * return { success: false, type: 'unknown', error };
932
+ * }
933
+ * ```
934
+ *
935
+ * @example
936
+ * ```typescript
937
+ * // Check before extracting validation details
938
+ * function handleError(error: unknown) {
939
+ * if (BaseException.isAnyValidatorError(error)) {
940
+ * // Now we know it's a validator error, extract it
941
+ * const validationError = BaseException.getValidatorError(error);
942
+ *
943
+ * // Then check specific type if needed
944
+ * if (BaseException.isValidatorClassError(validationError)) {
945
+ * return formatFieldErrors(validationError.fieldErrors);
946
+ * } else if (BaseException.isValidatorBulkError(validationError)) {
947
+ * return formatBulkErrors(validationError.itemErrors);
948
+ * }
949
+ * }
950
+ *
951
+ * return formatGenericError(error);
952
+ * }
953
+ * ```
954
+ *
955
+ * @example
956
+ * ```typescript
957
+ * // API error response handling
958
+ * async function apiCall(endpoint: string, data: any) {
959
+ * try {
960
+ * const response = await fetch(endpoint, {
961
+ * method: 'POST',
962
+ * body: JSON.stringify(data)
963
+ * });
964
+ * const result = await response.json();
965
+ *
966
+ * if (!response.ok) {
967
+ * // Check if it's a validation error
968
+ * if (BaseException.isAnyValidatorError(result)) {
969
+ * throw AppException.from(result, {
970
+ * code: 'VALIDATION_ERROR',
971
+ * statusCode: 400
972
+ * });
973
+ * }
974
+ * throw AppException.from(result);
975
+ * }
976
+ *
977
+ * return result;
978
+ * } catch (error) {
979
+ * if (BaseException.isAnyValidatorError(error)) {
980
+ * // Special handling for validation errors
981
+ * notifyUser('Please check your input');
982
+ * }
983
+ * throw error;
984
+ * }
985
+ * }
986
+ * ```
987
+ *
988
+ * @example
989
+ * ```typescript
990
+ * // Logging with error type detection
991
+ * function logError(error: unknown) {
992
+ * const errorType = BaseException.isAnyValidatorError(error)
993
+ * ? 'VALIDATION'
994
+ * : BaseException.is(error)
995
+ * ? 'APPLICATION'
996
+ * : 'UNKNOWN';
997
+ *
998
+ * logger.error({
999
+ * type: errorType,
1000
+ * message: error instanceof Error ? error.message : String(error),
1001
+ * details: BaseException.isAnyValidatorError(error) ? error : undefined
1002
+ * });
1003
+ * }
1004
+ * ```
1005
+ *
1006
+ * @remarks
1007
+ * **Behavior**:
1008
+ * - Returns `true` if error is `ValidatorError`, `ValidatorClassError`, or `ValidatorBulkError`
1009
+ * - Returns `false` for all other error types
1010
+ * - Does NOT check wrapped exceptions (use `getValidatorError()` for that)
1011
+ *
1012
+ * **Type Safety**:
1013
+ * - TypeScript will narrow the type to the union of all validator error types
1014
+ * - Use specific type guards for further narrowing if needed
1015
+ *
1016
+ * **Performance**:
1017
+ * - Performs up to 3 type checks (short-circuits on first match)
1018
+ * - Very fast for most cases due to short-circuiting
1019
+ *
1020
+ * **Alias Information**:
1021
+ * - This method delegates to `Validator.isAnyError()`
1022
+ * - Use whichever is more convenient in your context
1023
+ * - Both provide identical type narrowing and behavior
1024
+ *
1025
+ * @see {@link isValidatorError} - Check for single field validation errors only
1026
+ * @see {@link isValidatorClassError} - Check for class validation errors only
1027
+ * @see {@link isValidatorBulkError} - Check for bulk validation errors only
1028
+ * @see {@link getValidatorError} - Extract validation error from exceptions (checks wrapped errors)
1029
+ * @see {@link Validator.isAnyError} - Original implementation in Validator class
1030
+ * @see {@link ValidatorError} - Single field validation error type
1031
+ * @see {@link ValidatorClassError} - Class validation error type
1032
+ * @see {@link ValidatorBulkError} - Bulk validation error type
1033
+ */
1034
+ static isAnyValidatorError(error: unknown): error is ValidatorError | ValidatorClassError | ValidatorBulkError;
1035
+ /**
1036
+ * Extracts the underlying validation error from an error or exception.
1037
+ *
1038
+ * This method checks if the provided error is a validation error directly,
1039
+ * or if it's a BaseException that wraps a validation error. It returns the
1040
+ * validation error if found, or null otherwise.
1041
+ *
1042
+ * **Use Cases**:
1043
+ * - Extract validation errors from caught exceptions
1044
+ * - Check if an API error response contains validation failures
1045
+ * - Access detailed field-level validation errors for custom error handling
1046
+ *
1047
+ * @param error - The error to extract validation error from
1048
+ * @returns The validation error if found, null otherwise
1049
+ *
1050
+ * @example
1051
+ * ```typescript
1052
+ * // Extract from API exception
1053
+ * try {
1054
+ * await api.createUser(userData);
1055
+ * } catch (error) {
1056
+ * const validationError = BaseException.getValidatorError(error);
1057
+ * if (validationError) {
1058
+ * // Handle validation error specifically
1059
+ * if (BaseException.isValidatorClassError(validationError)) {
1060
+ * // Show field-level errors to user
1061
+ * validationError.fieldErrors.forEach(fieldError => {
1062
+ * showFieldError(fieldError.field, fieldError.message);
1063
+ * });
1064
+ * }
1065
+ * } else {
1066
+ * // Handle other error types
1067
+ * showGenericError(error);
1068
+ * }
1069
+ * }
1070
+ * ```
1071
+ *
1072
+ * @example
1073
+ * ```typescript
1074
+ * // Check validation error in action handler
1075
+ * async function handleFormSubmit(formData: FormData) {
1076
+ * try {
1077
+ * return await submitForm(formData);
1078
+ * } catch (error) {
1079
+ * const validationError = AppException.getValidatorError(error);
1080
+ *
1081
+ * if (validationError && AppException.isValidatorClassError(validationError)) {
1082
+ * // Return field errors to form
1083
+ * return {
1084
+ * success: false,
1085
+ * fieldErrors: validationError.fieldErrors.reduce((acc, err) => {
1086
+ * acc[err.field] = err.message;
1087
+ * return acc;
1088
+ * }, {} as Record<string, string>)
1089
+ * };
1090
+ * }
1091
+ *
1092
+ * // Return generic error
1093
+ * return {
1094
+ * success: false,
1095
+ * message: error.message
1096
+ * };
1097
+ * }
1098
+ * }
1099
+ * ```
1100
+ *
1101
+ * @example
1102
+ * ```typescript
1103
+ * // Bulk validation error handling
1104
+ * try {
1105
+ * await validateBulkImport(records);
1106
+ * } catch (error) {
1107
+ * const validationError = BaseException.getValidatorError(error);
1108
+ *
1109
+ * if (validationError && BaseException.isValidatorBulkError(validationError)) {
1110
+ * console.log(`${validationError.itemCount} of ${validationError.totalCount} items failed`);
1111
+ * validationError.itemErrors.forEach(itemError => {
1112
+ * console.log(`Row ${itemError.index + 1}: ${itemError.message}`);
1113
+ * });
1114
+ * }
1115
+ * }
1116
+ * ```
1117
+ *
1118
+ * @remarks
1119
+ * **Behavior**:
1120
+ * - Returns the error directly if it's a validator error
1121
+ * - Extracts `validatorError` property from BaseException instances
1122
+ * - Returns null if no validation error is found
1123
+ * - Works with wrapped exceptions (checks the `validatorError` property)
1124
+ *
1125
+ * **Type Safety**:
1126
+ * - Use with type guard methods for proper TypeScript narrowing
1127
+ * - Combine with `isValidatorClassError()` or `isValidatorBulkError()` for specific handling
1128
+ *
1129
+ * @see {@link isValidatorError} - Check if error is a single field validation error
1130
+ * @see {@link isValidatorClassError} - Check if error is a class validation error
1131
+ * @see {@link isValidatorBulkError} - Check if error is a bulk validation error
1132
+ * @see {@link ValidatorError} - Single field validation error type
1133
+ * @see {@link ValidatorClassError} - Class validation error type
1134
+ * @see {@link ValidatorBulkError} - Bulk validation error type
1135
+ */
1136
+ static getValidatorError(error: unknown): ValidatorError | ValidatorClassError | ValidatorBulkError | null;
818
1137
  /**
819
1138
  * Protected method to extract a human-readable message from an error value.
820
1139
  *
@@ -859,12 +1178,6 @@ export declare class BaseException<TDetails = unknown, TCause = unknown> extends
859
1178
  * return super.parseErrorMessage(error, options);
860
1179
  * }
861
1180
  * }
862
- *
863
- * const validationError = {
864
- * errors: [{ field: 'email', message: 'Invalid format' }]
865
- * };
866
- * const ex = ValidationException.from(validationError);
867
- * // ex.message === 'Validation failed for email: Invalid format'
868
1181
  * ```
869
1182
  *
870
1183
  * @example