Skip to content

Commit 056bd91

Browse files
authored
Merge pull request #35 from CodingAleCR/release/0.3.2
Release: 0.3.2
2 parents 08abd0f + 2e0b1b2 commit 056bd91

File tree

7 files changed

+36
-18
lines changed

7 files changed

+36
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.3.2
4+
5+
* Changed: Example now showcases exception handling.
6+
* Changed: README now showcases exception handling.
7+
* Fixed: Interceptor no longer using custom exceptions, instead it rethrows in the case that the retry policy is not set or if it has reached max attempts.
8+
39
## 0.3.1
410

511
* Fixed: Retry Policy's `shouldAttemptRetryOnResponse` was synchronous which would not allow async token updates.

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,27 @@ Here is an example with a repository using the `HttpWithInterceptor` class.
120120
```dart
121121
class WeatherRepository {
122122
123-
124-
Future<Map<String, dynamic>> fetchCityWeather(int id) async {
123+
Future<Map<String, dynamic>> fetchCityWeather(int id) async {
125124
var parsedWeather;
126125
try {
127-
var response = await HttpWithInterceptor.build(
128-
interceptors: [WeatherApiInterceptor()])
129-
.get("$baseUrl/weather", params: {'id': "$id"});
126+
final response =
127+
await client.get("$baseUrl/weather", params: {'id': "$id"});
130128
if (response.statusCode == 200) {
131129
parsedWeather = json.decode(response.body);
132130
} else {
133-
throw Exception("Error while fetching. \n ${response.body}");
131+
return Future.error(
132+
"Error while fetching.",
133+
StackTrace.fromString("${response.body}"),
134+
);
134135
}
135-
} catch (e) {
136-
print(e);
136+
} on SocketException {
137+
return Future.error('No Internet connection 😑');
138+
} on FormatException {
139+
return Future.error('Bad response format 👎');
140+
} on Exception {
141+
return Future.error('Unexpected error 😢');
137142
}
143+
138144
return parsedWeather;
139145
}
140146
@@ -143,7 +149,7 @@ class WeatherRepository {
143149

144150
### Retrying requests
145151

146-
**(NEW 🎉)** Sometimes you need to retry a request due to different circumstances, an expired token is a really good example. Here's how you could potentially implement an expired token retry policy with `http_interceptor`.
152+
**(NEW 🎉)** Sometimes you need to retry a request due to different circumstances, an expired token is a really good example. Here's how you could potentially implement an expired token retry policy with `http_interceptor`.
147153

148154
```dart
149155
class ExpiredTokenRetryPolicy extends RetryPolicy {
@@ -164,7 +170,7 @@ You can also set the maximum amount of retry attempts with `maxRetryAttempts` pr
164170

165171
### Using self signed certificates
166172

167-
**(EXPERIMENTAL ⚗️)** This plugin allows you to override the default `badCertificateCallback` provided by Dart's `io` package, this is really useful when working with self-signed certificates in your server. This can be done by sending a the callback to the HttpInterceptor builder functions. This feature is marked as experimental and **might be subject to change before release 1.0.0 comes**.
173+
**(EXPERIMENTAL ⚗️)** This plugin allows you to override the default `badCertificateCallback` provided by Dart's `io` package, this is really useful when working with self-signed certificates in your server. This can be done by sending a the callback to the HttpInterceptor builder functions. This feature is marked as experimental and **might be subject to change before release 1.0.0 comes**.
168174

169175
```dart
170176
class WeatherRepository {

example/lib/credentials.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Replace this if you are going to run the example. You can get your own at https://openweathermap.org/
2-
const String OPEN_WEATHER_API_KEY = "YOUR-KEY-HERE";
2+
const String OPEN_WEATHER_API_KEY = "YOUR-KEY-HERE";

example/lib/main.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:io';
23

34
import 'package:flutter/material.dart';
45
import 'package:http_interceptor/http_interceptor.dart';
@@ -142,7 +143,7 @@ class WeatherSearch extends SearchDelegate<String> {
142143
child: Text(snapshot.error),
143144
);
144145
}
145-
146+
146147
if (!snapshot.hasData) {
147148
return Center(
148149
child: CircularProgressIndicator(),
@@ -267,9 +268,14 @@ class WeatherRepository {
267268
StackTrace.fromString("${response.body}"),
268269
);
269270
}
270-
} catch (e) {
271-
print(e);
271+
} on SocketException {
272+
return Future.error('No Internet connection 😑');
273+
} on FormatException {
274+
return Future.error('Bad response format 👎');
275+
} on Exception {
276+
return Future.error('Unexpected error 😢');
272277
}
278+
273279
return parsedWeather;
274280
}
275281
}
@@ -280,7 +286,7 @@ class WeatherApiInterceptor implements InterceptorContract {
280286
try {
281287
data.params['appid'] = OPEN_WEATHER_API_KEY;
282288
data.params['units'] = 'metric';
283-
data.headers["Content-Type"] = "application/json";
289+
data.headers[HttpHeaders.contentTypeHeader] = "application/json";
284290
} catch (e) {
285291
print(e);
286292
}

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ packages:
8787
path: ".."
8888
relative: true
8989
source: path
90-
version: "0.3.1"
90+
version: "0.3.2"
9191
http_parser:
9292
dependency: transitive
9393
description:

lib/http_client_with_interceptor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class HttpClientWithInterceptor extends BaseClient {
223223
_retryCount += 1;
224224
return _attemptRequest(request);
225225
} else {
226-
throw HttpInterceptorException(error.toString());
226+
rethrow;
227227
}
228228
}
229229

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: http_interceptor
22
description: A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
3-
version: 0.3.1
3+
version: 0.3.2
44
homepage: https://github.com/CodingAleCR/http_interceptor
55
issue_tracker: https://github.com/CodingAleCR/http_interceptor/issues
66
repository: https://github.com/CodingAleCR/http_interceptor

0 commit comments

Comments
 (0)