Skip to content

Commit 7349220

Browse files
committed
fix(error_handler): use logger instead of print
- Replaced print statements with logger - Improved error logging with stack traces - Added logging package dependency - Used different log levels for severity - Improved error handling clarity
1 parent 76589a3 commit 7349220

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/src/middlewares/error_handler.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import 'package:dart_frog/dart_frog.dart';
77
import 'package:ht_api/src/config/environment_config.dart';
88
import 'package:ht_shared/ht_shared.dart';
99
import 'package:json_annotation/json_annotation.dart';
10+
import 'package:logging/logging.dart';
11+
12+
final _log = Logger('ErrorHandler');
1013

1114
/// Middleware that catches errors and converts them into
1215
/// standardized JSON responses.
@@ -20,7 +23,7 @@ Middleware errorHandler() {
2023
} on HtHttpException catch (e, stackTrace) {
2124
// Handle specific HtHttpExceptions from the client/repository layers
2225
final statusCode = _mapExceptionToStatusCode(e);
23-
print('HtHttpException Caught: $e\n$stackTrace'); // Log for debugging
26+
_log.warning('HtHttpException Caught', e, stackTrace);
2427
return _jsonErrorResponse(
2528
statusCode: statusCode,
2629
exception: e,
@@ -32,23 +35,23 @@ Middleware errorHandler() {
3235
final message =
3336
'Invalid request body: Field "$field" has an '
3437
'invalid value or is missing. ${e.message}';
35-
print('CheckedFromJsonException Caught: $e\n$stackTrace');
38+
_log.warning('CheckedFromJsonException Caught', e, stackTrace);
3639
return _jsonErrorResponse(
3740
statusCode: HttpStatus.badRequest, // 400
3841
exception: InvalidInputException(message),
3942
context: context,
4043
);
4144
} on FormatException catch (e, stackTrace) {
4245
// Handle data format/parsing errors (often indicates bad client input)
43-
print('FormatException Caught: $e\n$stackTrace'); // Log for debugging
46+
_log.warning('FormatException Caught', e, stackTrace);
4447
return _jsonErrorResponse(
4548
statusCode: HttpStatus.badRequest, // 400
4649
exception: InvalidInputException('Invalid data format: ${e.message}'),
4750
context: context,
4851
);
4952
} catch (e, stackTrace) {
5053
// Handle any other unexpected errors
51-
print('Unhandled Exception Caught: $e\n$stackTrace');
54+
_log.severe('Unhandled Exception Caught', e, stackTrace);
5255
return _jsonErrorResponse(
5356
statusCode: HttpStatus.internalServerError, // 500
5457
exception: const UnknownException(

0 commit comments

Comments
 (0)