| 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 schedule_error; | 5 library schedule_error; |
| 6 | 6 |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:stack_trace/stack_trace.dart'; | 7 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 8 |
| 11 import 'schedule.dart'; | 9 import 'schedule.dart'; |
| 12 import 'task.dart'; | 10 import 'task.dart'; |
| 13 import 'utils.dart'; | 11 import 'utils.dart'; |
| 14 | 12 |
| 15 /// A wrapper for errors that occur during a scheduled test. | 13 /// A wrapper for errors that occur during a scheduled test. |
| 16 class ScheduleError { | 14 class ScheduleError { |
| 17 /// The wrapped error. | 15 /// The wrapped error. |
| 18 final error; | 16 final error; |
| 19 | 17 |
| 20 /// The stack trace that was attached to the error. Can be `null`. | 18 /// The stack trace that was attached to the error. Can be `null`. |
| 21 final stackTrace; | 19 final StackTrace stackTrace; |
| 22 | 20 |
| 23 /// The schedule during which this error occurred. | 21 /// The schedule during which this error occurred. |
| 24 final Schedule schedule; | 22 final Schedule schedule; |
| 25 | 23 |
| 26 /// The task that was running when this error occurred. This may be `null` if | 24 /// The task that was running when this error occurred. This may be `null` if |
| 27 /// there was no such task. | 25 /// there was no such task. |
| 28 final Task task; | 26 final Task task; |
| 29 | 27 |
| 30 /// The task queue that was running when this error occured. This may be | 28 /// The task queue that was running when this error occured. This may be |
| 31 /// `null` if there was no such queue. | 29 /// `null` if there was no such queue. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 46 factory ScheduleError.from(Schedule schedule, error, | 44 factory ScheduleError.from(Schedule schedule, error, |
| 47 {StackTrace stackTrace}) { | 45 {StackTrace stackTrace}) { |
| 48 if (error is ScheduleError) return error; | 46 if (error is ScheduleError) return error; |
| 49 | 47 |
| 50 if (stackTrace == null && error is Error) { | 48 if (stackTrace == null && error is Error) { |
| 51 // Overwrite the explicit stack trace, because it probably came from a | 49 // Overwrite the explicit stack trace, because it probably came from a |
| 52 // rethrow in the first place. | 50 // rethrow in the first place. |
| 53 stackTrace = error.stackTrace; | 51 stackTrace = error.stackTrace; |
| 54 } | 52 } |
| 55 | 53 |
| 56 if (stackTrace == null) stackTrace = new Trace.current(); | 54 if (stackTrace == null) stackTrace = new Chain.current(); |
| 57 return new ScheduleError(schedule, error, stackTrace); | 55 return new ScheduleError(schedule, error, stackTrace); |
| 58 } | 56 } |
| 59 | 57 |
| 60 // TODO(floitsch): restore StackTrace type when it has been integrated into | 58 ScheduleError(Schedule schedule, this.error, this.stackTrace) |
| 61 // the core libraries. | 59 : schedule = schedule, |
| 62 ScheduleError(Schedule schedule, error, var stackTrace) | |
| 63 : error = error, | |
| 64 stackTrace = stackTrace, | |
| 65 schedule = schedule, | |
| 66 task = schedule.currentTask, | 60 task = schedule.currentTask, |
| 67 queue = schedule.currentQueue, | 61 queue = schedule.currentQueue, |
| 68 pendingCallbacks = schedule.currentQueue == null ? <PendingCallback>[] | 62 pendingCallbacks = schedule.currentQueue == null ? <PendingCallback>[] |
| 69 : schedule.currentQueue.pendingCallbacks.toList(), | 63 : schedule.currentQueue.pendingCallbacks.toList(), |
| 70 _stateWhenDetected = schedule.state; | 64 _stateWhenDetected = schedule.state; |
| 71 | 65 |
| 72 bool operator ==(other) => other is ScheduleError && task == other.task && | 66 bool operator ==(other) => other is ScheduleError && task == other.task && |
| 73 queue == other.queue && _stateWhenDetected == other._stateWhenDetected && | 67 queue == other.queue && _stateWhenDetected == other._stateWhenDetected && |
| 74 error == other.error && stackTrace == other.stackTrace; | 68 error == other.error && stackTrace == other.stackTrace; |
| 75 | 69 |
| 76 String toString() { | 70 String toString() { |
| 77 var result = new StringBuffer(); | 71 var result = new StringBuffer(); |
| 78 | 72 |
| 79 var errorString = error.toString(); | 73 var errorString = error.toString(); |
| 80 if (errorString.contains("\n")) { | 74 if (errorString.contains("\n")) { |
| 81 result.write('ScheduleError:\n'); | 75 result.write('ScheduleError:\n'); |
| 82 result.write(prefixLines(errorString.trim())); | 76 result.write(prefixLines(errorString.trim())); |
| 83 result.write("\n\n"); | 77 result.write("\n\n"); |
| 84 } else { | 78 } else { |
| 85 result.write('ScheduleError: "$errorString"\n'); | 79 result.write('ScheduleError: "$errorString"\n'); |
| 86 } | 80 } |
| 87 | 81 |
| 88 if (stackTrace != null) { | 82 if (stackTrace != null) { |
| 89 result.write('Stack trace:\n'); | 83 result.write('Stack chain:\n'); |
| 90 result.write(prefixLines(terseTraceString(stackTrace))); | 84 result.write(prefixLines(terseTraceString(stackTrace))); |
| 91 result.write("\n\n"); | 85 result.write("\n\n"); |
| 92 } | 86 } |
| 93 | 87 |
| 94 if (task != null) { | 88 if (task != null) { |
| 95 result.write('Error detected during task in queue "$queue":\n'); | 89 result.write('Error detected during task in queue "$queue":\n'); |
| 96 result.write(task.generateTree()); | 90 result.write(task.generateTree()); |
| 97 } else if (_stateWhenDetected == ScheduleState.DONE) { | 91 } else if (_stateWhenDetected == ScheduleState.DONE) { |
| 98 result.write('Error detected after all tasks in the schedule had ' | 92 result.write('Error detected after all tasks in the schedule had ' |
| 99 'finished.'); | 93 'finished.'); |
| 100 } else if (_stateWhenDetected == ScheduleState.RUNNING) { | 94 } else if (_stateWhenDetected == ScheduleState.RUNNING) { |
| 101 result.write('Error detected when waiting for out-of-band callbacks in ' | 95 result.write('Error detected when waiting for out-of-band callbacks in ' |
| 102 'queue "$queue".'); | 96 'queue "$queue".'); |
| 103 } else { // _stateWhenDetected == ScheduleState.SET_UP | 97 } else { // _stateWhenDetected == ScheduleState.SET_UP |
| 104 result.write('Error detected before the schedule started running.'); | 98 result.write('Error detected before the schedule started running.'); |
| 105 } | 99 } |
| 106 | 100 |
| 107 if (!pendingCallbacks.isEmpty) { | 101 if (!pendingCallbacks.isEmpty) { |
| 108 result.write("\n\n"); | 102 result.write("\n\n"); |
| 109 result.writeln("Pending out-of-band callbacks:"); | 103 result.writeln("Pending out-of-band callbacks:"); |
| 110 for (var callback in pendingCallbacks) { | 104 for (var callback in pendingCallbacks) { |
| 111 result.writeln(prefixLines(callback.toString(), firstPrefix: "* ")); | 105 result.writeln(prefixLines(callback.toString(), firstPrefix: "* ")); |
| 112 } | 106 } |
| 113 } | 107 } |
| 114 | 108 |
| 115 return result.toString().trim(); | 109 return result.toString().trim(); |
| 116 } | 110 } |
| 117 } | 111 } |
| OLD | NEW |