|
3 | 3 |
|
4 | 4 | import 'dart:io';
|
5 | 5 |
|
| 6 | +// --- Error Handling Strategy --- |
| 7 | +// Route-specific handlers (_handleGet, _handlePut, _handleDelete, etc.) should |
| 8 | +// generally allow HtHttpExceptions (like NotFoundException, BadRequestException) |
| 9 | +// and FormatExceptions thrown by lower layers (Repositories, Clients, JSON parsing) |
| 10 | +// to propagate upwards. |
| 11 | +// |
| 12 | +// These specific exceptions are caught and re-thrown by the main `onRequest` |
| 13 | +// handler in this file. |
| 14 | +// |
| 15 | +// The centralized `errorHandler` middleware (defined in lib/src/middlewares/) |
| 16 | +// is responsible for catching these re-thrown exceptions and mapping them to |
| 17 | +// appropriate, standardized JSON error responses (e.g., 400, 404, 500). |
| 18 | +// |
| 19 | +// Local try-catch blocks within specific _handle* methods should be reserved |
| 20 | +// for handling errors that require immediate, localized responses (like the |
| 21 | +// TypeError during deserialization in _handlePut) or for logging specific |
| 22 | +// context before allowing propagation. |
| 23 | + |
6 | 24 | import 'package:dart_frog/dart_frog.dart';
|
7 | 25 | // Import RequestId from the middleware file where it's defined
|
8 | 26 | import 'package:ht_api/src/registry/model_registry.dart';
|
@@ -275,40 +293,33 @@ Future<Response> _handleDelete(
|
275 | 293 | RequestContext context,
|
276 | 294 | String id,
|
277 | 295 | String modelName,
|
278 |
| - String |
279 |
| - requestId, // Receive requestId for logging, though not used in response |
| 296 | + String requestId, // Receive requestId for logging |
280 | 297 | ) async {
|
281 |
| - // Repository exceptions (like NotFoundException) will propagate up. |
282 |
| - try { |
283 |
| - switch (modelName) { |
284 |
| - case 'headline': |
285 |
| - await context.read<HtDataRepository<Headline>>().delete(id); |
286 |
| - case 'category': |
287 |
| - await context.read<HtDataRepository<Category>>().delete(id); |
288 |
| - case 'source': |
289 |
| - await context.read<HtDataRepository<Source>>().delete(id); |
290 |
| - case 'country': |
291 |
| - await context.read<HtDataRepository<Country>>().delete(id); |
292 |
| - default: |
293 |
| - // This case should ideally be caught by middleware, but added for safety |
294 |
| - return Response( |
295 |
| - statusCode: HttpStatus.internalServerError, |
296 |
| - body: |
297 |
| - 'Internal Server Error: Unsupported model type "$modelName" reached handler.', |
298 |
| - ); |
299 |
| - } |
300 |
| - } catch (e) { |
301 |
| - // Catch potential provider errors during context.read within this handler |
302 |
| - // Include requestId in the server log |
303 |
| - print( |
304 |
| - '[ReqID: $requestId] Error reading repository provider for model "$modelName" in _handleDelete [id]: $e', |
305 |
| - ); |
306 |
| - return Response( |
307 |
| - statusCode: HttpStatus.internalServerError, |
308 |
| - body: |
309 |
| - 'Internal Server Error: Could not resolve repository for model "$modelName".', |
310 |
| - ); |
| 298 | + // Allow repository exceptions (e.g., NotFoundException) to propagate |
| 299 | + // upwards to be handled by the standard error handling mechanism. |
| 300 | + // (Removed the overly broad try-catch block that was previously here). |
| 301 | + switch (modelName) { |
| 302 | + case 'headline': |
| 303 | + await context.read<HtDataRepository<Headline>>().delete(id); |
| 304 | + case 'category': |
| 305 | + await context.read<HtDataRepository<Category>>().delete(id); |
| 306 | + case 'source': |
| 307 | + await context.read<HtDataRepository<Source>>().delete(id); |
| 308 | + case 'country': |
| 309 | + await context.read<HtDataRepository<Country>>().delete(id); |
| 310 | + default: |
| 311 | + // This case should ideally be caught by the data/_middleware.dart, |
| 312 | + // but added for safety. Consider logging this unexpected state. |
| 313 | + print( |
| 314 | + '[ReqID: $requestId] Error: Unsupported model type "$modelName" reached _handleDelete.', |
| 315 | + ); |
| 316 | + return Response( |
| 317 | + statusCode: HttpStatus.internalServerError, |
| 318 | + body: |
| 319 | + 'Internal Server Error: Unsupported model type "$modelName" reached handler.', |
| 320 | + ); |
311 | 321 | }
|
| 322 | + |
312 | 323 | // Return 204 No Content for successful deletion (no body, no metadata)
|
313 | 324 | return Response(statusCode: HttpStatus.noContent);
|
314 | 325 | }
|
0 commit comments