react-native-healthkit-bridge
Version:
A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries
123 lines (122 loc) • 5.27 kB
JavaScript
import { HEALTHKIT_CONFIG } from '../config/healthkit.config';
export class HealthKitErrorHandler {
static createError(code, message, context, details) {
return {
code,
message: message || HEALTHKIT_CONFIG.ERROR_MESSAGES[code],
context,
details,
timestamp: Date.now()
};
}
static handle(error, context) {
if (this.isHealthKitError(error)) {
return Object.assign(Object.assign({}, 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 === null || error === void 0 ? void 0 : error.message) || 'Erro desconhecido', context, error);
}
static isHealthKitError(error) {
return error &&
typeof error === 'object' &&
'code' in error &&
'message' in error &&
Object.values(HEALTHKIT_CONFIG.ERROR_CODES).includes(error.code);
}
static isNetworkError(error) {
var _a, _b, _c;
return error && (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('network')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('connection')) ||
((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('fetch')) ||
error.code === 'NETWORK_ERROR' ||
error.name === 'NetworkError');
}
static isAuthorizationError(error) {
var _a, _b, _c;
return error && (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('authorization')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('permission')) ||
((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('unauthorized')) ||
error.code === 'UNAUTHORIZED' ||
error.name === 'AuthorizationError');
}
static isTimeoutError(error) {
var _a, _b;
return error && (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('timeout')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('timed out')) ||
error.code === 'TIMEOUT' ||
error.name === 'TimeoutError');
}
static isValidationError(error) {
var _a, _b, _c;
return error && (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('validation')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('invalid')) ||
((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes('parameter')) ||
error.code === 'INVALID_PARAMETER' ||
error.name === 'ValidationError');
}
static isQueryError(error) {
var _a, _b;
return error && (((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('query')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('database')) ||
error.code === 'QUERY_FAILED' ||
error.name === 'QueryError');
}
static formatError(error) {
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, level = 'error') {
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) {
this.logError(error);
throw new Error(this.formatError(error));
}
static async withErrorHandling(operation, context, fallback) {
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));
}
}
}