@umituz/react-native-firebase 2.5.1 → 2.6.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.
- package/package.json +1 -1
- package/src/application/auth/index.ts +42 -0
- package/src/application/auth/ports/AuthPort.ts +164 -0
- package/src/application/auth/use-cases/SignInUseCase.ts +253 -0
- package/src/application/auth/use-cases/SignOutUseCase.ts +288 -0
- package/src/application/auth/use-cases/index.ts +26 -0
- package/src/domains/account-deletion/domain/index.ts +15 -0
- package/src/domains/account-deletion/domain/services/UserValidationService.ts +295 -0
- package/src/domains/account-deletion/index.ts +43 -6
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor.ts +230 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler.ts +174 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository.ts +266 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionTypes.ts +33 -0
- package/src/domains/account-deletion/infrastructure/services/account-deletion.service.ts +39 -227
- package/src/domains/auth/domain.ts +16 -0
- package/src/domains/auth/index.ts +7 -148
- package/src/domains/auth/infrastructure.ts +156 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService.ts +247 -0
- package/src/domains/auth/presentation/hooks/useGoogleOAuth.ts +49 -103
- package/src/domains/auth/presentation.ts +25 -0
- package/src/domains/firestore/domain/entities/Collection.ts +288 -0
- package/src/domains/firestore/domain/entities/Document.ts +233 -0
- package/src/domains/firestore/domain/index.ts +30 -0
- package/src/domains/firestore/domain/services/QueryService.ts +182 -0
- package/src/domains/firestore/domain/services/QueryServiceAnalysis.ts +169 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers.ts +151 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts +191 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +320 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization.ts +207 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation.ts +182 -0
- package/src/domains/firestore/domain/value-objects/WhereClause.ts +299 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory.ts +207 -0
- package/src/domains/firestore/index.ts +20 -6
- package/src/index.ts +25 -0
- package/src/shared/domain/utils/calculation.util.ts +17 -305
- package/src/shared/domain/utils/error-handlers/error-messages.ts +11 -0
- package/src/shared/domain/utils/index.ts +0 -5
- package/src/shared/infrastructure/base/ErrorHandler.ts +189 -0
- package/src/shared/infrastructure/base/ServiceBase.ts +220 -0
- package/src/shared/infrastructure/base/TypedGuard.ts +131 -0
- package/src/shared/infrastructure/base/index.ts +34 -0
- package/src/shared/infrastructure/config/state/FirebaseClientState.ts +34 -12
- package/src/shared/infrastructure/config/base/ClientStateManager.ts +0 -82
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Calculation Utilities
|
|
3
|
-
* Common mathematical operations used across the codebase
|
|
4
|
-
* Optimized for performance with minimal allocations
|
|
5
3
|
*/
|
|
6
4
|
|
|
7
|
-
/**
|
|
8
|
-
* Safely calculates percentage (0-1 range)
|
|
9
|
-
* Optimized: Guards against division by zero
|
|
10
|
-
*
|
|
11
|
-
* @param current - Current value
|
|
12
|
-
* @param limit - Maximum value
|
|
13
|
-
* @returns Percentage between 0 and 1
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const percentage = calculatePercentage(750, 1000); // 0.75
|
|
18
|
-
* const percentage = calculatePercentage(1200, 1000); // 1.0 (capped)
|
|
19
|
-
* const percentage = calculatePercentage(0, 1000); // 0.0
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
5
|
export function calculatePercentage(current: number, limit: number): number {
|
|
23
6
|
if (limit <= 0) return 0;
|
|
24
7
|
if (current <= 0) return 0;
|
|
@@ -26,327 +9,56 @@ export function calculatePercentage(current: number, limit: number): number {
|
|
|
26
9
|
return current / limit;
|
|
27
10
|
}
|
|
28
11
|
|
|
29
|
-
/**
|
|
30
|
-
* Calculates remaining quota
|
|
31
|
-
* Optimized: Single Math.max call
|
|
32
|
-
*
|
|
33
|
-
* @param current - Current usage
|
|
34
|
-
* @param limit - Maximum limit
|
|
35
|
-
* @returns Remaining amount (minimum 0)
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* const remaining = calculateRemaining(250, 1000); // 750
|
|
40
|
-
* const remaining = calculateRemaining(1200, 1000); // 0 (capped)
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
12
|
export function calculateRemaining(current: number, limit: number): number {
|
|
44
13
|
return Math.max(0, limit - current);
|
|
45
14
|
}
|
|
46
15
|
|
|
47
|
-
/**
|
|
48
|
-
* Safe floor with minimum value
|
|
49
|
-
* Optimized: Single comparison
|
|
50
|
-
*
|
|
51
|
-
* @param value - Value to floor
|
|
52
|
-
* @param min - Minimum allowed value
|
|
53
|
-
* @returns Floored value, at least min
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const result = safeFloor(5.7, 1); // 5
|
|
58
|
-
* const result = safeFloor(0.3, 1); // 1 (min enforced)
|
|
59
|
-
* const result = safeFloor(-2.5, 0); // 0 (min enforced)
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
16
|
export function safeFloor(value: number, min: number): number {
|
|
63
17
|
const floored = Math.floor(value);
|
|
64
18
|
return floored < min ? min : floored;
|
|
65
19
|
}
|
|
66
20
|
|
|
67
|
-
/**
|
|
68
|
-
* Safe ceil with maximum value
|
|
69
|
-
* Optimized: Single comparison
|
|
70
|
-
*
|
|
71
|
-
* @param value - Value to ceil
|
|
72
|
-
* @param max - Maximum allowed value
|
|
73
|
-
* @returns Ceiled value, at most max
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* const result = safeCeil(5.2, 10); // 6
|
|
78
|
-
* const result = safeCeil(9.8, 10); // 10 (max enforced)
|
|
79
|
-
* const result = safeCeil(12.1, 10); // 10 (max enforced)
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export function safeCeil(value: number, max: number): number {
|
|
83
|
-
const ceiled = Math.ceil(value);
|
|
84
|
-
return ceiled > max ? max : ceiled;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Clamp value between min and max
|
|
89
|
-
* Optimized: Efficient without branching
|
|
90
|
-
*
|
|
91
|
-
* @param value - Value to clamp
|
|
92
|
-
* @param min - Minimum value
|
|
93
|
-
* @param max - Maximum value
|
|
94
|
-
* @returns Clamped value
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* const result = clamp(5, 0, 10); // 5
|
|
99
|
-
* const result = clamp(-5, 0, 10); // 0
|
|
100
|
-
* const result = clamp(15, 0, 10); // 10
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
export function clamp(value: number, min: number, max: number): number {
|
|
104
|
-
return Math.max(min, Math.min(value, max));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Calculate milliseconds between two dates
|
|
109
|
-
* Optimized: Direct subtraction without Date object creation
|
|
110
|
-
*
|
|
111
|
-
* @param date1 - First date (timestamp or Date)
|
|
112
|
-
* @param date2 - Second date (timestamp or Date)
|
|
113
|
-
* @returns Difference in milliseconds (date1 - date2)
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* const diff = diffMs(Date.now(), Date.now() - 3600000); // 3600000
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
21
|
export function diffMs(date1: number | Date, date2: number | Date): number {
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
return
|
|
22
|
+
const d1 = typeof date1 === 'number' ? date1 : date1.getTime();
|
|
23
|
+
const d2 = typeof date2 === 'number' ? date2 : date2.getTime();
|
|
24
|
+
return Math.abs(d1 - d2);
|
|
124
25
|
}
|
|
125
26
|
|
|
126
|
-
/**
|
|
127
|
-
* Calculate minutes difference between two dates
|
|
128
|
-
* Optimized: Single Math.floor call
|
|
129
|
-
*
|
|
130
|
-
* @param date1 - First date
|
|
131
|
-
* @param date2 - Second date
|
|
132
|
-
* @returns Difference in minutes (floored)
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* ```typescript
|
|
136
|
-
* const diff = diffMinutes(Date.now(), Date.now() - 180000); // 3
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
27
|
export function diffMinutes(date1: number | Date, date2: number | Date): number {
|
|
140
|
-
|
|
141
|
-
return Math.floor(msDiff / 60_000);
|
|
28
|
+
return Math.floor(diffMs(date1, date2) / 60000);
|
|
142
29
|
}
|
|
143
30
|
|
|
144
|
-
/**
|
|
145
|
-
* Calculate hours difference between two dates
|
|
146
|
-
* Optimized: Single Math.floor call
|
|
147
|
-
*
|
|
148
|
-
* @param date1 - First date
|
|
149
|
-
* @param date2 - Second date
|
|
150
|
-
* @returns Difference in hours (floored)
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```typescript
|
|
154
|
-
* const diff = diffHours(Date.now(), Date.now() - 7200000); // 2
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
31
|
export function diffHours(date1: number | Date, date2: number | Date): number {
|
|
158
|
-
|
|
159
|
-
return Math.floor(minsDiff / 60);
|
|
32
|
+
return Math.floor(diffMs(date1, date2) / 3600000);
|
|
160
33
|
}
|
|
161
34
|
|
|
162
|
-
/**
|
|
163
|
-
* Calculate days difference between two dates
|
|
164
|
-
* Optimized: Single Math.floor call
|
|
165
|
-
*
|
|
166
|
-
* @param date1 - First date
|
|
167
|
-
* @param date2 - Second date
|
|
168
|
-
* @returns Difference in days (floored)
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```typescript
|
|
172
|
-
* const diff = diffDays(Date.now(), Date.now() - 172800000); // 2
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
35
|
export function diffDays(date1: number | Date, date2: number | Date): number {
|
|
176
|
-
|
|
177
|
-
return Math.floor(hoursDiff / 24);
|
|
36
|
+
return Math.floor(diffMs(date1, date2) / 86400000);
|
|
178
37
|
}
|
|
179
38
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
* @param start - Start index (inclusive)
|
|
186
|
-
* @param end - End index (exclusive)
|
|
187
|
-
* @returns Sliced array
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* const items = [1, 2, 3, 4, 5];
|
|
192
|
-
* const sliced = safeSlice(items, 1, 3); // [2, 3]
|
|
193
|
-
* const sliced = safeSlice(items, -5, 10); // [1, 2, 3, 4, 5] (bounds checked)
|
|
194
|
-
* ```
|
|
195
|
-
*/
|
|
196
|
-
export function safeSlice<T>(array: T[], start: number, end?: number): T[] {
|
|
197
|
-
const len = array.length;
|
|
198
|
-
|
|
199
|
-
// Clamp start index
|
|
200
|
-
const safeStart = start < 0 ? 0 : (start >= len ? len : start);
|
|
201
|
-
|
|
202
|
-
// Clamp end index
|
|
203
|
-
const safeEnd = end === undefined
|
|
204
|
-
? len
|
|
205
|
-
: (end < 0 ? 0 : (end >= len ? len : end));
|
|
206
|
-
|
|
207
|
-
// Only slice if valid range
|
|
208
|
-
if (safeStart >= safeEnd) {
|
|
209
|
-
return [];
|
|
39
|
+
export function chunkArray<T>(array: readonly T[], chunkSize: number): T[][] {
|
|
40
|
+
if (chunkSize <= 0) return [];
|
|
41
|
+
const result: T[][] = [];
|
|
42
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
43
|
+
result.push(array.slice(i, i + chunkSize) as T[]);
|
|
210
44
|
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
211
47
|
|
|
212
|
-
|
|
48
|
+
export function safeSlice<T>(array: T[], start: number, end?: number): T[] {
|
|
49
|
+
if (start < 0) return array.slice(0, end);
|
|
50
|
+
if (start >= array.length) return [];
|
|
51
|
+
return array.slice(start, end);
|
|
213
52
|
}
|
|
214
53
|
|
|
215
|
-
/**
|
|
216
|
-
* Calculate fetch limit for pagination (pageLimit + 1)
|
|
217
|
-
* Optimized: Simple addition
|
|
218
|
-
*
|
|
219
|
-
* @param pageLimit - Requested page size
|
|
220
|
-
* @returns Fetch limit (pageLimit + 1 for hasMore detection)
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* const fetchLimit = getFetchLimit(10); // 11
|
|
225
|
-
* ```
|
|
226
|
-
*/
|
|
227
54
|
export function getFetchLimit(pageLimit: number): number {
|
|
228
55
|
return pageLimit + 1;
|
|
229
56
|
}
|
|
230
57
|
|
|
231
|
-
/**
|
|
232
|
-
* Calculate if hasMore based on items length and page limit
|
|
233
|
-
* Optimized: Direct comparison
|
|
234
|
-
*
|
|
235
|
-
* @param itemsLength - Total items fetched
|
|
236
|
-
* @param pageLimit - Requested page size
|
|
237
|
-
* @returns true if there are more items
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* ```typescript
|
|
241
|
-
* const hasMore = hasMore(11, 10); // true
|
|
242
|
-
* const hasMore = hasMore(10, 10); // false
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
58
|
export function hasMore(itemsLength: number, pageLimit: number): boolean {
|
|
246
59
|
return itemsLength > pageLimit;
|
|
247
60
|
}
|
|
248
61
|
|
|
249
|
-
/**
|
|
250
|
-
* Calculate result items count (min of itemsLength and pageLimit)
|
|
251
|
-
* Optimized: Single Math.min call
|
|
252
|
-
*
|
|
253
|
-
* @param itemsLength - Total items fetched
|
|
254
|
-
* @param pageLimit - Requested page size
|
|
255
|
-
* @returns Number of items to return
|
|
256
|
-
*
|
|
257
|
-
* @example
|
|
258
|
-
* ```typescript
|
|
259
|
-
* const count = getResultCount(11, 10); // 10
|
|
260
|
-
* const count = getResultCount(8, 10); // 8
|
|
261
|
-
* ```
|
|
262
|
-
*/
|
|
263
62
|
export function getResultCount(itemsLength: number, pageLimit: number): number {
|
|
264
63
|
return Math.min(itemsLength, pageLimit);
|
|
265
64
|
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Chunk array into smaller arrays
|
|
269
|
-
* Optimized: Pre-allocated chunks when size is known
|
|
270
|
-
*
|
|
271
|
-
* @param array - Array to chunk
|
|
272
|
-
* @param chunkSize - Size of each chunk
|
|
273
|
-
* @returns Array of chunks
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```typescript
|
|
277
|
-
* const chunks = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
278
|
-
* ```
|
|
279
|
-
*/
|
|
280
|
-
export function chunkArray<T>(array: readonly T[], chunkSize: number): T[][] {
|
|
281
|
-
if (chunkSize <= 0) {
|
|
282
|
-
throw new Error('chunkSize must be greater than 0');
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const chunks: T[][] = [];
|
|
286
|
-
const len = array.length;
|
|
287
|
-
|
|
288
|
-
for (let i = 0; i < len; i += chunkSize) {
|
|
289
|
-
const end = Math.min(i + chunkSize, len);
|
|
290
|
-
chunks.push(array.slice(i, end) as T[]);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return chunks;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Sum array of numbers
|
|
298
|
-
* Optimized: Direct for-loop (faster than reduce)
|
|
299
|
-
*
|
|
300
|
-
* @param numbers - Array of numbers to sum
|
|
301
|
-
* @returns Sum of all numbers
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* ```typescript
|
|
305
|
-
* const sum = sumArray([1, 2, 3, 4, 5]); // 15
|
|
306
|
-
* ```
|
|
307
|
-
*/
|
|
308
|
-
export function sumArray(numbers: number[]): number {
|
|
309
|
-
let sum = 0;
|
|
310
|
-
for (let i = 0; i < numbers.length; i++) {
|
|
311
|
-
const num = numbers[i];
|
|
312
|
-
if (num !== undefined && num !== null) {
|
|
313
|
-
sum += num;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return sum;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Average of array of numbers
|
|
321
|
-
* Optimized: Single-pass calculation
|
|
322
|
-
*
|
|
323
|
-
* @param numbers - Array of numbers
|
|
324
|
-
* @returns Average value
|
|
325
|
-
*
|
|
326
|
-
* @example
|
|
327
|
-
* ```typescript
|
|
328
|
-
* const avg = averageArray([1, 2, 3, 4, 5]); // 3
|
|
329
|
-
* ```
|
|
330
|
-
*/
|
|
331
|
-
export function averageArray(numbers: number[]): number {
|
|
332
|
-
if (numbers.length === 0) return 0;
|
|
333
|
-
return sumArray(numbers) / numbers.length;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Round to decimal places
|
|
338
|
-
* Optimized: Efficient rounding without string conversion
|
|
339
|
-
*
|
|
340
|
-
* @param value - Value to round
|
|
341
|
-
* @param decimals - Number of decimal places
|
|
342
|
-
* @returns Rounded value
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* ```typescript
|
|
346
|
-
* const rounded = roundToDecimals(3.14159, 2); // 3.14
|
|
347
|
-
* ```
|
|
348
|
-
*/
|
|
349
|
-
export function roundToDecimals(value: number, decimals: number): number {
|
|
350
|
-
const multiplier = Math.pow(10, decimals);
|
|
351
|
-
return Math.round(value * multiplier) / multiplier;
|
|
352
|
-
}
|
|
@@ -13,6 +13,11 @@ export const ERROR_MESSAGES = {
|
|
|
13
13
|
INVALID_CREDENTIALS: 'Invalid credentials provided',
|
|
14
14
|
USER_CHANGED: 'User changed during operation',
|
|
15
15
|
OPERATION_IN_PROGRESS: 'Operation already in progress',
|
|
16
|
+
USER_NOT_FOUND: 'User not found',
|
|
17
|
+
EMAIL_ALREADY_IN_USE: 'Email is already in use',
|
|
18
|
+
TOO_MANY_REQUESTS: 'Too many requests. Please try again later',
|
|
19
|
+
USER_DISABLED: 'This account has been disabled',
|
|
20
|
+
GENERIC_ERROR: 'Authentication error occurred',
|
|
16
21
|
},
|
|
17
22
|
FIRESTORE: {
|
|
18
23
|
NOT_INITIALIZED: 'Firestore is not initialized',
|
|
@@ -25,6 +30,12 @@ export const ERROR_MESSAGES = {
|
|
|
25
30
|
PERMISSION_DENIED: 'Permission denied',
|
|
26
31
|
TRANSACTION_FAILED: 'Transaction failed',
|
|
27
32
|
NETWORK_ERROR: 'Network error occurred',
|
|
33
|
+
NOT_FOUND: 'Document not found',
|
|
34
|
+
UNAVAILABLE: 'Firestore service is unavailable',
|
|
35
|
+
GENERIC_ERROR: 'Firestore error occurred',
|
|
36
|
+
},
|
|
37
|
+
STORAGE: {
|
|
38
|
+
GENERIC_ERROR: 'Storage error occurred',
|
|
28
39
|
},
|
|
29
40
|
REPOSITORY: {
|
|
30
41
|
DESTROYED: 'Repository has been destroyed',
|
|
@@ -37,8 +37,6 @@ export {
|
|
|
37
37
|
calculatePercentage,
|
|
38
38
|
calculateRemaining,
|
|
39
39
|
safeFloor,
|
|
40
|
-
safeCeil,
|
|
41
|
-
clamp,
|
|
42
40
|
diffMs,
|
|
43
41
|
diffMinutes,
|
|
44
42
|
diffHours,
|
|
@@ -48,7 +46,4 @@ export {
|
|
|
48
46
|
hasMore,
|
|
49
47
|
getResultCount,
|
|
50
48
|
chunkArray,
|
|
51
|
-
sumArray,
|
|
52
|
-
averageArray,
|
|
53
|
-
roundToDecimals,
|
|
54
49
|
} from './calculation.util';
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Handler Base Class
|
|
3
|
+
* Single Responsibility: Centralized error handling and conversion
|
|
4
|
+
*
|
|
5
|
+
* Eliminates try-catch duplication across 9+ files.
|
|
6
|
+
* Provides standardized error-to-ErrorInfo conversion.
|
|
7
|
+
* Integrates with existing Result pattern.
|
|
8
|
+
*
|
|
9
|
+
* Max lines: 150 (enforced for maintainability)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ErrorInfo } from '../../domain/utils';
|
|
13
|
+
import { toErrorInfo, successResult, type Result } from '../../domain/utils';
|
|
14
|
+
import { isCancelledError, isQuotaError, isRetryableError } from '../../domain/utils/error-handlers/error-checkers';
|
|
15
|
+
import { ERROR_MESSAGES } from '../../domain/utils/error-handlers/error-messages';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Error handler options
|
|
19
|
+
*/
|
|
20
|
+
export interface ErrorHandlerOptions {
|
|
21
|
+
/** Default error code if none can be extracted */
|
|
22
|
+
defaultErrorCode?: string;
|
|
23
|
+
/** Custom error message mapper */
|
|
24
|
+
errorMapper?: (error: unknown) => string | null;
|
|
25
|
+
/** Enable detailed logging in development */
|
|
26
|
+
enableDevLogging?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Base class for error handling
|
|
31
|
+
* Provides centralized error conversion and handling
|
|
32
|
+
*/
|
|
33
|
+
export class ErrorHandler {
|
|
34
|
+
private readonly options: ErrorHandlerOptions;
|
|
35
|
+
|
|
36
|
+
constructor(options: ErrorHandlerOptions = {}) {
|
|
37
|
+
this.options = {
|
|
38
|
+
defaultErrorCode: 'unknown/error',
|
|
39
|
+
enableDevLogging: __DEV__,
|
|
40
|
+
...options,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Wrap an async operation with standardized error handling
|
|
46
|
+
* Eliminates try-catch duplication across services
|
|
47
|
+
*/
|
|
48
|
+
async handleAsync<T>(
|
|
49
|
+
operation: () => Promise<T>,
|
|
50
|
+
errorCode: string = this.options.defaultErrorCode || 'unknown/error'
|
|
51
|
+
): Promise<Result<T>> {
|
|
52
|
+
try {
|
|
53
|
+
const result = await operation();
|
|
54
|
+
return successResult(result);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (this.options.enableDevLogging) {
|
|
57
|
+
console.error(`[ErrorHandler] Operation failed: ${errorCode}`, error);
|
|
58
|
+
}
|
|
59
|
+
return { success: false, error: toErrorInfo(error, errorCode) };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Wrap a synchronous operation with standardized error handling
|
|
65
|
+
*/
|
|
66
|
+
handle<T>(
|
|
67
|
+
operation: () => T,
|
|
68
|
+
errorCode: string = this.options.defaultErrorCode || 'unknown/error'
|
|
69
|
+
): Result<T> {
|
|
70
|
+
try {
|
|
71
|
+
const result = operation();
|
|
72
|
+
return successResult(result);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (this.options.enableDevLogging) {
|
|
75
|
+
console.error(`[ErrorHandler] Operation failed: ${errorCode}`, error);
|
|
76
|
+
}
|
|
77
|
+
return { success: false, error: toErrorInfo(error, errorCode) };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Convert error to ErrorInfo with optional custom code
|
|
83
|
+
*/
|
|
84
|
+
toErrorInfo(error: unknown, code?: string): ErrorInfo {
|
|
85
|
+
// Try custom error mapper first
|
|
86
|
+
if (this.options.errorMapper) {
|
|
87
|
+
const customMessage = this.options.errorMapper(error);
|
|
88
|
+
if (customMessage) {
|
|
89
|
+
return {
|
|
90
|
+
code: code || this.options.defaultErrorCode || 'unknown/error',
|
|
91
|
+
message: customMessage,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Use standard conversion
|
|
97
|
+
return toErrorInfo(error, code || this.options.defaultErrorCode);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Check if error is a cancelled operation
|
|
102
|
+
*/
|
|
103
|
+
isCancelledError(error: unknown): boolean {
|
|
104
|
+
return isCancelledError(error);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Check if error is a quota error
|
|
109
|
+
*/
|
|
110
|
+
isQuotaError(error: unknown): boolean {
|
|
111
|
+
return isQuotaError(error);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Check if error is retryable
|
|
116
|
+
*/
|
|
117
|
+
isRetryableError(error: unknown): boolean {
|
|
118
|
+
return isRetryableError(error);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Get user-friendly error message
|
|
123
|
+
* Maps error codes to user-friendly messages
|
|
124
|
+
*/
|
|
125
|
+
getUserMessage(error: ErrorInfo): string {
|
|
126
|
+
// Check if it's a known error category
|
|
127
|
+
if (error.code.startsWith('auth/')) {
|
|
128
|
+
return this.getAuthUserMessage(error.code);
|
|
129
|
+
}
|
|
130
|
+
if (error.code.startsWith('firestore/')) {
|
|
131
|
+
return this.getFirestoreUserMessage(error.code);
|
|
132
|
+
}
|
|
133
|
+
if (error.code.startsWith('storage/')) {
|
|
134
|
+
return ERROR_MESSAGES.STORAGE.GENERIC_ERROR;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return error.message || ERROR_MESSAGES.GENERAL.UNKNOWN;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Get user-friendly auth error message
|
|
142
|
+
*/
|
|
143
|
+
private getAuthUserMessage(code: string): string {
|
|
144
|
+
// Map common auth error codes to user-friendly messages
|
|
145
|
+
const authMessages: Record<string, string> = {
|
|
146
|
+
'auth/user-not-found': ERROR_MESSAGES.AUTH.USER_NOT_FOUND,
|
|
147
|
+
'auth/wrong-password': ERROR_MESSAGES.AUTH.INVALID_CREDENTIALS,
|
|
148
|
+
'auth/email-already-in-use': ERROR_MESSAGES.AUTH.EMAIL_ALREADY_IN_USE,
|
|
149
|
+
'auth/invalid-email': ERROR_MESSAGES.AUTH.INVALID_EMAIL,
|
|
150
|
+
'auth/weak-password': ERROR_MESSAGES.AUTH.WEAK_PASSWORD,
|
|
151
|
+
'auth/too-many-requests': ERROR_MESSAGES.AUTH.TOO_MANY_REQUESTS,
|
|
152
|
+
'auth/user-disabled': ERROR_MESSAGES.AUTH.USER_DISABLED,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return authMessages[code] || ERROR_MESSAGES.AUTH.GENERIC_ERROR;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Get user-friendly firestore error message
|
|
160
|
+
*/
|
|
161
|
+
private getFirestoreUserMessage(code: string): string {
|
|
162
|
+
const firestoreMessages: Record<string, string> = {
|
|
163
|
+
'firestore/permission-denied': ERROR_MESSAGES.FIRESTORE.PERMISSION_DENIED,
|
|
164
|
+
'firestore/not-found': ERROR_MESSAGES.FIRESTORE.NOT_FOUND,
|
|
165
|
+
'firestore/unavailable': ERROR_MESSAGES.FIRESTORE.UNAVAILABLE,
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
return firestoreMessages[code] || ERROR_MESSAGES.FIRESTORE.GENERIC_ERROR;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Create a failure result from error code
|
|
173
|
+
*/
|
|
174
|
+
failureFrom(code: string, message?: string): Result {
|
|
175
|
+
return {
|
|
176
|
+
success: false,
|
|
177
|
+
error: {
|
|
178
|
+
code,
|
|
179
|
+
message: message || ERROR_MESSAGES.GENERAL.UNKNOWN,
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Default error handler instance
|
|
187
|
+
* Can be used across the application
|
|
188
|
+
*/
|
|
189
|
+
export const defaultErrorHandler = new ErrorHandler();
|