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 | |
kevmoo
2015/02/12 02:24:29
Comments about what's expected for [error]?
Strin
nweiz
2015/02/12 19:03:41
Any error object—which is to say, theoretically an
| |
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. | |
kevmoo
2015/02/12 02:24:29
Fall back to message.toString()?
nweiz
2015/02/12 19:03:41
If we're here, there is no message property.
| |
43 } | |
44 } | |
45 | |
46 return { | |
47 'message': message, | |
48 'type': error.runtimeType.toString(), | |
49 'toString': error.toString(), | |
50 'stackChain': new Chain.forTrace(stackTrace).toString() | |
51 }; | |
52 } | |
53 | |
54 /// Deserializes an exception serialized with [RemoteException.serialize]. | |
55 /// | |
56 /// The returned [AsyncError] is guaranteed to have a [RemoteException] as its | |
57 /// error and a [Chain] as its stack trace. | |
58 static AsyncError deserialize(serialized) { | |
59 var exception; | |
60 if (serialized['type'] == 'TestFailure') { | |
kevmoo
2015/02/12 02:24:29
You could be a bit more clever coordinating betwee
nweiz
2015/02/12 19:03:41
Good point. Done.
| |
61 exception = new RemoteTestFailure._(serialized['message']); | |
62 } else { | |
63 exception = new RemoteException._( | |
64 serialized['message'], | |
65 serialized['type'], | |
66 serialized['toString']); | |
67 } | |
68 | |
69 return new AsyncError(exception, new Chain.parse(serialized['stackChain'])); | |
70 } | |
71 | |
72 RemoteException._(this.message, this.type, this._toString); | |
73 | |
74 String toString() => _toString; | |
75 } | |
76 | |
77 /// A subclass of [RemoteException] that implements [TestFailure]. | |
78 /// | |
79 /// It's important to preserve [TestFailure]-ness, because tests have different | |
80 /// results depending on whether an exception was a failure or an error. | |
81 class RemoteTestFailure implements TestFailure, RemoteException { | |
kevmoo
2015/02/12 02:24:29
Analyzer complains that this class does not implem
nweiz
2015/02/12 19:03:41
Done.
| |
82 final String message; | |
83 final type = "TestFailure"; | |
kevmoo
2015/02/12 02:24:29
Could this be a property? Avoid allocating a Strin
nweiz
2015/02/12 19:03:41
I removed this, but:
On 2015/02/12 02:24:29, kevm
| |
84 | |
85 RemoteTestFailure._(this.message); | |
86 | |
87 String toString() => message; | |
88 } | |
OLD | NEW |