feat: Implement final error coverage

This commit is contained in:
gpt-engineer-app[bot]
2025-11-04 19:50:06 +00:00
parent a9334c7a3a
commit 0df047d56b
9 changed files with 291 additions and 403 deletions

View File

@@ -11,8 +11,7 @@ import type {
IdentitySafetyCheck,
IdentityOperationResult
} from '@/types/identity';
import { logger } from './logger';
import { getErrorMessage } from './errorHandler';
import { handleNonCriticalError, handleError, getErrorMessage } from './errorHandler';
/**
* Get all identities for the current user
@@ -25,10 +24,9 @@ export async function getUserIdentities(): Promise<UserIdentity[]> {
return (data?.identities || []) as UserIdentity[];
} catch (error) {
const errorMsg = getErrorMessage(error);
logger.error('Failed to get user identities', {
action: 'get_identities',
error: errorMsg
handleNonCriticalError(error, {
action: 'Get User Identities',
metadata: { returnedEmptyArray: true }
});
return [];
}
@@ -102,9 +100,9 @@ export async function disconnectIdentity(
// Get AAL level - fail closed on error
const { data: aalData, error: aalError } = await supabase.auth.mfa.getAuthenticatorAssuranceLevel();
if (aalError) {
logger.error('Failed to get AAL level for identity disconnect', {
action: 'disconnect_identity_aal_check',
error: aalError.message
handleNonCriticalError(aalError, {
action: 'Get AAL Level (Identity Disconnect)',
metadata: { failClosed: true }
});
return {
success: false,
@@ -120,9 +118,9 @@ export async function disconnectIdentity(
const { data: factors, error: factorsError } = await supabase.auth.mfa.listFactors();
if (factorsError) {
logger.error('Failed to list MFA factors for identity disconnect', {
action: 'disconnect_identity_mfa_check',
error: factorsError.message
handleNonCriticalError(factorsError, {
action: 'List MFA Factors (Identity Disconnect)',
metadata: { failClosed: true }
});
return {
success: false,
@@ -177,15 +175,13 @@ export async function disconnectIdentity(
return { success: true };
} catch (error) {
const errorMsg = getErrorMessage(error);
logger.error('Failed to disconnect identity', {
action: 'identity_disconnect',
provider,
error: errorMsg
handleError(error, {
action: 'Disconnect Identity',
metadata: { provider }
});
return {
success: false,
error: errorMsg
error: getErrorMessage(error)
};
}
}
@@ -210,15 +206,13 @@ export async function connectIdentity(
return { success: true };
} catch (error) {
const errorMsg = getErrorMessage(error);
logger.error('Failed to connect identity', {
action: 'identity_connect',
provider,
error: errorMsg
handleError(error, {
action: 'Connect Identity',
metadata: { provider }
});
return {
success: false,
error: errorMsg
error: getErrorMessage(error)
};
}
}
@@ -240,11 +234,6 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
};
}
logger.info('Initiating password setup', {
action: 'password_setup_initiated',
email: userEmail
});
// Trigger Supabase password reset email
// User clicks link and sets password, which automatically creates email identity
const { error: resetError } = await supabase.auth.resetPasswordForEmail(
@@ -255,20 +244,14 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
);
if (resetError) {
logger.error('Failed to send password reset email', {
handleError(resetError, {
action: 'Send Password Reset Email',
userId: user?.id,
action: 'password_setup_email',
error: resetError.message
metadata: { email: userEmail }
});
throw resetError;
}
logger.info('Password reset email sent', {
userId: user!.id,
action: 'password_setup_initiated',
email: userEmail
});
// Log the action
await logIdentityChange(user!.id, 'password_setup_initiated', {
method: 'reset_password_flow',
@@ -281,14 +264,12 @@ export async function addPasswordToAccount(): Promise<IdentityOperationResult> {
email: userEmail
};
} catch (error) {
const errorMsg = getErrorMessage(error);
logger.error('Failed to initiate password setup', {
action: 'password_setup',
error: errorMsg
handleError(error, {
action: 'Initiate Password Setup'
});
return {
success: false,
error: errorMsg
error: getErrorMessage(error)
};
}
}
@@ -310,10 +291,10 @@ async function logIdentityChange(
_details: details
});
} catch (error) {
logger.error('Failed to log identity change to audit', {
handleNonCriticalError(error, {
action: 'Log Identity Change to Audit',
userId,
action,
error: error instanceof Error ? error.message : String(error)
metadata: { auditAction: action }
});
// Don't fail the operation if audit logging fails
}