1- //
2- // ignore_for_file: lines_longer_than_80_chars, no_default_cases, avoid_catches_without_on_clauses, avoid_catching_errors
3-
41import 'dart:io' ;
52
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-
243import 'package:dart_frog/dart_frog.dart' ;
25- // Import RequestId from the middleware file where it's defined
264import 'package:ht_api/src/registry/model_registry.dart' ;
275import 'package:ht_data_repository/ht_data_repository.dart' ;
28- // Import exceptions
29- // Import models, SuccessApiResponse, ResponseMetadata
306import 'package:ht_shared/ht_shared.dart' ;
317
328import '../../../_middleware.dart' ;
@@ -48,7 +24,7 @@ Future<Response> onRequest(RequestContext context, String id) async {
4824 context,
4925 id,
5026 modelName,
51- modelConfig, // Pass modelConfig
27+ modelConfig,
5228 authenticatedUser,
5329 requestId,
5430 );
@@ -66,11 +42,10 @@ Future<Response> onRequest(RequestContext context, String id) async {
6642 context,
6743 id,
6844 modelName,
69- modelConfig, // Pass modelConfig
45+ modelConfig,
7046 authenticatedUser,
7147 requestId,
7248 );
73- // Add cases for other methods if needed in the future
7449 default :
7550 // Methods not allowed on the item endpoint
7651 return Response (statusCode: HttpStatus .methodNotAllowed);
@@ -101,11 +76,11 @@ Future<Response> _handleGet(
10176 RequestContext context,
10277 String id,
10378 String modelName,
104- ModelConfig <dynamic > modelConfig, // Receive modelConfig
105- User authenticatedUser, // Receive authenticatedUser
79+ ModelConfig <dynamic > modelConfig,
80+ User authenticatedUser,
10681 String requestId,
10782) async {
108- dynamic item; // Use dynamic
83+ dynamic item;
10984
11085 String ? userIdForRepoCall;
11186 if (modelConfig.ownership == ModelOwnership .userOwned) {
@@ -179,7 +154,7 @@ Future<Response> _handlePut(
179154 String id,
180155 String modelName,
181156 ModelConfig <dynamic > modelConfig,
182- User authenticatedUser, // Receive authenticatedUser
157+ User authenticatedUser,
183158 String requestId,
184159) async {
185160 final requestBody = await context.request.json () as Map <String , dynamic >? ;
@@ -191,7 +166,7 @@ Future<Response> _handlePut(
191166 }
192167
193168 // Deserialize using ModelConfig's fromJson, catching TypeErrors locally
194- dynamic itemToUpdate; // Use dynamic initially
169+ dynamic itemToUpdate;
195170 try {
196171 itemToUpdate = modelConfig.fromJson (requestBody);
197172 } on TypeError catch (e) {
@@ -213,14 +188,14 @@ Future<Response> _handlePut(
213188 );
214189 }
215190
216- dynamic updatedItem; // Use dynamic
191+ dynamic updatedItem;
217192
218193 String ? userIdForRepoCall;
219194 if (modelConfig.ownership == ModelOwnership .userOwned) {
220195 userIdForRepoCall = authenticatedUser.id;
221196 } else {
222- // For global models, update might imply admin rights.
223- // For now, pass null, assuming repo handles global updates or has other checks .
197+ // TODO(fulleni): For global models, update might imply admin rights.
198+ // For now, pass null, consider adding an admin user check .
224199 userIdForRepoCall = null ;
225200 }
226201
@@ -326,7 +301,7 @@ Future<Response> _handlePut(
326301 // Wrap the updated item in SuccessApiResponse with metadata
327302 final successResponse = SuccessApiResponse <dynamic >(
328303 data: updatedItem,
329- metadata: metadata, // Include the created metadata
304+ metadata: metadata,
330305 );
331306
332307 // Provide the correct toJsonT for the specific model type
@@ -344,16 +319,16 @@ Future<Response> _handleDelete(
344319 RequestContext context,
345320 String id,
346321 String modelName,
347- ModelConfig <dynamic > modelConfig, // Receive modelConfig
348- User authenticatedUser, // Receive authenticatedUser
322+ ModelConfig <dynamic > modelConfig,
323+ User authenticatedUser,
349324 String requestId,
350325) async {
351326 String ? userIdForRepoCall;
352327 if (modelConfig.ownership == ModelOwnership .userOwned) {
353328 userIdForRepoCall = authenticatedUser.id;
354329 } else {
355- // For global models, delete might imply admin rights.
356- // For now, pass null.
330+ // TODO(fulleni): For global models, update might imply admin rights.
331+ // For now, pass null, consider adding an admin user check .
357332 userIdForRepoCall = null ;
358333 }
359334
0 commit comments