| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import '../result.dart'; | 7 import 'result.dart'; |
| 8 import 'value.dart'; | 8 import 'value.dart'; |
| 9 | 9 |
| 10 /// A result representing a thrown error. | 10 /// A result representing a thrown error. |
| 11 class ErrorResult<T> implements Result<T> { | 11 class ErrorResult implements Result<Null> { |
| 12 /// The error object that was thrown. |
| 12 final error; | 13 final error; |
| 14 |
| 15 /// The stack trace corresponding to where [error] was thrown. |
| 13 final StackTrace stackTrace; | 16 final StackTrace stackTrace; |
| 14 | 17 |
| 15 bool get isValue => false; | 18 bool get isValue => false; |
| 16 bool get isError => true; | 19 bool get isError => true; |
| 17 ValueResult<T> get asValue => null; | 20 ValueResult<Null> get asValue => null; |
| 18 ErrorResult<T> get asError => this; | 21 ErrorResult get asError => this; |
| 19 | 22 |
| 20 ErrorResult(this.error, this.stackTrace); | 23 ErrorResult(this.error, this.stackTrace); |
| 21 | 24 |
| 22 void complete(Completer completer) { | 25 void complete(Completer completer) { |
| 23 completer.completeError(error, stackTrace); | 26 completer.completeError(error, stackTrace); |
| 24 } | 27 } |
| 25 | 28 |
| 26 void addTo(EventSink sink) { | 29 void addTo(EventSink sink) { |
| 27 sink.addError(error, stackTrace); | 30 sink.addError(error, stackTrace); |
| 28 } | 31 } |
| 29 | 32 |
| 30 Future<T> get asFuture => new Future.error(error, stackTrace); | 33 Future<Null> get asFuture => new Future<Null>.error(error, stackTrace); |
| 31 | 34 |
| 32 /// Calls an error handler with the error and stacktrace. | 35 /// Calls an error handler with the error and stacktrace. |
| 33 /// | 36 /// |
| 34 /// An async error handler function is either a function expecting two | 37 /// An async error handler function is either a function expecting two |
| 35 /// arguments, which will be called with the error and the stack trace, or it | 38 /// arguments, which will be called with the error and the stack trace, or it |
| 36 /// has to be a function expecting only one argument, which will be called | 39 /// has to be a function expecting only one argument, which will be called |
| 37 /// with only the error. | 40 /// with only the error. |
| 38 void handle(Function errorHandler) { | 41 void handle(Function errorHandler) { |
| 39 if (errorHandler is ZoneBinaryCallback) { | 42 if (errorHandler is ZoneBinaryCallback) { |
| 40 errorHandler(error, stackTrace); | 43 errorHandler(error, stackTrace); |
| 41 } else { | 44 } else { |
| 42 errorHandler(error); | 45 errorHandler(error); |
| 43 } | 46 } |
| 44 } | 47 } |
| 48 |
| 49 int get hashCode => error.hashCode ^ stackTrace.hashCode ^ 0x1d61823f; |
| 50 |
| 51 /// This is equal only to an error result with equal [error] and [stackTrace]. |
| 52 bool operator ==(Object other) => |
| 53 other is ErrorResult && |
| 54 error == other.error && |
| 55 stackTrace == other.stackTrace; |
| 45 } | 56 } |
| OLD | NEW |