Skip to content

Commit 5ae636f

Browse files
committed
refactor(data): rethrow exceptions for centralized error handling
- Rethrow HtHttpException and FormatException in data/[id].dart and data/index.dart - Remove specific exception handling from central error handler - Update error logging and response for unexpected errors
1 parent 785f406 commit 5ae636f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,14 @@ Future<Response> onRequest(RequestContext context, String id) async {
194194
// --- Other Methods ---
195195
// Methods not allowed on the item endpoint
196196
return Response(statusCode: HttpStatus.methodNotAllowed);
197+
} on HtHttpException catch (_) {
198+
// Let the errorHandler middleware handle HtHttpExceptions (incl. NotFound)
199+
rethrow;
200+
} on FormatException catch (_) {
201+
// Let the errorHandler middleware handle FormatExceptions (e.g., from PUT body)
202+
rethrow;
197203
} catch (e, stackTrace) {
198-
// Catch any other unexpected errors (e.g., provider resolution, etc.)
199-
// Specific HtHttpException (including NotFoundException), FormatException,
200-
// and TypeError should be caught by the central errorHandler middleware
201-
// or handled specifically within the PUT/POST logic for request body validation.
204+
// Handle any other unexpected errors locally (e.g., provider resolution)
202205
print(
203206
'Unexpected error in /data/[id].dart handler: $e\n$stackTrace',
204207
);

routes/api/v1/data/index.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,17 @@ Future<Response> onRequest(RequestContext context) async {
171171
// --- Other Methods ---
172172
// Methods not allowed on the collection endpoint
173173
return Response(statusCode: HttpStatus.methodNotAllowed);
174+
} on HtHttpException catch (_) {
175+
// Let the errorHandler middleware handle HtHttpExceptions
176+
rethrow;
177+
} on FormatException catch (_) {
178+
// Let the errorHandler middleware handle FormatExceptions
179+
rethrow;
174180
} catch (e, stackTrace) {
175-
// Catch any other unexpected errors (e.g., provider resolution, etc.)
176-
// Specific HtHttpException and FormatException should be caught by
177-
// the central errorHandler middleware.
181+
// Handle any other unexpected errors locally (e.g., provider resolution)
178182
print(
179183
'Unexpected error in /data/index.dart handler: $e\n$stackTrace',
180-
); // Log the error and stack trace
184+
);
181185
return Response(
182186
statusCode: HttpStatus.internalServerError,
183187
body: 'Internal Server Error.',

0 commit comments

Comments
 (0)