OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library unittest.remote_exception; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 |
| 11 import 'expect.dart'; |
| 12 |
| 13 /// An exception that was thrown remotely. |
| 14 /// |
| 15 /// This could be an exception thrown in a different isolate, a different |
| 16 /// process, or on an entirely different computer. |
| 17 class RemoteException implements Exception { |
| 18 /// The original exception's message, if it had one. |
| 19 /// |
| 20 /// If the original exception was a plain string, this will contain that |
| 21 /// string. |
| 22 final String message; |
| 23 |
| 24 /// The value of the original exception's `runtimeType.toString()`. |
| 25 final String type; |
| 26 |
| 27 /// The value of the original exception's `toString()`. |
| 28 final String _toString; |
| 29 |
| 30 /// Serializes [error] and [stackTrace] into a JSON-safe object. |
| 31 /// |
| 32 /// Other than JSON- and isolate-safety, no guarantees are made about the |
| 33 /// serialized format. |
| 34 static serialize(error, StackTrace stackTrace) { |
| 35 var message; |
| 36 if (error is String) { |
| 37 message = error; |
| 38 } else { |
| 39 try { |
| 40 message = error.message.toString(); |
| 41 } on NoSuchMethodError catch (_) { |
| 42 // Do nothing. |
| 43 } |
| 44 } |
| 45 |
| 46 return { |
| 47 'message': message, |
| 48 'type': error.runtimeType.toString(), |
| 49 'isTestFailure': error is TestFailure, |
| 50 'toString': error.toString(), |
| 51 'stackChain': new Chain.forTrace(stackTrace).toString() |
| 52 }; |
| 53 } |
| 54 |
| 55 /// Deserializes an exception serialized with [RemoteException.serialize]. |
| 56 /// |
| 57 /// The returned [AsyncError] is guaranteed to have a [RemoteException] as its |
| 58 /// error and a [Chain] as its stack trace. |
| 59 static AsyncError deserialize(serialized) { |
| 60 var exception; |
| 61 if (serialized['isTestFailure']) { |
| 62 exception = new RemoteTestFailure._( |
| 63 serialized['message'], |
| 64 serialized['type'], |
| 65 serialized['toString']); |
| 66 } else { |
| 67 exception = new RemoteException._( |
| 68 serialized['message'], |
| 69 serialized['type'], |
| 70 serialized['toString']); |
| 71 } |
| 72 |
| 73 return new AsyncError(exception, new Chain.parse(serialized['stackChain'])); |
| 74 } |
| 75 |
| 76 RemoteException._(this.message, this.type, this._toString); |
| 77 |
| 78 String toString() => _toString; |
| 79 } |
| 80 |
| 81 /// A subclass of [RemoteException] that implements [TestFailure]. |
| 82 /// |
| 83 /// It's important to preserve [TestFailure]-ness, because tests have different |
| 84 /// results depending on whether an exception was a failure or an error. |
| 85 class RemoteTestFailure extends RemoteException implements TestFailure { |
| 86 RemoteTestFailure._(String message, String type, String toString) |
| 87 : super._(message, type, toString); |
| 88 } |
OLD | NEW |