refactor: debug logs

This commit is contained in:
cogwheel0
2025-08-20 22:15:26 +05:30
parent 9a5c5a573f
commit 4dc9ce1762
27 changed files with 1965 additions and 2195 deletions

View File

@@ -1,7 +1,7 @@
import 'package:flutter/foundation.dart';
import 'schema_registry.dart';
import 'validation_result.dart';
import 'field_mapper.dart';
import '../utils/debug_logger.dart';
/// Comprehensive API request and response validator
/// Validates against OpenAPI specification schemas
@@ -24,9 +24,9 @@ class ApiValidator {
try {
await _schemaRegistry.loadSchemas();
_initialized = true;
debugPrint('ApiValidator: Successfully initialized with schemas');
DebugLogger.validation('Successfully initialized with schemas');
} catch (e) {
debugPrint('ApiValidator: Failed to initialize: $e');
DebugLogger.error('Failed to initialize', e);
// Continue without validation if schemas can't be loaded
}
}

View File

@@ -1,4 +1,4 @@
import 'package:flutter/foundation.dart';
import '../utils/debug_logger.dart';
/// Handles field name transformations between API and client formats
/// Converts between snake_case (API) and camelCase (client)
@@ -224,7 +224,7 @@ class FieldMapper {
void clearCache() {
_toCamelCaseCache.clear();
_toSnakeCaseCache.clear();
debugPrint('FieldMapper: Cleared transformation caches');
DebugLogger.validation('Cleared transformation caches');
}
/// Add custom field mapping
@@ -236,7 +236,7 @@ class FieldMapper {
_toCamelCaseCache.remove(apiField);
_toSnakeCaseCache.remove(clientField);
debugPrint('FieldMapper: Added custom mapping: $apiField <-> $clientField');
DebugLogger.validation('Added custom mapping: $apiField <-> $clientField');
}
/// Validate that field transformations are reversible
@@ -266,14 +266,14 @@ class FieldMapper {
}
if (errors.isNotEmpty) {
debugPrint('FieldMapper: Transformation validation errors:');
DebugLogger.error('Transformation validation errors:');
for (final error in errors) {
debugPrint(' $error');
DebugLogger.error(' $error');
}
return false;
}
debugPrint('FieldMapper: All transformations validated successfully');
DebugLogger.validation('All transformations validated successfully');
return true;
}
}

View File

@@ -1,7 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import '../utils/debug_logger.dart';
/// Registry for OpenAPI schemas
/// Loads and provides access to request/response schemas for validation
@@ -19,15 +19,15 @@ class SchemaRegistry {
/// Load schemas from OpenAPI specification
Future<void> loadSchemas() async {
try {
debugPrint('SchemaRegistry: Loading OpenAPI specification...');
DebugLogger.validation('Loading OpenAPI specification...');
// Try to load from assets first, then from file system as fallback
String openApiContent;
try {
openApiContent = await rootBundle.loadString('assets/openapi.json');
} catch (e) {
debugPrint(
'SchemaRegistry: Could not load from assets, trying file system...',
DebugLogger.warning(
'Could not load from assets, trying file system...',
);
// Fallback - in a real app you might load from network or local file
throw Exception('OpenAPI specification not found in assets');
@@ -35,14 +35,14 @@ class SchemaRegistry {
_openApiSpec = jsonDecode(openApiContent) as Map<String, dynamic>;
debugPrint(
'SchemaRegistry: Successfully loaded OpenAPI spec with ${_getPaths().length} paths',
DebugLogger.validation(
'Successfully loaded OpenAPI spec with ${_getPaths().length} paths',
);
// Pre-process and cache commonly used schemas
await _buildSchemaCache();
} catch (e) {
debugPrint('SchemaRegistry: Failed to load schemas: $e');
DebugLogger.error('Failed to load schemas', e);
rethrow;
}
}
@@ -86,8 +86,9 @@ class SchemaRegistry {
return schema;
} catch (e) {
debugPrint(
'SchemaRegistry: Error getting request schema for $method $endpoint: $e',
DebugLogger.error(
'Error getting request schema for $method $endpoint',
e,
);
return null;
}
@@ -146,8 +147,9 @@ class SchemaRegistry {
return schema;
} catch (e) {
debugPrint(
'SchemaRegistry: Error getting response schema for $method $endpoint ($code): $e',
DebugLogger.error(
'Error getting response schema for $method $endpoint ($code)',
e,
);
return null;
}
@@ -240,7 +242,7 @@ class SchemaRegistry {
/// Resolve $ref reference
Map<String, dynamic>? _resolveReference(String ref) {
if (!ref.startsWith('#/')) {
debugPrint('SchemaRegistry: External references not supported: $ref');
DebugLogger.warning('External references not supported: $ref');
return null;
}
@@ -251,7 +253,7 @@ class SchemaRegistry {
if (current is Map<String, dynamic> && current.containsKey(segment)) {
current = current[segment];
} else {
debugPrint('SchemaRegistry: Could not resolve reference: $ref');
DebugLogger.warning('Could not resolve reference: $ref');
return null;
}
}
@@ -329,9 +331,7 @@ class SchemaRegistry {
}
}
debugPrint(
'SchemaRegistry: Pre-cached schemas for $cachedCount operations',
);
DebugLogger.validation('Pre-cached schemas for $cachedCount operations');
}
/// Get all available endpoints

View File

@@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'api_validator.dart';
import 'validation_result.dart';
import '../utils/debug_logger.dart';
/// Dio interceptor for automatic API validation
/// Validates requests and responses against OpenAPI schemas
@@ -61,7 +62,7 @@ class ValidationInterceptor extends Interceptor {
);
return;
} else {
debugPrint('ValidationInterceptor: Request validation error: $e');
DebugLogger.error('Request validation error', e);
}
}
}
@@ -120,7 +121,7 @@ class ValidationInterceptor extends Interceptor {
);
return;
} else {
debugPrint('ValidationInterceptor: Response validation error: $e');
DebugLogger.error('Response validation error', e);
}
}
}
@@ -165,9 +166,7 @@ class ValidationInterceptor extends Interceptor {
err.response!.extra['validationResult'] = result;
}
} catch (e) {
debugPrint(
'ValidationInterceptor: Error response validation failed: $e',
);
DebugLogger.error('Error response validation failed', e);
}
}
@@ -187,21 +186,21 @@ class ValidationInterceptor extends Interceptor {
final statusText = statusCode != null ? ' ($statusCode)' : '';
final icon = result.isValid ? '' : '';
debugPrint(
DebugLogger.validation(
'$icon Validation $type: ${method.toUpperCase()} $path$statusText - ${result.status.name}',
);
if (result.hasErrors) {
debugPrint(' Errors: ${result.errors.join(', ')}');
DebugLogger.error(' Errors: ${result.errors.join(', ')}');
}
if (result.hasWarnings) {
debugPrint(' Warnings: ${result.warnings.join(', ')}');
DebugLogger.warning(' Warnings: ${result.warnings.join(', ')}');
}
if (result.message.isNotEmpty &&
result.status != ValidationStatus.success) {
debugPrint(' Message: ${result.message}');
DebugLogger.info(' Message: ${result.message}');
}
}