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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 return { | 107 return { |
108 'type': error.runtimeType.toString(), | 108 'type': error.runtimeType.toString(), |
109 'message': _getErrorMessage(error), | 109 'message': _getErrorMessage(error), |
110 'stack': stack == null ? null : new Chain.forTrace(stack).toString() | 110 'stack': stack == null ? null : new Chain.forTrace(stack).toString() |
111 }; | 111 }; |
112 } | 112 } |
113 | 113 |
114 String toString() => "$message\n$stackTrace"; | 114 String toString() => "$message\n$stackTrace"; |
115 } | 115 } |
116 | 116 |
117 // Get a string description of an exception. | 117 /// A regular expression to match the exception prefix that some exceptions' |
118 // | 118 /// [Object.toString] values contain. |
119 // Most exception types have a "message" property. We prefer this since | 119 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
120 // it skips the "Exception:", "HttpException:", etc. prefix that calling | 120 |
121 // toString() adds. But, alas, "message" isn't actually defined in the | 121 /// Get a string description of an exception. |
122 // base Exception type so there's no easy way to know if it's available | 122 /// |
123 // short of a giant pile of type tests for each known exception type. | 123 /// Many exceptions include the exception class name at the beginning of their |
124 // | 124 /// [toString], so we remove that if it exists. |
125 // So just try it. If it throws, default to toString(). | 125 String _getErrorMessage(error) => |
126 String _getErrorMessage(error) { | 126 error.toString().replaceFirst(_exceptionPrefix, ''); |
127 try { | |
128 return error.message; | |
129 } on NoSuchMethodError catch (_) { | |
130 return error.toString(); | |
131 } | |
132 } | |
OLD | NEW |