Skip to content

Commit ddc934e

Browse files
committed
Throw exceptions correctly
1 parent 8e062e7 commit ddc934e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

package_src/lib/src/dio.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ class Dio {
703703
response = _makeRequest<T>(data, cancelToken);
704704
} else {
705705
// Otherwise, use the Future value as the request result.
706-
// If the return type is Error, we should throw it
707-
if (data is Error) throw _assureDioError(data);
706+
// If the return type is Exception, we should throw it
707+
if (data is Exception) throw _assureDioError(data);
708708
var r = _assureResponse<T>(data);
709709

710710
response = r;
@@ -905,7 +905,7 @@ class Dio {
905905
return future.then<Response<T>>((data) {
906906
// Strictly be a DioError instance, but we relax the restrictions
907907
// if (data is DioError)
908-
if (data is Error) {
908+
if (data is Exception) {
909909
return reject<T>(data);
910910
}
911911
return resolve<T>(data);
@@ -980,7 +980,7 @@ class Dio {
980980
DioError _assureDioError(err) {
981981
if (err is DioError) {
982982
return err;
983-
} else if (err is Error) {
983+
} else if (err is Exception) {
984984
err = new DioError(
985985
response: null,
986986
message: err.toString(),

package_src/test/exception_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test("catch DioError", () async {
6+
dynamic error;
7+
8+
try {
9+
await Dio().get("https://does.not.exist");
10+
fail("did not throw");
11+
} on DioError catch (e) {
12+
error = e;
13+
}
14+
15+
expect(error, isNotNull);
16+
expect(error is Exception, isTrue);
17+
});
18+
19+
test("catch DioError as Exception", () async {
20+
dynamic error;
21+
22+
try {
23+
await Dio().get("https://does.not.exist");
24+
fail("did not throw");
25+
} on Exception catch (e) {
26+
error = e;
27+
}
28+
29+
expect(error, isNotNull);
30+
expect(error is Exception, isTrue);
31+
});
32+
}

0 commit comments

Comments
 (0)