@@ -8,6 +8,10 @@ import 'package:ht_api/src/services/dashboard_summary_service.dart';
8
8
import 'package:ht_api/src/services/user_preference_limit_service.dart' ; // Import UserPreferenceLimitService
9
9
import 'package:ht_data_repository/ht_data_repository.dart' ;
10
10
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' );
11
15
12
16
/// Handles requests for the /api/v1/data/[id] endpoint.
13
17
/// Dispatches requests to specific handlers based on the HTTP method.
@@ -136,7 +140,7 @@ Future<Response> _handleGet(
136
140
! permissionService.isAdmin (authenticatedUser)) {
137
141
// Ensure getOwnerId is provided for models requiring ownership check
138
142
if (modelConfig.getOwnerId == null ) {
139
- print (
143
+ _logger. severe (
140
144
'Configuration Error: Model "$modelName " requires '
141
145
'ownership check for GET item but getOwnerId is not provided.' ,
142
146
);
@@ -192,9 +196,9 @@ Future<Response> _handlePut(
192
196
dynamic itemToUpdate;
193
197
try {
194
198
itemToUpdate = modelConfig.fromJson (requestBody);
195
- } on TypeError catch (e) {
199
+ } on TypeError catch (e, s ) {
196
200
// 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 );
198
202
// Throw BadRequestException to be caught by the errorHandler
199
203
throw const BadRequestException (
200
204
'Invalid request body: Missing or invalid required field(s).' ,
@@ -214,7 +218,7 @@ Future<Response> _handlePut(
214
218
} catch (e) {
215
219
// Ignore if getId throws, means ID might not be in the body,
216
220
// 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 ' );
218
222
}
219
223
220
224
// --- Handler-Level Limit Check (for UserContentPreferences PUT) ---
@@ -224,7 +228,7 @@ Future<Response> _handlePut(
224
228
try {
225
229
// Ensure the itemToUpdate is the correct type for the limit service
226
230
if (itemToUpdate is ! UserContentPreferences ) {
227
- print (
231
+ _logger. severe (
228
232
'Type Error: Expected UserContentPreferences '
229
233
'for limit check, but got ${itemToUpdate .runtimeType }.' ,
230
234
);
@@ -239,11 +243,13 @@ Future<Response> _handlePut(
239
243
} on HtHttpException {
240
244
// Propagate known exceptions from the limit service (e.g., ForbiddenException)
241
245
rethrow ;
242
- } catch (e) {
246
+ } catch (e, s ) {
243
247
// Catch unexpected errors from the limit service
244
- print (
248
+ _logger. severe (
245
249
'Unexpected error during limit check for '
246
- 'UserContentPreferences PUT: $e ' ,
250
+ 'UserContentPreferences PUT' ,
251
+ e,
252
+ s,
247
253
);
248
254
throw const OperationFailedException (
249
255
'An unexpected error occurred during limit check.' ,
@@ -358,7 +364,7 @@ Future<Response> _handlePut(
358
364
! permissionService.isAdmin (authenticatedUser)) {
359
365
// Ensure getOwnerId is provided for models requiring ownership check
360
366
if (modelConfig.getOwnerId == null ) {
361
- print (
367
+ _logger. severe (
362
368
'Configuration Error: Model "$modelName " requires '
363
369
'ownership check for PUT but getOwnerId is not provided.' ,
364
370
);
@@ -374,7 +380,7 @@ Future<Response> _handlePut(
374
380
if (itemOwnerId != authenticatedUser.id) {
375
381
// This scenario should ideally not happen if the repository correctly
376
382
// enforced ownership during the update call when userId was passed.
377
- print (
383
+ _logger. warning (
378
384
'Ownership check failed AFTER PUT for item $id . '
379
385
'Item owner: $itemOwnerId , User: ${authenticatedUser .id }' ,
380
386
);
@@ -424,7 +430,7 @@ Future<Response> _handleDelete(
424
430
! permissionService.isAdmin (authenticatedUser)) {
425
431
// Ensure getOwnerId is provided for models requiring ownership check
426
432
if (modelConfig.getOwnerId == null ) {
427
- print (
433
+ _logger. severe (
428
434
'Configuration Error: Model "$modelName " requires '
429
435
'ownership check for DELETE but getOwnerId is not provided.' ,
430
436
);
@@ -461,15 +467,15 @@ Future<Response> _handleDelete(
461
467
final repo = context.read <HtDataRepository <RemoteConfig >>();
462
468
itemToDelete = await repo.read (
463
469
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.' ,
473
479
);
474
480
}
475
481
@@ -534,8 +540,8 @@ Future<Response> _handleDelete(
534
540
default :
535
541
// This case should ideally be caught by the data/_middleware.dart,
536
542
// but added for safety.
537
- print (
538
- 'Error: Unsupported model type "$modelName " reached _handleDelete.' ,
543
+ _logger. severe (
544
+ 'Unsupported model type "$modelName " reached _handleDelete.' ,
539
545
);
540
546
// Throw an exception to be caught by the errorHandler
541
547
throw OperationFailedException (
0 commit comments