Skip to content

Commit 85a289b

Browse files
committed
fix(api): replace print statements with logger
- Replaced `print` statements with `_logger` - Improved error handling and logging - Added stack traces to error logs - Used more specific log levels - Improved log message clarity
1 parent 170539f commit 85a289b

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

routes/api/v1/data/[id]/index.dart

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import 'package:ht_api/src/services/dashboard_summary_service.dart';
88
import 'package:ht_api/src/services/user_preference_limit_service.dart'; // Import UserPreferenceLimitService
99
import 'package:ht_data_repository/ht_data_repository.dart';
1010
import 'package:ht_shared/ht_shared.dart';
11+
import 'package:logging/logging.dart';
12+
13+
// Create a logger for this file.
14+
final _logger = Logger('data_item_handler');
1115

1216
/// Handles requests for the /api/v1/data/[id] endpoint.
1317
/// Dispatches requests to specific handlers based on the HTTP method.
@@ -136,7 +140,7 @@ Future<Response> _handleGet(
136140
!permissionService.isAdmin(authenticatedUser)) {
137141
// Ensure getOwnerId is provided for models requiring ownership check
138142
if (modelConfig.getOwnerId == null) {
139-
print(
143+
_logger.severe(
140144
'Configuration Error: Model "$modelName" requires '
141145
'ownership check for GET item but getOwnerId is not provided.',
142146
);
@@ -192,9 +196,9 @@ Future<Response> _handlePut(
192196
dynamic itemToUpdate;
193197
try {
194198
itemToUpdate = modelConfig.fromJson(requestBody);
195-
} on TypeError catch (e) {
199+
} on TypeError catch (e, s) {
196200
// Catch errors during deserialization (e.g., missing required fields)
197-
print('Deserialization TypeError in PUT /data/[id]: $e');
201+
_logger.warning('Deserialization TypeError in PUT /data/[id]', e, s);
198202
// Throw BadRequestException to be caught by the errorHandler
199203
throw const BadRequestException(
200204
'Invalid request body: Missing or invalid required field(s).',
@@ -214,7 +218,7 @@ Future<Response> _handlePut(
214218
} catch (e) {
215219
// Ignore if getId throws, means ID might not be in the body,
216220
// which is acceptable depending on the model/client.
217-
print('Warning: Could not get ID from PUT body: $e');
221+
_logger.info('Could not get ID from PUT body: $e');
218222
}
219223

220224
// --- Handler-Level Limit Check (for UserContentPreferences PUT) ---
@@ -224,7 +228,7 @@ Future<Response> _handlePut(
224228
try {
225229
// Ensure the itemToUpdate is the correct type for the limit service
226230
if (itemToUpdate is! UserContentPreferences) {
227-
print(
231+
_logger.severe(
228232
'Type Error: Expected UserContentPreferences '
229233
'for limit check, but got ${itemToUpdate.runtimeType}.',
230234
);
@@ -239,11 +243,13 @@ Future<Response> _handlePut(
239243
} on HtHttpException {
240244
// Propagate known exceptions from the limit service (e.g., ForbiddenException)
241245
rethrow;
242-
} catch (e) {
246+
} catch (e, s) {
243247
// Catch unexpected errors from the limit service
244-
print(
248+
_logger.severe(
245249
'Unexpected error during limit check for '
246-
'UserContentPreferences PUT: $e',
250+
'UserContentPreferences PUT',
251+
e,
252+
s,
247253
);
248254
throw const OperationFailedException(
249255
'An unexpected error occurred during limit check.',
@@ -358,7 +364,7 @@ Future<Response> _handlePut(
358364
!permissionService.isAdmin(authenticatedUser)) {
359365
// Ensure getOwnerId is provided for models requiring ownership check
360366
if (modelConfig.getOwnerId == null) {
361-
print(
367+
_logger.severe(
362368
'Configuration Error: Model "$modelName" requires '
363369
'ownership check for PUT but getOwnerId is not provided.',
364370
);
@@ -374,7 +380,7 @@ Future<Response> _handlePut(
374380
if (itemOwnerId != authenticatedUser.id) {
375381
// This scenario should ideally not happen if the repository correctly
376382
// enforced ownership during the update call when userId was passed.
377-
print(
383+
_logger.warning(
378384
'Ownership check failed AFTER PUT for item $id. '
379385
'Item owner: $itemOwnerId, User: ${authenticatedUser.id}',
380386
);
@@ -424,7 +430,7 @@ Future<Response> _handleDelete(
424430
!permissionService.isAdmin(authenticatedUser)) {
425431
// Ensure getOwnerId is provided for models requiring ownership check
426432
if (modelConfig.getOwnerId == null) {
427-
print(
433+
_logger.severe(
428434
'Configuration Error: Model "$modelName" requires '
429435
'ownership check for DELETE but getOwnerId is not provided.',
430436
);
@@ -461,15 +467,15 @@ Future<Response> _handleDelete(
461467
final repo = context.read<HtDataRepository<RemoteConfig>>();
462468
itemToDelete = await repo.read(
463469
id: id,
464-
userId: userIdForRepoCall,
465-
); // userId should be null for AppConfig
466-
default:
467-
print(
468-
'Error: Unsupported model type "$modelName" reached _handleDelete ownership check.',
469-
);
470-
// Throw an exception to be caught by the errorHandler
471-
throw OperationFailedException(
472-
'Unsupported model type "$modelName" reached handler.',
470+
userId: userIdForRepoCall,
471+
); // userId should be null for AppConfig
472+
default:
473+
_logger.severe(
474+
'Unsupported model type "$modelName" reached _handleDelete ownership check.',
475+
);
476+
// Throw an exception to be caught by the errorHandler
477+
throw OperationFailedException(
478+
'Unsupported model type "$modelName" reached handler.',
473479
);
474480
}
475481

@@ -534,8 +540,8 @@ Future<Response> _handleDelete(
534540
default:
535541
// This case should ideally be caught by the data/_middleware.dart,
536542
// but added for safety.
537-
print(
538-
'Error: Unsupported model type "$modelName" reached _handleDelete.',
543+
_logger.severe(
544+
'Unsupported model type "$modelName" reached _handleDelete.',
539545
);
540546
// Throw an exception to be caught by the errorHandler
541547
throw OperationFailedException(

0 commit comments

Comments
 (0)