Skip to content

Commit 692adc5

Browse files
authored
feat: add request to retry on exception policy (#106)
* feat: add request to shouldAttemptRetryOnException * test: update test * chore: cleanup * example: update shouldAttemptRetryOnException in example * docs: updated changelog * ver: 2.0.0-beta.4
1 parent 1b7c747 commit 692adc5

File tree

6 files changed

+17
-6
lines changed

6 files changed

+17
-6
lines changed

CHANGELOG.md

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

3+
## 2.0.0-beta.4
4+
5+
- ❗️🛠  Changed: `shouldAttemptRetryOnException` will now also pass the `BaseRequest`.
6+
- 🚦  Tests: Updated tests.
7+
38
## 2.0.0-beta.3
49

510
- 🐞  Fixed: `MultipartRequest` does not get intercepted correctly (has missing fields).

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class ExpiredTokenRetryPolicy extends RetryPolicy {
355355
int get maxRetryAttempts => 2;
356356

357357
@override
358-
bool shouldAttemptRetryOnException(Exception reason) {
358+
bool shouldAttemptRetryOnException(Exception reason, BaseRequest request) {
359359
log(reason.toString());
360360

361361
return false;

lib/http/intercepted_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class InterceptedClient extends BaseClient {
278278
} on Exception catch (error) {
279279
if (retryPolicy != null &&
280280
retryPolicy!.maxRetryAttempts > _retryCount &&
281-
retryPolicy!.shouldAttemptRetryOnException(error)) {
281+
retryPolicy!.shouldAttemptRetryOnException(error, request)) {
282282
_retryCount += 1;
283283
return _attemptRequest(request);
284284
} else {

lib/models/retry_policy.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import 'package:http/http.dart';
3636
abstract class RetryPolicy {
3737
/// Defines whether the request should be retried when an Exception occurs
3838
/// while making said request to the server.
39-
bool shouldAttemptRetryOnException(Exception reason) => false;
39+
bool shouldAttemptRetryOnException(Exception reason, BaseRequest request) =>
40+
false;
4041

4142
/// Defines whether the request should be retried after the request has
4243
/// received `response` from the server.

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: 2.0.0-beta.3
3+
version: 2.0.0-beta.4
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

test/models/retry_policy_test.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ main() {
1717
group("shouldAttemptRetryOnException", () {
1818
test("returns false by default", () {
1919
expect(
20-
testObject
21-
.shouldAttemptRetryOnException(Exception("Test Exception.")),
20+
testObject.shouldAttemptRetryOnException(
21+
Exception("Test Exception."),
22+
Request(
23+
'GET',
24+
Uri(),
25+
),
26+
),
2227
false);
2328
});
2429
});

0 commit comments

Comments
 (0)