Skip to content

Commit 785f406

Browse files
committed
refactor(error handling): centralize error handling in middleware
- Remove specific error handling in individual routes - Rely on centralized errorHandler middleware for consistent error handling - Update comments to reflect new error handling approach - Improve code maintainability and reduce duplication
1 parent 2df8f32 commit 785f406

File tree

3 files changed

+8
-44
lines changed

3 files changed

+8
-44
lines changed

routes/_middleware.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,5 @@ Handler middleware(Handler handler) {
129129

130130
// Add other essential middleware like error handling
131131
.use(requestLogger()) // Basic request logging
132-
.use(errorHandler()); // Removed as the file was deleted
132+
.use(errorHandler()); // Centralized error handling
133133
}

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,30 +194,11 @@ 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 NotFoundException catch (e) {
198-
// Handle specific case where the item ID is not found
199-
// This should be caught by the central error handler, but added as fallback
200-
return Response(statusCode: HttpStatus.notFound, body: e.message);
201-
} on HtHttpException catch (e) {
202-
// Handle other known HTTP exceptions
203-
// These should ideally be caught by the central error handler middleware
204-
if (e is BadRequestException) {
205-
return Response(statusCode: HttpStatus.badRequest, body: e.message);
206-
}
207-
print('HtHttpException occurred in /data/[id].dart: $e');
208-
return Response(
209-
statusCode: HttpStatus.internalServerError,
210-
body: 'API Error: ${e.message}',
211-
);
212-
} on FormatException catch (e) {
213-
// Handle potential JSON parsing/serialization errors during PUT
214-
print('FormatException occurred in /data/[id].dart: $e');
215-
return Response(
216-
statusCode: HttpStatus.badRequest,
217-
body: 'Invalid data format: ${e.message}',
218-
);
219197
} catch (e, stackTrace) {
220-
// Catch any other unexpected errors
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.
221202
print(
222203
'Unexpected error in /data/[id].dart handler: $e\n$stackTrace',
223204
);

routes/api/v1/data/index.dart

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,10 @@ 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 (e) {
175-
// Handle known HTTP exceptions from the repository/client layer
176-
// These should ideally be caught by the central error handler middleware,
177-
// but handling here provides a fallback.
178-
if (e is BadRequestException) {
179-
return Response(statusCode: HttpStatus.badRequest, body: e.message);
180-
}
181-
print('HtHttpException occurred in /data/index.dart: $e'); // Log the error
182-
return Response(
183-
statusCode: HttpStatus.internalServerError,
184-
body: 'API Error: ${e.message}',
185-
);
186-
} on FormatException catch (e) {
187-
// Handle potential JSON parsing/serialization errors during POST
188-
print('FormatException occurred in /data/index.dart: $e'); // Log the error
189-
return Response(
190-
statusCode: HttpStatus.badRequest,
191-
body: 'Invalid data format: ${e.message}',
192-
);
193174
} catch (e, stackTrace) {
194-
// Catch any other unexpected errors
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.
195178
print(
196179
'Unexpected error in /data/index.dart handler: $e\n$stackTrace',
197180
); // Log the error and stack trace

0 commit comments

Comments
 (0)