Skip to content

Commit

Permalink
Throw exceptions correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed May 23, 2019
1 parent 8e062e7 commit ddc934e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions package_src/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ class Dio {
response = _makeRequest<T>(data, cancelToken);
} else {
// Otherwise, use the Future value as the request result.
// If the return type is Error, we should throw it
if (data is Error) throw _assureDioError(data);
// If the return type is Exception, we should throw it
if (data is Exception) throw _assureDioError(data);
var r = _assureResponse<T>(data);

response = r;
Expand Down Expand Up @@ -905,7 +905,7 @@ class Dio {
return future.then<Response<T>>((data) {
// Strictly be a DioError instance, but we relax the restrictions
// if (data is DioError)
if (data is Error) {
if (data is Exception) {
return reject<T>(data);
}
return resolve<T>(data);
Expand Down Expand Up @@ -980,7 +980,7 @@ class Dio {
DioError _assureDioError(err) {
if (err is DioError) {
return err;
} else if (err is Error) {
} else if (err is Exception) {
err = new DioError(
response: null,
message: err.toString(),
Expand Down
32 changes: 32 additions & 0 deletions package_src/test/exception_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:dio/dio.dart';
import 'package:test/test.dart';

void main() {
test("catch DioError", () async {
dynamic error;

try {
await Dio().get("https://does.not.exist");
fail("did not throw");
} on DioError catch (e) {
error = e;
}

expect(error, isNotNull);
expect(error is Exception, isTrue);
});

test("catch DioError as Exception", () async {
dynamic error;

try {
await Dio().get("https://does.not.exist");
fail("did not throw");
} on Exception catch (e) {
error = e;
}

expect(error, isNotNull);
expect(error is Exception, isTrue);
});
}

0 comments on commit ddc934e

Please sign in to comment.