@heritsilavo/react-error-boundary 2.1.0 → 2.1.2

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.
package/README.md ADDED
@@ -0,0 +1,272 @@
1
+
2
+ # @heritsilavo/react-error-boundary
3
+
4
+ [![npm version](https://img.shields.io/npm/v/@heritsilavo/react-error-boundary)](https://www.npmjs.com/package/@heritsilavo/react-error-boundary)
5
+ [![license](https://img.shields.io/npm/l/@heritsilavo/react-error-boundary)](https://github.com/heritsilavo/react-error-boundary/blob/main/LICENSE)
6
+
7
+ A robust error boundary component for React and Next.js applications with customizable error handling and display.
8
+
9
+ ## Features
10
+
11
+ - 🛡️ Comprehensive error boundary for React and Next.js
12
+ - 🎨 Customizable error display component
13
+ - ⏱️ Auto-hide functionality with progress indicator
14
+ - 🚦 Error context for centralized error management
15
+ - 📦 Supports both CommonJS and ES Modules
16
+ - 🏷️ Fully typed with TypeScript
17
+ - ⚛️ React 18+ compatible
18
+ - ⏭️ Next.js 13+ support
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install @heritsilavo/react-error-boundary
24
+ # or
25
+ yarn add @heritsilavo/react-error-boundary
26
+ # or
27
+ pnpm add @heritsilavo/react-error-boundary
28
+ ```
29
+
30
+ ## Peer Dependencies
31
+
32
+ This package requires:
33
+ - React 18+
34
+ - React DOM 18+
35
+ - For Next.js usage: Next.js 13+
36
+
37
+ ## Basic Usage
38
+
39
+ ### For React Applications
40
+
41
+ ```tsx
42
+ import { ErrorProvider } from '@heritsilavo/react-error-boundary';
43
+
44
+ function App() {
45
+ return (
46
+ <ErrorProvider>
47
+ <YourApp />
48
+ </ErrorProvider>
49
+ );
50
+ }
51
+ ```
52
+
53
+ ### For Next.js Applications
54
+
55
+ ```tsx
56
+ import { ErrorProvider } from '@heritsilavo/react-error-boundary/next';
57
+
58
+ function App({ Component, pageProps }) {
59
+ return (
60
+ <ErrorProvider>
61
+ <Component {...pageProps} />
62
+ </ErrorProvider>
63
+ );
64
+ }
65
+ ```
66
+
67
+ ## Advanced Usage
68
+
69
+ ### Custom Error Component
70
+
71
+ ```tsx
72
+ import { ErrorProvider } from '@heritsilavo/react-error-boundary';
73
+ import type { ErrorComponentType } from '@heritsilavo/react-error-boundary';
74
+
75
+ function CustomError({ error, onClose }: ErrorComponentType) {
76
+ return (
77
+ <div className="custom-error">
78
+ <h3>Error: {error.code}</h3>
79
+ <p>{error.message}</p>
80
+ {error.description && <small>{error.description}</small>}
81
+ <button onClick={onClose}>Close</button>
82
+ </div>
83
+ );
84
+ }
85
+
86
+ function App() {
87
+ return (
88
+ <ErrorProvider
89
+ ErrorComponent={CustomError}
90
+ autoHideDelay={10000}
91
+ >
92
+ <YourApp />
93
+ </ErrorProvider>
94
+ );
95
+ }
96
+ ```
97
+
98
+ ### Throwing Custom Errors
99
+
100
+ ```tsx
101
+ import { useThrowError } from '@heritsilavo/react-error-boundary';
102
+
103
+ function MyComponent() {
104
+ const throwError = useThrowError();
105
+
106
+ const handleClick = () => {
107
+ try {
108
+ // Your code that might fail
109
+ } catch (err) {
110
+ throwError(
111
+ 'API_ERROR',
112
+ 'Failed to fetch data',
113
+ err.message
114
+ );
115
+ }
116
+ };
117
+
118
+ return <button onClick={handleClick}>Fetch Data</button>;
119
+ }
120
+ ```
121
+
122
+ ### Accessing Error Context
123
+
124
+ ```tsx
125
+ import { useError } from '@heritsilavo/react-error-boundary';
126
+
127
+ function StatusIndicator() {
128
+ const { error, clearError } = useError();
129
+
130
+ if (!error) return null;
131
+
132
+ return (
133
+ <div>
134
+ <span>Error: {error.message}</span>
135
+ <button onClick={clearError}>Dismiss</button>
136
+ </div>
137
+ );
138
+ }
139
+ ```
140
+
141
+ ## API Reference
142
+
143
+ ### Components
144
+
145
+ #### `ErrorProvider`
146
+
147
+ The root provider component that sets up the error boundary and context.
148
+
149
+ **Props:**
150
+
151
+ | Prop | Type | Default | Description |
152
+ |------|------|---------|-------------|
153
+ | `children` | ReactNode | required | Your application components |
154
+ | `ErrorComponent` | ComponentType<ErrorComponentType> | `DefaultErrorComponent` | Custom error display component |
155
+ | `autoHideDelay` | number | 5000 | Time in ms before auto-hiding (0 to disable) |
156
+ | `onError` | () => void | () => {} | Callback when error occurs |
157
+ | `closeErrorComponetOnClick` | boolean | false | Close error component when clicked |
158
+
159
+ #### `ErrorBoundary`
160
+
161
+ The actual error boundary component (used internally by ErrorProvider but can be used directly if needed).
162
+
163
+ ### Hooks
164
+
165
+ #### `useError()`
166
+
167
+ Returns the error context with:
168
+ - `error: CustomError | null` - Current error object
169
+ - `handleError: (error: Error) => void` - Function to trigger errors
170
+ - `clearError: () => void` - Function to clear current error
171
+
172
+ #### `useThrowError()`
173
+
174
+ Returns a convenience function to throw custom errors:
175
+ `(code: string, message: string, description: string) => void`
176
+
177
+ ### Types
178
+
179
+ #### `CustomError`
180
+
181
+ Custom error class with:
182
+ - `code: string`
183
+ - `message: string`
184
+ - `description: string`
185
+
186
+ #### `ErrorComponentType`
187
+
188
+ Type for custom error components with props:
189
+ - `error: CustomError`
190
+ - `onClose: () => void`
191
+ - `closeOnClick?: boolean`
192
+ - `visiblityTime?: number`
193
+
194
+ ## Styling
195
+
196
+ The default error component comes with basic styling that can be overridden with CSS variables:
197
+
198
+ ```css
199
+ :root {
200
+ --tsila-err-color: #ff4444;
201
+ --tsila-err-bg: #fff;
202
+ --tsila-err-border: 1px solid #ff4444;
203
+ --tsila-err-padding: 1rem;
204
+ --tsila-err-border-radius: 4px;
205
+ --tsila-err-box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
206
+ }
207
+ ```
208
+
209
+ ## Examples
210
+
211
+ ### Handling API Errors
212
+
213
+ ```tsx
214
+ import { useThrowError } from '@heritsilavo/react-error-boundary';
215
+
216
+ function DataFetcher() {
217
+ const throwError = useThrowError();
218
+ const [data, setData] = useState(null);
219
+
220
+ const fetchData = async () => {
221
+ try {
222
+ const response = await fetch('/api/data');
223
+ if (!response.ok) {
224
+ throwError(
225
+ 'FETCH_FAILED',
226
+ 'Failed to load data',
227
+ `Server returned ${response.status}`
228
+ );
229
+ return;
230
+ }
231
+ setData(await response.json());
232
+ } catch (err) {
233
+ throwError(
234
+ 'NETWORK_ERROR',
235
+ 'Network request failed',
236
+ err.message
237
+ );
238
+ }
239
+ };
240
+
241
+ // ...
242
+ }
243
+ ```
244
+
245
+ ### Custom Error Boundary in Next.js
246
+
247
+ ```tsx
248
+ // pages/_app.tsx
249
+ import { ErrorProvider } from '@heritsilavo/react-error-boundary/next';
250
+ import { CustomErrorPage } from '../components/CustomErrorPage';
251
+
252
+ export default function App({ Component, pageProps }) {
253
+ return (
254
+ <ErrorProvider
255
+ ErrorComponent={CustomErrorPage}
256
+ autoHideDelay={0} // Disable auto-hide
257
+ closeErrorComponetOnClick={true}
258
+ >
259
+ <Component {...pageProps} />
260
+ </ErrorProvider>
261
+ );
262
+ }
263
+ ```
264
+
265
+ ## Contributing
266
+
267
+ Contributions are welcome! Please open an issue or submit a pull request.
268
+
269
+ ## License
270
+
271
+ ISC © [Heritsilavo](mailto:heritsilavo4835@gmail.com)
272
+ ```
@@ -0,0 +1,5 @@
1
+ export declare class CustomError extends Error {
2
+ code: string;
3
+ description: string;
4
+ constructor(code: string, message: string, description: string);
5
+ }
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ type WrapperPropsType = {
3
+ children: React.ReactNode;
4
+ };
5
+ export declare const ErrorBoundary: React.FC<WrapperPropsType>;
6
+ export {};
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { CustomError } from './CustomError';
3
+ import { ErrorComponentType } from './components/DefaultErrorComponents';
4
+ interface ContextType {
5
+ handleError: (error: Error) => void;
6
+ error: CustomError | null;
7
+ clearError: () => void;
8
+ }
9
+ interface ErrorProviderProps {
10
+ children: React.ReactNode;
11
+ ErrorComponent?: React.ComponentType<ErrorComponentType>;
12
+ autoHideDelay?: number;
13
+ onError?: () => void;
14
+ closeErrorComponetOnClick?: boolean;
15
+ }
16
+ export declare const ErrorProvider: React.FC<ErrorProviderProps>;
17
+ export declare const useError: () => ContextType;
18
+ export declare const useThrowError: () => (code: string, message: string, description: string) => void;
19
+ export {};
@@ -0,0 +1,42 @@
1
+ import { z } from "zod";
2
+ import './style.css';
3
+ import React from "react";
4
+ declare const ErrorComponentPropsSchema: z.ZodObject<{
5
+ error: z.ZodObject<{
6
+ code: z.ZodString;
7
+ message: z.ZodString;
8
+ description: z.ZodOptional<z.ZodString>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ code: string;
11
+ message: string;
12
+ description?: string | undefined;
13
+ }, {
14
+ code: string;
15
+ message: string;
16
+ description?: string | undefined;
17
+ }>;
18
+ onClose: z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodVoid>;
19
+ closeOnClick: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
20
+ visiblityTime: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ error: {
23
+ code: string;
24
+ message: string;
25
+ description?: string | undefined;
26
+ };
27
+ onClose: (...args: unknown[]) => void;
28
+ closeOnClick: boolean;
29
+ visiblityTime: number;
30
+ }, {
31
+ error: {
32
+ code: string;
33
+ message: string;
34
+ description?: string | undefined;
35
+ };
36
+ onClose: (...args: unknown[]) => void;
37
+ closeOnClick?: boolean | undefined;
38
+ visiblityTime?: number | undefined;
39
+ }>;
40
+ export type ErrorComponentType = z.infer<typeof ErrorComponentPropsSchema>;
41
+ export declare const DefaultErrorComponent: React.FC<ErrorComponentType>;
42
+ export default DefaultErrorComponent;
@@ -0,0 +1,4 @@
1
+ export * from "./CustomError";
2
+ export * from "./ErrorBoundary";
3
+ export * from "./ErrorContext";
4
+ export * from "./components/DefaultErrorComponents";