@heritsilavo/react-error-boundary 1.1.0 → 2.0.0

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.
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ export declare const ErrorBoundary: React.FC<{
3
+ children: React.ReactNode;
4
+ handleError: (error: Error) => void;
5
+ }>;
@@ -12,6 +12,7 @@ interface ErrorProviderProps {
12
12
  autoHideDelay?: number;
13
13
  onError?: () => void;
14
14
  closeErrorComponetOnClick?: boolean;
15
+ handleError?: (error: Error) => void;
15
16
  }
16
17
  export declare const ErrorProvider: React.FC<ErrorProviderProps>;
17
18
  export declare const useError: () => ContextType;
@@ -11,6 +11,34 @@ class CustomError extends Error {
11
11
  }
12
12
  }
13
13
 
14
+ // Composant de classe qui capture les erreurs React
15
+ class ErrorBoundaryComponent extends React.Component {
16
+ constructor(props) {
17
+ super(props);
18
+ this.state = { hasError: false };
19
+ }
20
+ // Appelé quand une erreur est détectée dans un composant enfant
21
+ static getDerivedStateFromError(error) {
22
+ return { hasError: true };
23
+ }
24
+ // Permet de logger l'erreur et d'exécuter du code de récupération
25
+ componentDidCatch(error, errorInfo) {
26
+ if (this.props.handleError) {
27
+ this.props.handleError(error);
28
+ }
29
+ }
30
+ render() {
31
+ if (this.state.hasError) {
32
+ this.setState({ hasError: false });
33
+ return this.props.children;
34
+ }
35
+ return this.props.children;
36
+ }
37
+ }
38
+ const ErrorBoundary = ({ children, handleError }) => {
39
+ return React.createElement(ErrorBoundaryComponent, { handleError: handleError }, children);
40
+ };
41
+
14
42
  var util;
15
43
  (function (util) {
16
44
  util.assertEqual = (val) => val;
@@ -4336,7 +4364,7 @@ const ErrorContext = React.createContext({
4336
4364
  error: null,
4337
4365
  clearError: () => { }
4338
4366
  });
4339
- const ErrorProvider = ({ children, ErrorComponent = DefaultErrorComponent, autoHideDelay = 5000, onError = () => { }, closeErrorComponetOnClick = false }) => {
4367
+ const ErrorProvider = ({ children, ErrorComponent = DefaultErrorComponent, autoHideDelay = 5000, onError = () => { }, closeErrorComponetOnClick = false, handleError: externalHandleError }) => {
4340
4368
  const [error, setError] = React.useState(null);
4341
4369
  const timeoutRef = React.useRef(null);
4342
4370
  // Fonction de nettoyage du timeout
@@ -4357,10 +4385,14 @@ const ErrorProvider = ({ children, ErrorComponent = DefaultErrorComponent, autoH
4357
4385
  ? error
4358
4386
  : new CustomError('UNKNOWN_ERROR', 'Une erreur inattendue est survenue', error.message);
4359
4387
  setError(newError);
4388
+ // Appel du gestionnaire d'erreurs externe s'il est fourni
4389
+ if (externalHandleError) {
4390
+ externalHandleError(newError);
4391
+ }
4360
4392
  if (autoHideDelay > 0) {
4361
4393
  timeoutRef.current = setTimeout(() => setError(null), autoHideDelay);
4362
4394
  }
4363
- }, [autoHideDelay]);
4395
+ }, [autoHideDelay, externalHandleError, onError]);
4364
4396
  const clearError = React.useCallback(() => {
4365
4397
  clearTimeout();
4366
4398
  setError(null);
@@ -4371,7 +4403,7 @@ const ErrorProvider = ({ children, ErrorComponent = DefaultErrorComponent, autoH
4371
4403
  clearError
4372
4404
  }), [handleError, error, clearError]);
4373
4405
  return (React.createElement(ErrorContext.Provider, { value: contextValue },
4374
- React.createElement(ErrorBoundary, null,
4406
+ React.createElement(ErrorBoundary, { handleError: handleError },
4375
4407
  error && (React.createElement(ErrorComponent, { error: error, onClose: clearError, closeOnClick: closeErrorComponetOnClick, visiblityTime: autoHideDelay })),
4376
4408
  children)));
4377
4409
  };
@@ -4391,38 +4423,6 @@ const useThrowError = () => {
4391
4423
  }, [handleError]);
4392
4424
  };
4393
4425
 
4394
- // Composant de classe qui capture les erreurs React
4395
- class ErrorBoundaryComponent extends React.Component {
4396
- constructor(props) {
4397
- super(props);
4398
- this.state = { hasError: false };
4399
- }
4400
- // Appelé quand une erreur est détectée dans un composant enfant
4401
- static getDerivedStateFromError(error) {
4402
- return { hasError: true };
4403
- }
4404
- // Permet de logger l'erreur et d'exécuter du code de récupération
4405
- componentDidCatch(error, errorInfo) {
4406
- if (this.props.handleError) {
4407
- this.props.handleError(error);
4408
- }
4409
- }
4410
- render() {
4411
- if (this.state.hasError) {
4412
- this.setState({ hasError: false });
4413
- return this.props.children;
4414
- }
4415
- return this.props.children;
4416
- }
4417
- }
4418
- const ErrorBoundary = ({ children }) => {
4419
- const { handleError } = useError();
4420
- return React.createElement(ErrorBoundaryComponent, { handleError: handleError },
4421
- " ",
4422
- children,
4423
- " ");
4424
- };
4425
-
4426
4426
  exports.CustomError = CustomError;
4427
4427
  exports.DefaultErrorComponent = DefaultErrorComponent;
4428
4428
  exports.ErrorBoundary = ErrorBoundary;