diff --git a/utils/pub/command_lish.dart b/utils/pub/command_lish.dart index e57402ccad9..544fb9451fe 100644 --- a/utils/pub/command_lish.dart +++ b/utils/pub/command_lish.dart @@ -72,16 +72,15 @@ class LishCommand extends PubCommand { }).then((location) => client.get(location)) .then(handleJsonSuccess); }).catchError((asyncError) { - var e = getRealError(asyncError); - if (e is! PubHttpException) throw asyncError; - var url = e.response.request.url; + if (asyncError.error is! PubHttpException) throw asyncError; + var url = asyncError.error.response.request.url; if (url.toString() == cloudStorageUrl.toString()) { // TODO(nweiz): the response may have XML-formatted information about // the error. Try to parse that out once we have an easily-accessible // XML parser. throw 'Failed to upload the package.'; } else if (url.origin == server.origin) { - handleJsonError(e.response); + handleJsonError(asyncError.error.response); } }); } diff --git a/utils/pub/command_uploader.dart b/utils/pub/command_uploader.dart index ef37210b975..eb3cd4ac0f0 100644 --- a/utils/pub/command_uploader.dart +++ b/utils/pub/command_uploader.dart @@ -77,9 +77,8 @@ class UploaderCommand extends PubCommand { } }); }).then(handleJsonSuccess).catchError((asyncError) { - var e = getRealError(asyncError); - if (e is! PubHttpException) throw asyncError; - handleJsonError(e.response); + if (asyncError.error is! PubHttpException) throw asyncError; + handleJsonError(asyncError.error.response); }); } } diff --git a/utils/pub/hosted_source.dart b/utils/pub/hosted_source.dart index 28bb1611d67..3ae7c5581f8 100644 --- a/utils/pub/hosted_source.dart +++ b/utils/pub/hosted_source.dart @@ -120,19 +120,18 @@ class HostedSource extends Source { /// this tries to translate into a more user friendly error message. Always /// throws an error, either the original one or a better one. void _throwFriendlyError(AsyncError asyncError, package, url) { - var ex = getRealError(asyncError); - - if (ex is PubHttpException && ex.response.statusCode == 404) { + if (asyncError.error is PubHttpException && + asyncError.error.response.statusCode == 404) { throw 'Could not find package "$package" at $url.'; } - if (ex is TimeoutException) { + if (asyncError.error is TimeoutException) { throw 'Timed out trying to find package "$package" at $url.'; } - if (ex is io.SocketIOException) { + if (asyncError.error is io.SocketIOException) { throw 'Got socket error trying to find package "$package" at $url.\n' - '${ex.osError}'; + '${asyncError.error.osError}'; } // Otherwise re-throw the original exception. diff --git a/utils/pub/http.dart b/utils/pub/http.dart index d02c0ab38df..0a2bf2eeddc 100644 --- a/utils/pub/http.dart +++ b/utils/pub/http.dart @@ -58,13 +58,12 @@ class PubHttpClient extends http.BaseClient { throw new PubHttpException(response); }); }).catchError((asyncError) { - var e = getRealError(asyncError); - if (e is SocketIOException && - e.osError != null && - (e.osError.errorCode == 8 || - e.osError.errorCode == -2 || - e.osError.errorCode == -5 || - e.osError.errorCode == 11004)) { + if (asyncError.error is SocketIOException && + asyncError.error.osError != null && + (asyncError.error.osError.errorCode == 8 || + asyncError.error.osError.errorCode == -2 || + asyncError.error.osError.errorCode == -5 || + asyncError.error.osError.errorCode == 11004)) { throw 'Could not resolve URL "${request.url.origin}".'; } throw asyncError; diff --git a/utils/pub/io.dart b/utils/pub/io.dart index 2a4e89b1684..f70849f9a6d 100644 --- a/utils/pub/io.dart +++ b/utils/pub/io.dart @@ -189,10 +189,10 @@ Future ensureDir(path) { return ensureDir(dirname(path)).then((_) { return createDir(path).catchError((asyncError) { - var error = getRealError(asyncError); - if (error is! DirectoryIOException) throw asyncError; + if (asyncError.error is! DirectoryIOException) throw asyncError; // Error 17 means the directory already exists (or 183 on Windows). - if (error.osError.errorCode == 17 || error.osError.errorCode == 183) { + if (asyncError.error.osError.errorCode == 17 || + asyncError.error.osError.errorCode == 183) { log.fine("Got 'already exists' error when creating directory."); return _getDirectory(path); } diff --git a/utils/pub/oauth2.dart b/utils/pub/oauth2.dart index 44ac07f59de..24d798afd60 100644 --- a/utils/pub/oauth2.dart +++ b/utils/pub/oauth2.dart @@ -75,14 +75,15 @@ Future withClient(SystemCache cache, Future fn(Client client)) { return _saveCredentials(cache, client.credentials); }); }).catchError((asyncError) { - var e = getRealError(asyncError); - if (e is ExpirationException) { + if (asyncError.error is ExpirationException) { log.error("Pub's authorization to upload packages has expired and " "can't be automatically refreshed."); return withClient(cache, fn); - } else if (e is AuthorizationException) { + } else if (asyncError.error is AuthorizationException) { var message = "OAuth2 authorization failed"; - if (e.description != null) message = "$message (${e.description})"; + if (asyncError.error.description != null) { + message = "$message (${asyncError.error.description})"; + } log.error("$message."); return clearCredentials(cache).then((_) => withClient(cache, fn)); } else { diff --git a/utils/pub/pub.dart b/utils/pub/pub.dart index 157b6805154..ae6c13a7df7 100644 --- a/utils/pub/pub.dart +++ b/utils/pub/pub.dart @@ -262,7 +262,7 @@ abstract class PubCommand { future .then((_) => cache_.deleteTempDir()) .catchError((asyncError) { - var e = getRealError(asyncError); + var e = asyncError.error; if (e is PubspecNotFoundException && e.name == null) { e = 'Could not find a file named "pubspec.yaml" in the directory ' '${path.current}.'; @@ -271,7 +271,7 @@ abstract class PubCommand { '${basename(path.current)}").'; } - handleError(e, getRealStackTrace(asyncError)); + handleError(e, asyncError.stackTrace); }) // Explicitly exit on success to ensure that any dangling dart:io handles // don't cause the process to never terminate. diff --git a/utils/pub/utils.dart b/utils/pub/utils.dart index e605579417e..aac48fe0e30 100644 --- a/utils/pub/utils.dart +++ b/utils/pub/utils.dart @@ -177,27 +177,3 @@ void mapAddAll(Map destination, Map source) => /// replacing `+` with ` `. String urlDecode(String encoded) => decodeUriComponent(encoded.replaceAll("+", " ")); - -// TODO(rnystrom): Remove this when #7781 is fixed. -/// When an error is rethrown in an async callback, you can end up with nested -/// AsyncErrors. This unwraps them to find the real originating error. -getRealError(error) { - while (error is AsyncError) { - error = error.error; - } - - return error; -} - -// TODO(nweiz): Remove this when #7781 is fixed. -/// When an error is rethrown in an async callback, you can end up with nested -/// AsyncErrors. This unwraps them to find the real originating stack trace. -getRealStackTrace(error) { - var trace = null; - while (error is AsyncError) { - trace = error.stackTrace; - error = error.error; - } - - return trace; -} diff --git a/utils/tests/pub/curl_client_test.dart b/utils/tests/pub/curl_client_test.dart index 2baa84dd6d5..eda16bf2062 100644 --- a/utils/tests/pub/curl_client_test.dart +++ b/utils/tests/pub/curl_client_test.dart @@ -168,12 +168,6 @@ class _Parse extends BaseMatcher { } } -// ---------------------------------------------------------------------------- - -// TODO(nweiz): All the code from here to the next "---..." line was also copied -// from pkg/http/test/utils.dart. However, it was also modified to use -// getRealError in order to work around issue 7781. - // TODO(nweiz): remove this once it's built in to unittest /// A matcher for StateErrors. const isStateError = const _StateError(); @@ -184,7 +178,7 @@ const Matcher throwsStateError = class _StateError extends TypeMatcher { const _StateError() : super("StateError"); - bool matches(item, MatchState matchState) => getRealError(item) is StateError; + bool matches(item, MatchState matchState) => item is StateError; } /// A matcher for HttpExceptions. @@ -196,8 +190,7 @@ const Matcher throwsHttpException = class _HttpException extends TypeMatcher { const _HttpException() : super("HttpException"); - bool matches(item, MatchState matchState) => - getRealError(item) is HttpException; + bool matches(item, MatchState matchState) => item is HttpException; } /// A matcher for RedirectLimitExceededExceptions. @@ -213,7 +206,7 @@ class _RedirectLimitExceededException extends TypeMatcher { super("RedirectLimitExceededException"); bool matches(item, MatchState matchState) => - getRealError(item) is RedirectLimitExceededException; + item is RedirectLimitExceededException; } // ----------------------------------------------------------------------------