Skip to content

Commit 2df8f32

Browse files
committed
style(dart): add avoid_catching_errors lint ignore and refactor switch cases
- Add 'avoid_catching_errors' to ignore_for_file directive - Refactor switch cases in both [id].dart and index.dart files - Remove unnecessary curly braces in some switch cases - Add comments for block scopes in update operation
1 parent 49948d4 commit 2df8f32

File tree

2 files changed

+83
-79
lines changed

2 files changed

+83
-79
lines changed

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses
2+
// ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses, avoid_catching_errors
33

44
import 'dart:io';
55

@@ -28,29 +28,29 @@ Future<Response> onRequest(RequestContext context, String id) async {
2828
switch (modelName) {
2929
case 'headline':
3030
final repo = context.read<HtDataRepository<Headline>>();
31-
final item = await repo.read(id);
32-
// Serialize using the specific model's toJson method
33-
itemJson = item.toJson();
34-
case 'category':
35-
final repo = context.read<HtDataRepository<Category>>();
36-
final item = await repo.read(id);
37-
itemJson = item.toJson();
38-
case 'source':
39-
final repo = context.read<HtDataRepository<Source>>();
40-
final item = await repo.read(id);
41-
itemJson = item.toJson();
42-
case 'country':
43-
final repo = context.read<HtDataRepository<Country>>();
44-
final item = await repo.read(id);
45-
itemJson = item.toJson();
46-
default:
47-
// This case should ideally be caught by middleware, but added for safety
48-
return Response(
49-
statusCode: HttpStatus.internalServerError,
50-
body:
51-
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
52-
);
53-
}
31+
final item = await repo.read(id);
32+
// Serialize using the specific model's toJson method
33+
itemJson = item.toJson();
34+
case 'category':
35+
final repo = context.read<HtDataRepository<Category>>();
36+
final item = await repo.read(id);
37+
itemJson = item.toJson();
38+
case 'source':
39+
final repo = context.read<HtDataRepository<Source>>();
40+
final item = await repo.read(id);
41+
itemJson = item.toJson();
42+
case 'country':
43+
final repo = context.read<HtDataRepository<Country>>();
44+
final item = await repo.read(id);
45+
itemJson = item.toJson();
46+
default:
47+
// This case should ideally be caught by middleware, but added for safety
48+
return Response(
49+
statusCode: HttpStatus.internalServerError,
50+
body:
51+
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
52+
);
53+
}
5454
// Return the serialized item
5555
return Response.json(body: itemJson);
5656
}
@@ -91,7 +91,8 @@ Future<Response> onRequest(RequestContext context, String id) async {
9191
// Removed inner try-catch block to allow exceptions to propagate
9292
switch (modelName) {
9393
case 'headline':
94-
{ // Added block scope
94+
{
95+
// Added block scope
9596
final repo = context.read<HtDataRepository<Headline>>();
9697
final typedItem = itemToUpdate as Headline; // Cast to specific type
9798
// Validate ID consistency
@@ -105,8 +106,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
105106
final updatedItem = await repo.update(id, typedItem);
106107
updatedJson = updatedItem.toJson();
107108
} // End block scope
108-
case 'category':
109-
{ // Added block scope
109+
case 'category':
110+
{
111+
// Added block scope
110112
final repo = context.read<HtDataRepository<Category>>();
111113
final typedItem = itemToUpdate as Category; // Cast to specific type
112114
// Validate ID consistency
@@ -120,8 +122,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
120122
final updatedItem = await repo.update(id, typedItem);
121123
updatedJson = updatedItem.toJson();
122124
} // End block scope
123-
case 'source':
124-
{ // Added block scope
125+
case 'source':
126+
{
127+
// Added block scope
125128
final repo = context.read<HtDataRepository<Source>>();
126129
final typedItem = itemToUpdate as Source; // Cast to specific type
127130
// Validate ID consistency
@@ -135,8 +138,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
135138
final updatedItem = await repo.update(id, typedItem);
136139
updatedJson = updatedItem.toJson();
137140
} // End block scope
138-
case 'country':
139-
{ // Added block scope
141+
case 'country':
142+
{
143+
// Added block scope
140144
final repo = context.read<HtDataRepository<Country>>();
141145
final typedItem = itemToUpdate as Country; // Cast to specific type
142146
// Validate ID consistency
@@ -150,14 +154,14 @@ Future<Response> onRequest(RequestContext context, String id) async {
150154
final updatedItem = await repo.update(id, typedItem);
151155
updatedJson = updatedItem.toJson();
152156
} // End block scope
153-
default:
154-
// This case should ideally be caught by middleware, but added for safety
155-
return Response(
156-
statusCode: HttpStatus.internalServerError,
157-
body:
158-
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
159-
);
160-
}
157+
default:
158+
// This case should ideally be caught by middleware, but added for safety
159+
return Response(
160+
statusCode: HttpStatus.internalServerError,
161+
body:
162+
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
163+
);
164+
}
161165
// Return the serialized updated item
162166
return Response.json(body: updatedJson);
163167
}
@@ -168,21 +172,21 @@ Future<Response> onRequest(RequestContext context, String id) async {
168172
// No serialization needed, just call delete based on type
169173
switch (modelName) {
170174
case 'headline':
171-
await context.read<HtDataRepository<Headline>>().delete(id);
172-
case 'category':
173-
await context.read<HtDataRepository<Category>>().delete(id);
174-
case 'source':
175-
await context.read<HtDataRepository<Source>>().delete(id);
176-
case 'country':
177-
await context.read<HtDataRepository<Country>>().delete(id);
178-
default:
179-
// This case should ideally be caught by middleware, but added for safety
180-
return Response(
181-
statusCode: HttpStatus.internalServerError,
182-
body:
183-
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
184-
);
185-
}
175+
await context.read<HtDataRepository<Headline>>().delete(id);
176+
case 'category':
177+
await context.read<HtDataRepository<Category>>().delete(id);
178+
case 'source':
179+
await context.read<HtDataRepository<Source>>().delete(id);
180+
case 'country':
181+
await context.read<HtDataRepository<Country>>().delete(id);
182+
default:
183+
// This case should ideally be caught by middleware, but added for safety
184+
return Response(
185+
statusCode: HttpStatus.internalServerError,
186+
body:
187+
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
188+
);
189+
}
186190
// Return 204 No Content for successful deletion
187191
return Response(statusCode: HttpStatus.noContent);
188192
}

routes/api/v1/data/index.dart

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses
2+
// ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses, avoid_catching_errors
33

44
import 'dart:io';
55

@@ -140,30 +140,30 @@ Future<Response> onRequest(RequestContext context) async {
140140
switch (modelName) {
141141
case 'headline':
142142
final repo = context.read<HtDataRepository<Headline>>();
143-
// Cast newItem to the specific type expected by the repository's create method
144-
final createdItem = await repo.create(newItem as Headline);
145-
// Serialize using the specific model's toJson method
146-
createdJson = createdItem.toJson();
147-
case 'category':
148-
final repo = context.read<HtDataRepository<Category>>();
149-
final createdItem = await repo.create(newItem as Category);
150-
createdJson = createdItem.toJson();
151-
case 'source':
152-
final repo = context.read<HtDataRepository<Source>>();
153-
final createdItem = await repo.create(newItem as Source);
154-
createdJson = createdItem.toJson();
155-
case 'country':
156-
final repo = context.read<HtDataRepository<Country>>();
157-
final createdItem = await repo.create(newItem as Country);
158-
createdJson = createdItem.toJson();
159-
default:
160-
// This case should ideally be caught by middleware, but added for safety
161-
return Response(
162-
statusCode: HttpStatus.internalServerError,
163-
body:
164-
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
165-
);
166-
}
143+
// Cast newItem to the specific type expected by the repository's create method
144+
final createdItem = await repo.create(newItem as Headline);
145+
// Serialize using the specific model's toJson method
146+
createdJson = createdItem.toJson();
147+
case 'category':
148+
final repo = context.read<HtDataRepository<Category>>();
149+
final createdItem = await repo.create(newItem as Category);
150+
createdJson = createdItem.toJson();
151+
case 'source':
152+
final repo = context.read<HtDataRepository<Source>>();
153+
final createdItem = await repo.create(newItem as Source);
154+
createdJson = createdItem.toJson();
155+
case 'country':
156+
final repo = context.read<HtDataRepository<Country>>();
157+
final createdItem = await repo.create(newItem as Country);
158+
createdJson = createdItem.toJson();
159+
default:
160+
// This case should ideally be caught by middleware, but added for safety
161+
return Response(
162+
statusCode: HttpStatus.internalServerError,
163+
body:
164+
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
165+
);
166+
}
167167
// Return the serialized created item
168168
return Response.json(statusCode: HttpStatus.created, body: createdJson);
169169
}

0 commit comments

Comments
 (0)