Skip to content

Commit 9b158d7

Browse files
refactor
1 parent 77d748f commit 9b158d7

File tree

5 files changed

+55
-60
lines changed

5 files changed

+55
-60
lines changed

lib/Business_Logic/Exceptions/exception_handlers.dart

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
import 'package:flutter/material.dart';
2-
3-
import '../../Presentation/Components/snack_bar.dart';
4-
5-
enum ExceptionTypes {
6-
socketException,
7-
httpException,
8-
formatException,
9-
timeoutException
10-
}
1+
import 'dart:async';
2+
import 'dart:io';
113

124
class ExceptionHandlers {
13-
String typeOfException(ExceptionTypes exceptionTypes) {
14-
switch (exceptionTypes) {
15-
case ExceptionTypes.socketException:
16-
return 'No internet connection.';
17-
case ExceptionTypes.httpException:
18-
return 'HTTP error occured.';
19-
case ExceptionTypes.formatException:
20-
return 'Invalid data format.';
21-
case ExceptionTypes.timeoutException:
22-
return 'Request timeout.';
5+
getExceptionString(error) {
6+
if (error is SocketException) {
7+
return 'No internet connection.';
8+
} else if (error is HttpException) {
9+
return 'HTTP error occured.';
10+
} else if (error is FormatException) {
11+
return 'Invalid data format.';
12+
} else if (error is TimeoutException) {
13+
return 'Request timedout.';
14+
} else {
15+
return 'Unknown error occured.';
2316
}
2417
}
2518
}
@@ -56,4 +49,3 @@ class NotFoundException extends AppException {
5649
NotFoundException([String? message, String? url])
5750
: super(message, 'Page not found', url);
5851
}
59-

lib/Data/API/base_client.dart

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'dart:async';
2+
23
import 'dart:convert';
3-
import 'dart:io';
4+
import 'package:http/http.dart' as http;
45

56
import '../../Business_Logic/Exceptions/exception_handlers.dart';
6-
import 'package:http/http.dart' as http;
77

88
class BaseClient {
99
static const int timeOutDuration = 35;
@@ -15,14 +15,8 @@ class BaseClient {
1515
var response =
1616
await http.get(uri).timeout(const Duration(seconds: timeOutDuration));
1717
return _processResponse(response);
18-
} on SocketException {
19-
throw const SocketException('Socket exception');
20-
} on TimeoutException {
21-
throw ApiNotRespondingException('Server not responding.');
22-
} on FormatException {
23-
} on HttpException {
2418
} catch (e) {
25-
throw e.toString();
19+
throw ExceptionHandlers().getExceptionString(e);
2620
}
2721
}
2822

@@ -36,11 +30,9 @@ class BaseClient {
3630
.timeout(const Duration(seconds: timeOutDuration));
3731

3832
return _processResponse(response);
39-
} on SocketException {
40-
} on TimeoutException {
41-
throw ApiNotRespondingException('Server not responding.');
42-
} on FormatException {
43-
} on HttpException {}
33+
} catch (e) {
34+
throw ExceptionHandlers().getExceptionString(e);
35+
}
4436
}
4537

4638
//DELETE
@@ -54,13 +46,13 @@ class BaseClient {
5446
var responseJson = response.body;
5547
return responseJson;
5648
case 400: //Bad request
57-
throw BadRequestException(response.body);
49+
throw BadRequestException(response.body).message.toString();
5850
case 401: //Unauthorized
59-
throw UnAuthorizedException(response.body);
51+
throw UnAuthorizedException(response.body).message.toString();
6052
case 403: //Forbidden
61-
throw UnAuthorizedException(response.body);
53+
throw UnAuthorizedException(response.body).message.toString();
6254
case 404: //Resource Not Found
63-
throw NotFoundException(response.body);
55+
throw NotFoundException(response.body).message.toString();
6456
case 500: //Internal Server Error
6557
default:
6658
throw FetchDataException(

lib/Data/Repository/home_page_repo.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ abstract class DemoRepository {
1010
class DemoRepo extends DemoRepository {
1111
@override
1212
Future<DemoModel> fetchData() async {
13-
try{
14-
final response =
13+
final response =
1514
await BaseClient().get('${(dotenv.env['API_BASE_URL'])}/todos/1');
1615
return DemoModel.fromJson(response);
17-
}
18-
catch(e){
19-
throw e.toString();
20-
}
2116
}
2217
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:flutter/material.dart';
2+
3+
class FutureBuilderWidget extends StatelessWidget {
4+
const FutureBuilderWidget({Key? key, required this.futureCall})
5+
: super(key: key);
6+
7+
final Future futureCall;
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
return FutureBuilder(
12+
future: futureCall,
13+
builder: (BuildContext context, snapshot) {
14+
if (snapshot.hasData) {
15+
return Text(snapshot.data.toString());
16+
} else if (snapshot.hasError) {
17+
return Text(snapshot.error.toString());
18+
} else if (snapshot.connectionState == ConnectionState.waiting) {
19+
return const Text('Waiting....');
20+
} else if (snapshot.connectionState == ConnectionState.none) {
21+
return const Text('cannot establish connection with the server.');
22+
}
23+
return const Center(child: CircularProgressIndicator());
24+
},
25+
);
26+
}
27+
}
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import 'package:api_exceptions_part2/Data/Repository/home_page_repo.dart';
1+
import '../../../../Data/Repository/home_page_repo.dart';
2+
import '../../../Components/future_builder.dart';
23
import 'package:flutter/material.dart';
34

45
class MyHomePage extends StatefulWidget {
@@ -18,20 +19,8 @@ class _MyHomePageState extends State<MyHomePage> {
1819
title: Text(widget.title),
1920
),
2021
body: Center(
21-
child: ElevatedButton(
22-
onPressed: () => getData(),
23-
child: const Text('Fetch Data'),
24-
)),
22+
child: FutureBuilderWidget(futureCall: DemoRepo().fetchData()),
23+
),
2524
);
2625
}
27-
28-
Future<void> getData() async {
29-
print('getting data');
30-
try {
31-
var response = await DemoRepo().fetchData();
32-
print(response);
33-
} catch (e) {
34-
print(e.toString());
35-
}
36-
}
3726
}

0 commit comments

Comments
 (0)