| 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 /** | 7 final Expando _stackTraceExpando = new Expando("asynchronous error"); |
| 8 * Error result of an asynchronous computation. | |
| 9 */ | |
| 10 class AsyncError { | |
| 11 /** The actual error thrown by the computation. */ | |
| 12 final error; | |
| 13 /** Stack trace corresponding to the error, if available. */ | |
| 14 final Object stackTrace; | |
| 15 /** Asynchronous error leading to this error, if error handling fails. */ | |
| 16 final AsyncError cause; | |
| 17 | 8 |
| 18 // TODO(lrn): When possible, combine into one constructor with both optional | 9 void _attachStackTrace(o, st) { |
| 19 // positional and named arguments. | 10 if (o == null || o is bool || o is num || o is String) return; |
| 20 AsyncError(this.error, [this.stackTrace]): cause = null; | 11 _stackTraceExpando[o] = st; |
| 21 AsyncError.withCause(this.error, this.stackTrace, this.cause); | |
| 22 | |
| 23 void _writeOn(StringSink buffer) { | |
| 24 buffer.write("'"); | |
| 25 String message; | |
| 26 try { | |
| 27 message = error.toString(); | |
| 28 } catch (e) { | |
| 29 message = Error.safeToString(error); | |
| 30 } | |
| 31 buffer.write(message); | |
| 32 buffer.write("'\n"); | |
| 33 if (stackTrace != null) { | |
| 34 buffer.write("Stack trace:\n"); | |
| 35 buffer.writeln(stackTrace.toString()); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 String toString() { | |
| 40 StringBuffer buffer = new StringBuffer(); | |
| 41 buffer.write("AsyncError: "); | |
| 42 _writeOn(buffer); | |
| 43 AsyncError cause = this.cause; | |
| 44 while (cause != null) { | |
| 45 buffer.write("Caused by: "); | |
| 46 cause._writeOn(buffer); | |
| 47 cause = cause.cause; | |
| 48 } | |
| 49 return buffer.toString(); | |
| 50 } | |
| 51 | |
| 52 throwDelayed() { | |
| 53 reportError() { | |
| 54 print("Uncaught Error: $error"); | |
| 55 if (stackTrace != null) { | |
| 56 print("Stack Trace:\n$stackTrace\n"); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 try { | |
| 61 Timer.run(() { | |
| 62 reportError(); | |
| 63 // TODO(floitsch): we potentially want to call the global error handler | |
| 64 // directly so that we can pass the stack trace. | |
| 65 throw error; | |
| 66 }); | |
| 67 } catch (e) { | |
| 68 // Unfortunately there is not much more we can do... | |
| 69 reportError(); | |
| 70 } | |
| 71 } | |
| 72 } | 12 } |
| 73 | 13 |
| 14 getAttachedStackTrace(o) { |
| 15 if (o == null || o is bool || o is num || o is String) return null; |
| 16 return _stackTraceExpando[o]; |
| 17 } |
| OLD | NEW |