OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 library unittest.remote_exception; | 5 library unittest.remote_exception; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:isolate'; |
8 | 9 |
9 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
10 | 11 |
11 import 'expect.dart'; | 12 import 'expect.dart'; |
12 | 13 |
13 /// An exception that was thrown remotely. | 14 /// An exception that was thrown remotely. |
14 /// | 15 /// |
15 /// This could be an exception thrown in a different isolate, a different | 16 /// This could be an exception thrown in a different isolate, a different |
16 /// process, or on an entirely different computer. | 17 /// process, or on an entirely different computer. |
17 class RemoteException implements Exception { | 18 class RemoteException implements Exception { |
(...skipping 18 matching lines...) Loading... |
36 if (error is String) { | 37 if (error is String) { |
37 message = error; | 38 message = error; |
38 } else { | 39 } else { |
39 try { | 40 try { |
40 message = error.message.toString(); | 41 message = error.message.toString(); |
41 } on NoSuchMethodError catch (_) { | 42 } on NoSuchMethodError catch (_) { |
42 // Do nothing. | 43 // Do nothing. |
43 } | 44 } |
44 } | 45 } |
45 | 46 |
| 47 // It's possible (although unlikely) for a user-defined class to have |
| 48 // multiple of these supertypes. That's fine, though, since we only care |
| 49 // about core-library-raised IsolateSpawnExceptions anyway. |
| 50 var supertype; |
| 51 if (error is TestFailure) { |
| 52 supertype = 'TestFailure'; |
| 53 } else if (error is IsolateSpawnException) { |
| 54 supertype = 'IsolateSpawnException'; |
| 55 } |
| 56 |
46 return { | 57 return { |
47 'message': message, | 58 'message': message, |
48 'type': error.runtimeType.toString(), | 59 'type': error.runtimeType.toString(), |
49 'isTestFailure': error is TestFailure, | 60 'supertype': supertype, |
50 'toString': error.toString(), | 61 'toString': error.toString(), |
51 'stackChain': new Chain.forTrace(stackTrace).toString() | 62 'stackChain': new Chain.forTrace(stackTrace).toString() |
52 }; | 63 }; |
53 } | 64 } |
54 | 65 |
55 /// Deserializes an exception serialized with [RemoteException.serialize]. | 66 /// Deserializes an exception serialized with [RemoteException.serialize]. |
56 /// | 67 /// |
57 /// The returned [AsyncError] is guaranteed to have a [RemoteException] as its | 68 /// The returned [AsyncError] is guaranteed to have a [RemoteException] as its |
58 /// error and a [Chain] as its stack trace. | 69 /// error and a [Chain] as its stack trace. |
59 static AsyncError deserialize(serialized) { | 70 static AsyncError deserialize(serialized) { |
60 var exception; | 71 return new AsyncError( |
61 if (serialized['isTestFailure']) { | 72 _deserializeException(serialized), |
62 exception = new RemoteTestFailure._( | 73 new Chain.parse(serialized['stackChain'])); |
63 serialized['message'], | 74 } |
64 serialized['type'], | 75 |
65 serialized['toString']); | 76 /// Deserializes the exception portion of [serialized]. |
66 } else { | 77 static RemoteException _deserializeException(serialized) { |
67 exception = new RemoteException._( | 78 var message = serialized['message']; |
68 serialized['message'], | 79 var type = serialized['type']; |
69 serialized['type'], | 80 var toString = serialized['toString']; |
70 serialized['toString']); | 81 |
| 82 switch (serialized['supertype']) { |
| 83 case 'TestFailure': |
| 84 return new _RemoteTestFailure(message, type, toString); |
| 85 case 'IsolateSpawnException': |
| 86 return new _RemoteIsolateSpawnException(message, type, toString); |
| 87 default: |
| 88 return new RemoteException._(message, type, toString); |
71 } | 89 } |
72 | |
73 return new AsyncError(exception, new Chain.parse(serialized['stackChain'])); | |
74 } | 90 } |
75 | 91 |
76 RemoteException._(this.message, this.type, this._toString); | 92 RemoteException._(this.message, this.type, this._toString); |
77 | 93 |
78 String toString() => _toString; | 94 String toString() => _toString; |
79 } | 95 } |
80 | 96 |
81 /// A subclass of [RemoteException] that implements [TestFailure]. | 97 /// A subclass of [RemoteException] that implements [TestFailure]. |
82 /// | 98 /// |
83 /// It's important to preserve [TestFailure]-ness, because tests have different | 99 /// It's important to preserve [TestFailure]-ness, because tests have different |
84 /// results depending on whether an exception was a failure or an error. | 100 /// results depending on whether an exception was a failure or an error. |
85 class RemoteTestFailure extends RemoteException implements TestFailure { | 101 class _RemoteTestFailure extends RemoteException implements TestFailure { |
86 RemoteTestFailure._(String message, String type, String toString) | 102 _RemoteTestFailure(String message, String type, String toString) |
87 : super._(message, type, toString); | 103 : super._(message, type, toString); |
88 } | 104 } |
| 105 |
| 106 /// A subclass of [RemoteException] that implements [IsolateSpawnException]. |
| 107 class _RemoteIsolateSpawnException extends RemoteException |
| 108 implements IsolateSpawnException { |
| 109 _RemoteIsolateSpawnException(String message, String type, String toString) |
| 110 : super._(message, type, toString); |
| 111 } |
OLD | NEW |