react-native-healthkit-bridge
Version:
A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries
190 lines (166 loc) • 4.87 kB
text/typescript
import { HEALTHKIT_CONFIG } from '../config/healthkit.config';
import { HealthKitError } from '../core/types/HealthKitTypes';
import { HealthKitErrorCode } from '../config/healthkit.config';
export class HealthKitErrorHandler {
static createError(
code: HealthKitErrorCode,
message?: string,
context?: string,
details?: any
): HealthKitError {
return {
code,
message: message || HEALTHKIT_CONFIG.ERROR_MESSAGES[code as keyof typeof HEALTHKIT_CONFIG.ERROR_MESSAGES],
context,
details,
timestamp: Date.now()
};
}
static handle(error: any, context: string): HealthKitError {
if (this.isHealthKitError(error)) {
return {
...error,
context: context || error.context,
timestamp: error.timestamp || Date.now()
};
}
if (this.isNetworkError(error)) {
return this.createError(
HEALTHKIT_CONFIG.ERROR_CODES.NETWORK_ERROR,
error.message,
context,
error
);
}
if (this.isAuthorizationError(error)) {
return this.createError(
HEALTHKIT_CONFIG.ERROR_CODES.UNAUTHORIZED,
error.message,
context,
error
);
}
if (this.isTimeoutError(error)) {
return this.createError(
HEALTHKIT_CONFIG.ERROR_CODES.TIMEOUT,
error.message,
context,
error
);
}
if (this.isValidationError(error)) {
return this.createError(
HEALTHKIT_CONFIG.ERROR_CODES.INVALID_PARAMETER,
error.message,
context,
error
);
}
return this.createError(
HEALTHKIT_CONFIG.ERROR_CODES.UNKNOWN_ERROR,
error?.message || 'Erro desconhecido',
context,
error
);
}
static isHealthKitError(error: any): error is HealthKitError {
return error &&
typeof error === 'object' &&
'code' in error &&
'message' in error &&
Object.values(HEALTHKIT_CONFIG.ERROR_CODES).includes(error.code);
}
static isNetworkError(error: any): boolean {
return error && (
error.message?.includes('network') ||
error.message?.includes('connection') ||
error.message?.includes('fetch') ||
error.code === 'NETWORK_ERROR' ||
error.name === 'NetworkError'
);
}
static isAuthorizationError(error: any): boolean {
return error && (
error.message?.includes('authorization') ||
error.message?.includes('permission') ||
error.message?.includes('unauthorized') ||
error.code === 'UNAUTHORIZED' ||
error.name === 'AuthorizationError'
);
}
static isTimeoutError(error: any): boolean {
return error && (
error.message?.includes('timeout') ||
error.message?.includes('timed out') ||
error.code === 'TIMEOUT' ||
error.name === 'TimeoutError'
);
}
static isValidationError(error: any): boolean {
return error && (
error.message?.includes('validation') ||
error.message?.includes('invalid') ||
error.message?.includes('parameter') ||
error.code === 'INVALID_PARAMETER' ||
error.name === 'ValidationError'
);
}
static isQueryError(error: any): boolean {
return error && (
error.message?.includes('query') ||
error.message?.includes('database') ||
error.code === 'QUERY_FAILED' ||
error.name === 'QueryError'
);
}
static formatError(error: HealthKitError): string {
let message = `[${error.code}] ${error.message}`;
if (error.context) {
message += ` (Contexto: ${error.context})`;
}
if (error.details) {
message += `\nDetalhes: ${JSON.stringify(error.details, null, 2)}`;
}
return message;
}
static logError(error: HealthKitError, level: 'debug' | 'info' | 'warn' | 'error' = 'error'): void {
if (!HEALTHKIT_CONFIG.LOGGING_ENABLED) {
return;
}
const message = this.formatError(error);
switch (level) {
case 'debug':
console.debug(message);
break;
case 'info':
console.info(message);
break;
case 'warn':
console.warn(message);
break;
case 'error':
console.error(message);
break;
}
}
static throwError(error: HealthKitError): never {
this.logError(error);
throw new Error(this.formatError(error));
}
static async withErrorHandling<T>(
operation: () => Promise<T>,
context: string,
fallback?: T
): Promise<T> {
try {
return await operation();
} catch (error) {
const healthKitError = this.handle(error, context);
this.logError(healthKitError);
if (fallback !== undefined) {
return fallback;
}
throw new Error(this.formatError(healthKitError));
}
}
}