naim-firebase-auth-wrapper
Version:
React components and hooks for Firebase Authentication and Firestore with Mantine UI
43 lines • 2.15 kB
JavaScript
/**
* Error handler for standardizing error handling across the app
*/
export class FirebaseAuthError extends Error {
constructor(message, code = 'unknown', originalError) {
super(message);
this.name = 'FirebaseAuthError';
this.code = code;
this.originalError = originalError;
// Maintains proper stack trace for where our error was thrown
if (Error.captureStackTrace) {
Error.captureStackTrace(this, FirebaseAuthError);
}
}
}
export const handleServiceError = (error, defaultMessage = 'An unexpected error occurred') => {
// Handle Firebase Auth errors
if (error && typeof error === 'object' && 'code' in error && typeof error.code === 'string') {
const firebaseError = error;
// Map common Firebase error codes to user-friendly messages
switch (firebaseError.code) {
case 'auth/user-not-found':
return new FirebaseAuthError('User not found. Please check your credentials.', firebaseError.code);
case 'auth/wrong-password':
return new FirebaseAuthError('Incorrect password. Please try again.', firebaseError.code);
case 'auth/email-already-in-use':
return new FirebaseAuthError('This email is already registered. Try logging in instead.', firebaseError.code);
case 'auth/weak-password':
return new FirebaseAuthError('Password is too weak. Please use a stronger password.', firebaseError.code);
case 'auth/invalid-email':
return new FirebaseAuthError('Invalid email format. Please enter a valid email.', firebaseError.code);
default:
return new FirebaseAuthError(firebaseError.message || defaultMessage, firebaseError.code, error instanceof Error ? error : undefined);
}
}
// For non-Firebase or generic errors
if (error instanceof Error) {
return new FirebaseAuthError(error.message, 'unknown', error);
}
// For completely unknown errors
return new FirebaseAuthError(defaultMessage);
};
//# sourceMappingURL=errorHandler.js.map