| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library schedule_error; | |
| 6 | |
| 7 import 'package:stack_trace/stack_trace.dart'; | |
| 8 | |
| 9 import 'schedule.dart'; | |
| 10 import 'task.dart'; | |
| 11 import 'utils.dart'; | |
| 12 | |
| 13 /// A wrapper for errors that occur during a scheduled test. | |
| 14 class ScheduleError { | |
| 15 /// The wrapped error. | |
| 16 final error; | |
| 17 | |
| 18 /// The stack trace that was attached to the error. Can be `null`. | |
| 19 final StackTrace stackTrace; | |
| 20 | |
| 21 /// The schedule during which this error occurred. | |
| 22 final Schedule schedule; | |
| 23 | |
| 24 /// The task that was running when this error occurred. This may be `null` if | |
| 25 /// there was no such task. | |
| 26 final Task task; | |
| 27 | |
| 28 /// The task queue that was running when this error occured. This may be | |
| 29 /// `null` if there was no such queue. | |
| 30 final TaskQueue queue; | |
| 31 | |
| 32 /// The descriptions of out-of-band callbacks that were pending when this | |
| 33 /// error occurred. | |
| 34 final Iterable<PendingCallback> pendingCallbacks; | |
| 35 | |
| 36 /// The state of the schedule at the time the error was detected. | |
| 37 final ScheduleState _stateWhenDetected; | |
| 38 | |
| 39 int get hashCode => schedule.hashCode ^ task.hashCode ^ queue.hashCode ^ | |
| 40 _stateWhenDetected.hashCode ^ error.hashCode ^ stackTrace.hashCode; | |
| 41 | |
| 42 /// Creates a new [ScheduleError] wrapping [error]. The metadata in | |
| 43 /// [ScheduleError]s will be preserved. | |
| 44 factory ScheduleError.from(Schedule schedule, error, | |
| 45 {StackTrace stackTrace}) { | |
| 46 if (error is ScheduleError) return error; | |
| 47 | |
| 48 if (stackTrace == null && error is Error) { | |
| 49 // Overwrite the explicit stack trace, because it probably came from a | |
| 50 // rethrow in the first place. | |
| 51 stackTrace = error.stackTrace; | |
| 52 } | |
| 53 | |
| 54 if (stackTrace == null) stackTrace = new Chain.current(); | |
| 55 return new ScheduleError(schedule, error, stackTrace); | |
| 56 } | |
| 57 | |
| 58 ScheduleError(Schedule schedule, this.error, this.stackTrace) | |
| 59 : schedule = schedule, | |
| 60 task = schedule.currentTask, | |
| 61 queue = schedule.currentQueue, | |
| 62 pendingCallbacks = schedule.currentQueue == null ? <PendingCallback>[] | |
| 63 : schedule.currentQueue.pendingCallbacks.toList(), | |
| 64 _stateWhenDetected = schedule.state; | |
| 65 | |
| 66 bool operator ==(other) => other is ScheduleError && task == other.task && | |
| 67 queue == other.queue && _stateWhenDetected == other._stateWhenDetected && | |
| 68 error == other.error && stackTrace == other.stackTrace; | |
| 69 | |
| 70 String toString() { | |
| 71 var result = new StringBuffer(); | |
| 72 | |
| 73 var errorString = error.toString(); | |
| 74 if (errorString.contains("\n")) { | |
| 75 result.write('ScheduleError:\n'); | |
| 76 result.write(prefixLines(errorString.trim())); | |
| 77 result.write("\n\n"); | |
| 78 } else { | |
| 79 result.write('ScheduleError: "$errorString"\n'); | |
| 80 } | |
| 81 | |
| 82 if (stackTrace != null) { | |
| 83 result.write('Stack chain:\n'); | |
| 84 result.write(prefixLines(terseTraceString(stackTrace))); | |
| 85 result.write("\n\n"); | |
| 86 } | |
| 87 | |
| 88 if (task != null) { | |
| 89 result.write('Error detected during task in queue "$queue":\n'); | |
| 90 result.write(task.generateTree()); | |
| 91 } else if (_stateWhenDetected == ScheduleState.DONE) { | |
| 92 result.write('Error detected after all tasks in the schedule had ' | |
| 93 'finished.'); | |
| 94 } else if (_stateWhenDetected == ScheduleState.RUNNING) { | |
| 95 result.write('Error detected when waiting for out-of-band callbacks in ' | |
| 96 'queue "$queue".'); | |
| 97 } else { // _stateWhenDetected == ScheduleState.SET_UP | |
| 98 result.write('Error detected before the schedule started running.'); | |
| 99 } | |
| 100 | |
| 101 if (!pendingCallbacks.isEmpty) { | |
| 102 result.write("\n\n"); | |
| 103 result.writeln("Pending out-of-band callbacks:"); | |
| 104 for (var callback in pendingCallbacks) { | |
| 105 result.writeln(prefixLines(callback.toString(), firstPrefix: "* ")); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 return result.toString().trim(); | |
| 110 } | |
| 111 } | |
| OLD | NEW |