OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 barback.serialize; | 5 library barback.serialize; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 /// The name of the type of exception thrown. | 81 /// The name of the type of exception thrown. |
82 /// | 82 /// |
83 /// This is the return value of [error.runtimeType.toString()]. Keep in mind | 83 /// This is the return value of [error.runtimeType.toString()]. Keep in mind |
84 /// that objects in different libraries may have the same type name. | 84 /// that objects in different libraries may have the same type name. |
85 final String type; | 85 final String type; |
86 | 86 |
87 /// The exception's message, or its [toString] if it didn't expose a `message` | 87 /// The exception's message, or its [toString] if it didn't expose a `message` |
88 /// property. | 88 /// property. |
89 final String message; | 89 final String message; |
90 | 90 |
91 /// The exception's stack trace, or `null` if no stack trace was available. | 91 /// The exception's stack chain, or `null` if no stack chain was available. |
92 final Trace stackTrace; | 92 final Chain stackTrace; |
93 | 93 |
94 /// Loads a [CrossIsolateException] from a serialized representation. | 94 /// Loads a [CrossIsolateException] from a serialized representation. |
95 /// | 95 /// |
96 /// [error] should be the result of [CrossIsolateException.serialize]. | 96 /// [error] should be the result of [CrossIsolateException.serialize]. |
97 factory CrossIsolateException.deserialize(Map error) { | 97 CrossIsolateException.deserialize(Map error) |
98 var type = error['type']; | 98 : type = error['type'], |
99 var message = error['message']; | 99 message = error['message'], |
100 var stackTrace = error['stack'] == null ? null : | 100 stackTrace = error['stack'] == null ? null : |
101 new Trace.parse(error['stack']); | 101 new Chain.parse(error['stack']); |
102 return new CrossIsolateException._(type, message, stackTrace); | |
103 } | |
104 | |
105 /// Loads a [CrossIsolateException] from a serialized representation. | |
106 /// | |
107 /// [error] should be the result of [CrossIsolateException.serialize]. | |
108 CrossIsolateException._(this.type, this.message, this.stackTrace); | |
109 | 102 |
110 /// Serializes [error] to an object that can safely be passed across isolate | 103 /// Serializes [error] to an object that can safely be passed across isolate |
111 /// boundaries. | 104 /// boundaries. |
112 static Map serialize(error, [StackTrace stack]) { | 105 static Map serialize(error, [StackTrace stack]) { |
113 if (stack == null && error is Error) stack = error.stackTrace; | 106 if (stack == null && error is Error) stack = error.stackTrace; |
114 return { | 107 return { |
115 'type': error.runtimeType.toString(), | 108 'type': error.runtimeType.toString(), |
116 'message': _getErrorMessage(error), | 109 'message': _getErrorMessage(error), |
117 'stack': stack == null ? null : stack.toString() | 110 'stack': stack == null ? null : new Chain.forTrace(stack).toString() |
118 }; | 111 }; |
119 } | 112 } |
120 | 113 |
121 String toString() => "$message\n$stackTrace"; | 114 String toString() => "$message\n$stackTrace"; |
122 } | 115 } |
123 | 116 |
124 // Get a string description of an exception. | 117 // Get a string description of an exception. |
125 // | 118 // |
126 // Most exception types have a "message" property. We prefer this since | 119 // Most exception types have a "message" property. We prefer this since |
127 // it skips the "Exception:", "HttpException:", etc. prefix that calling | 120 // it skips the "Exception:", "HttpException:", etc. prefix that calling |
128 // toString() adds. But, alas, "message" isn't actually defined in the | 121 // toString() adds. But, alas, "message" isn't actually defined in the |
129 // base Exception type so there's no easy way to know if it's available | 122 // base Exception type so there's no easy way to know if it's available |
130 // short of a giant pile of type tests for each known exception type. | 123 // short of a giant pile of type tests for each known exception type. |
131 // | 124 // |
132 // So just try it. If it throws, default to toString(). | 125 // So just try it. If it throws, default to toString(). |
133 String _getErrorMessage(error) { | 126 String _getErrorMessage(error) { |
134 try { | 127 try { |
135 return error.message; | 128 return error.message; |
136 } on NoSuchMethodError catch (_) { | 129 } on NoSuchMethodError catch (_) { |
137 return error.toString(); | 130 return error.toString(); |
138 } | 131 } |
139 } | 132 } |
OLD | NEW |