@@ -7,6 +7,9 @@ import 'package:dart_frog/dart_frog.dart';
7
7
import 'package:ht_api/src/config/environment_config.dart' ;
8
8
import 'package:ht_shared/ht_shared.dart' ;
9
9
import 'package:json_annotation/json_annotation.dart' ;
10
+ import 'package:logging/logging.dart' ;
11
+
12
+ final _log = Logger ('ErrorHandler' );
10
13
11
14
/// Middleware that catches errors and converts them into
12
15
/// standardized JSON responses.
@@ -20,7 +23,7 @@ Middleware errorHandler() {
20
23
} on HtHttpException catch (e, stackTrace) {
21
24
// Handle specific HtHttpExceptions from the client/repository layers
22
25
final statusCode = _mapExceptionToStatusCode (e);
23
- print ('HtHttpException Caught: $ e \n $ stackTrace ' ); // Log for debugging
26
+ _log. warning ('HtHttpException Caught' , e, stackTrace);
24
27
return _jsonErrorResponse (
25
28
statusCode: statusCode,
26
29
exception: e,
@@ -32,23 +35,23 @@ Middleware errorHandler() {
32
35
final message =
33
36
'Invalid request body: Field "$field " has an '
34
37
'invalid value or is missing. ${e .message }' ;
35
- print ('CheckedFromJsonException Caught: $ e \n $ stackTrace ' );
38
+ _log. warning ('CheckedFromJsonException Caught' , e, stackTrace);
36
39
return _jsonErrorResponse (
37
40
statusCode: HttpStatus .badRequest, // 400
38
41
exception: InvalidInputException (message),
39
42
context: context,
40
43
);
41
44
} on FormatException catch (e, stackTrace) {
42
45
// 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);
44
47
return _jsonErrorResponse (
45
48
statusCode: HttpStatus .badRequest, // 400
46
49
exception: InvalidInputException ('Invalid data format: ${e .message }' ),
47
50
context: context,
48
51
);
49
52
} catch (e, stackTrace) {
50
53
// Handle any other unexpected errors
51
- print ('Unhandled Exception Caught: $ e \n $ stackTrace ' );
54
+ _log. severe ('Unhandled Exception Caught' , e, stackTrace);
52
55
return _jsonErrorResponse (
53
56
statusCode: HttpStatus .internalServerError, // 500
54
57
exception: const UnknownException (
0 commit comments