OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 pub.asset.serialize.exception; | |
6 | |
7 import 'package:barback/barback.dart'; | |
8 import 'package:stack_trace/stack_trace.dart'; | |
9 | |
10 import '../utils.dart'; | |
11 | |
12 /// An exception that was originally raised in another isolate. | |
13 /// | |
14 /// Exception objects can't cross isolate boundaries in general, so this class | |
15 /// wraps as much information as can be consistently serialized. | |
16 class CrossIsolateException implements Exception { | |
17 /// The name of the type of exception thrown. | |
18 /// | |
19 /// This is the return value of [error.runtimeType.toString()]. Keep in mind | |
20 /// that objects in different libraries may have the same type name. | |
21 final String type; | |
22 | |
23 /// The exception's message, or its [toString] if it didn't expose a `message` | |
24 /// property. | |
25 final String message; | |
26 | |
27 /// The exception's stack chain, or `null` if no stack chain was available. | |
28 final Chain stackTrace; | |
29 | |
30 /// Loads a [CrossIsolateException] from a serialized representation. | |
31 /// | |
32 /// [error] should be the result of [CrossIsolateException.serialize]. | |
33 CrossIsolateException.deserialize(Map error) | |
34 : type = error['type'], | |
35 message = error['message'], | |
36 stackTrace = error['stack'] == null ? null : | |
37 new Chain.parse(error['stack']); | |
38 | |
39 /// Serializes [error] to an object that can safely be passed across isolate | |
40 /// boundaries. | |
41 static Map serialize(error, [StackTrace stack]) { | |
42 if (stack == null && error is Error) stack = error.stackTrace; | |
43 return { | |
44 'type': error.runtimeType.toString(), | |
45 'message': getErrorMessage(error), | |
46 'stack': stack == null ? null : new Chain.forTrace(stack).toString() | |
47 }; | |
48 } | |
49 | |
50 String toString() => "$message\n$stackTrace"; | |
51 } | |
52 | |
53 /// An [AssetNotFoundException] that was originally raised in another isolate. | |
54 class _CrossIsolateAssetNotFoundException extends CrossIsolateException | |
55 implements AssetNotFoundException { | |
56 final AssetId id; | |
57 | |
58 String get message => "Could not find asset $id."; | |
59 | |
60 /// Loads a [_CrossIsolateAssetNotFoundException] from a serialized | |
61 /// representation. | |
62 /// | |
63 /// [error] should be the result of | |
64 /// [_CrossIsolateAssetNotFoundException.serialize]. | |
65 _CrossIsolateAssetNotFoundException.deserialize(Map error) | |
66 : id = new AssetId(error['package'], error['path']), | |
67 super.deserialize(error); | |
68 | |
69 /// Serializes [error] to an object that can safely be passed across isolate | |
70 /// boundaries. | |
71 static Map serialize(AssetNotFoundException error, [StackTrace stack]) { | |
72 var map = CrossIsolateException.serialize(error); | |
73 map['package'] = error.id.package; | |
74 map['path'] = error.id.path; | |
75 return map; | |
76 } | |
77 } | |
78 | |
79 /// Serializes [error] to an object that can safely be passed across isolate | |
80 /// boundaries. | |
81 /// | |
82 /// This handles [AssetNotFoundException]s specially, ensuring that their | |
83 /// metadata is preserved. | |
84 Map serializeException(error, [StackTrace stack]) { | |
85 if (error is AssetNotFoundException) { | |
86 return _CrossIsolateAssetNotFoundException.serialize(error, stack); | |
87 } else { | |
88 return CrossIsolateException.serialize(error, stack); | |
89 } | |
90 } | |
91 | |
92 /// Loads an exception from a serialized representation. | |
93 /// | |
94 /// This handles [AssetNotFoundException]s specially, ensuring that their | |
95 /// metadata is preserved. | |
96 CrossIsolateException deserializeException(Map error) { | |
97 if (error['type'] == 'AssetNotFoundException') { | |
98 return new _CrossIsolateAssetNotFoundException.deserialize(error); | |
99 } else { | |
100 return new CrossIsolateException.deserialize(error); | |
101 } | |
102 } | |
OLD | NEW |