Line data Source code
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 3 : // BSD-style license that can be found in the LICENSE file. 4 : 5 : import 'dart:async'; 6 : 7 : import 'result.dart'; 8 : import 'value.dart'; 9 : 10 : /// A result representing a thrown error. 11 : class ErrorResult implements Result<Never> { 12 : /// The error object that was thrown. 13 : final Object error; 14 : 15 : /// The stack trace corresponding to where [error] was thrown. 16 : final StackTrace stackTrace; 17 : 18 0 : @override 19 : bool get isValue => false; 20 0 : @override 21 : bool get isError => true; 22 0 : @override 23 : ValueResult<Never>? get asValue => null; 24 0 : @override 25 : ErrorResult get asError => this; 26 : 27 0 : ErrorResult(this.error, [StackTrace? stackTrace]) 28 0 : : stackTrace = stackTrace ?? AsyncError.defaultStackTrace(error); 29 : 30 0 : @override 31 : void complete(Completer completer) { 32 0 : completer.completeError(error, stackTrace); 33 : } 34 : 35 0 : @override 36 : void addTo(EventSink sink) { 37 0 : sink.addError(error, stackTrace); 38 : } 39 : 40 0 : @override 41 0 : Future<Never> get asFuture => Future<Never>.error(error, stackTrace); 42 : 43 : /// Calls an error handler with the error and stacktrace. 44 : /// 45 : /// An async error handler function is either a function expecting two 46 : /// arguments, which will be called with the error and the stack trace, or it 47 : /// has to be a function expecting only one argument, which will be called 48 : /// with only the error. 49 0 : void handle(Function errorHandler) { 50 0 : if (errorHandler is ZoneBinaryCallback) { 51 0 : errorHandler(error, stackTrace); 52 : } else { 53 0 : errorHandler(error); 54 : } 55 : } 56 : 57 0 : @override 58 0 : int get hashCode => error.hashCode ^ stackTrace.hashCode ^ 0x1d61823f; 59 : 60 : /// This is equal only to an error result with equal [error] and [stackTrace]. 61 0 : @override 62 : bool operator ==(Object other) => 63 0 : other is ErrorResult && 64 0 : error == other.error && 65 0 : stackTrace == other.stackTrace; 66 : }