Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 final Expando _stackTraceExpando = new Expando("asynchronous error"); | 7 final Expando _stackTraceExpando = new Expando("asynchronous error"); |
| 8 | 8 |
| 9 void _attachStackTrace(o, st) { | 9 void _attachStackTrace(o, st) { |
| 10 if (o == null || o is bool || o is num || o is String) return; | 10 if (o == null || o is bool || o is num || o is String) return; |
| 11 _stackTraceExpando[o] = st; | 11 _stackTraceExpando[o] = st; |
| 12 } | 12 } |
| 13 | 13 |
| 14 _invokeErrorHandler(Function errorHandler, | |
| 15 Object error, StackTrace stackTrace) { | |
| 16 if (errorHandler is ZoneBinaryCallback) { | |
| 17 return errorHandler(error, stackTrace); | |
| 18 } else { | |
| 19 return errorHandler(error); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 Function _registerErrorHandler(Function errorHandler, Zone zone) { | |
| 24 if (errorHandler is ZoneBinaryCallback) { | |
| 25 return zone.registerBinaryCallback(errorHandler); | |
| 26 } else { | |
| 27 return zone.registerUnaryCallback(errorHandler); | |
| 28 } | |
| 29 } | |
| 30 | |
| 14 /** | 31 /** |
| 15 * *This is an experimental API.* | 32 * *This is an experimental API.* |
| 16 * | 33 * |
| 17 * Get the [StackTrace] attached to [o]. | 34 * Get the [StackTrace] attached to [o]. |
| 18 * | 35 * |
| 19 * If object [o] was thrown and caught in a dart:async method, a [StackTrace] | 36 * If object [o] was thrown and caught in a dart:async method, a [StackTrace] |
| 20 * object was attached to it. Use [getAttachedStackTrace] to get that object. | 37 * object was attached to it. Use [getAttachedStackTrace] to get that object. |
| 21 * | 38 * |
| 22 * Returns [null] if no [StackTrace] was attached. | 39 * Returns [null] if no [StackTrace] was attached. |
| 23 */ | 40 */ |
| 24 getAttachedStackTrace(o) { | 41 getAttachedStackTrace(o) { |
| 25 if (o == null || o is bool || o is num || o is String) return null; | 42 if (o == null || o is bool || o is num || o is String) return null; |
| 26 return _stackTraceExpando[o]; | 43 return _stackTraceExpando[o]; |
| 27 } | 44 } |
| 45 | |
| 46 class _AsyncError implements Error { | |
| 47 final error; | |
| 48 final StackTrace stackTrace; | |
| 49 | |
| 50 _AsyncError(this.error, this.stackTrace); | |
| 51 } | |
| 52 | |
| 53 class _UncaughtAsyncError extends _AsyncError { | |
| 54 _UncaughtAsyncError(error, StackTrace stackTrace) | |
| 55 : super(error, _getBestStackTrace(error, stackTrace)) { | |
| 56 // Clear the attached stack trace. | |
| 57 _attachStackTrace(error, null); | |
| 58 } | |
| 59 | |
| 60 static StackTrace _getBestStackTrace(error, StackTrace stackTrace) { | |
| 61 if (stackTrace != null) return stackTrace; | |
| 62 var trace = getAttachedStackTrace(error); | |
| 63 if (trace != null) return trace; | |
| 64 if (error is Error) { | |
| 65 Error e = error; | |
|
Lasse Reichstein Nielsen
2013/10/07 12:39:20
Soon to be redundant cast! :)
| |
| 66 return e.stackTrace; | |
| 67 } | |
| 68 return null; | |
| 69 } | |
| 70 | |
| 71 String toString() { | |
| 72 String result = "Uncaught Error: ${error}"; | |
| 73 | |
| 74 if (stackTrace != null) { | |
| 75 result += "\nStack Trace:\n$stackTrace"; | |
| 76 } | |
| 77 return result; | |
| 78 } | |
| 79 } | |
| OLD | NEW |