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.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 // Exceptions are thrown either by the VM or from Dart code. | 7 // Exceptions are thrown either by the VM or from Dart code. |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * A marker interface implemented by all core library exceptions. | 10 * A marker interface implemented by all core library exceptions. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Exception thrown when a string or some other data does not have an expected | 39 * Exception thrown when a string or some other data does not have an expected |
| 40 * format and cannot be parsed or processed. | 40 * format and cannot be parsed or processed. |
| 41 */ | 41 */ |
| 42 class FormatException implements Exception { | 42 class FormatException implements Exception { |
| 43 /** | 43 /** |
| 44 * A message describing the format error. | 44 * A message describing the format error. |
| 45 */ | 45 */ |
| 46 final String message; | 46 final String message; |
| 47 /** | |
| 48 * The source that caused the error. | |
|
nweiz
2014/07/14 19:33:50
The documentation and name doesn't make it clear a
Lasse Reichstein Nielsen
2014/07/15 08:55:59
I'll try to make it more explicit, including the w
| |
| 49 * | |
| 50 * This is usually a [String], but can be other types too. If it is a string, | |
| 51 * parts of it may be included in the [toString] message. | |
| 52 * | |
| 53 * May also be `null` if omitted. | |
| 54 */ | |
| 55 final source; | |
|
nweiz
2014/07/14 19:33:49
It would be a lot more useful to consumers of this
Lasse Reichstein Nielsen
2014/07/15 08:55:59
I don't want to mandate a type. It will almost alw
| |
| 56 /** | |
| 57 * The position in source where the error was detected. | |
|
nweiz
2014/07/14 19:33:49
Please be clearer about what you mean by "position
Lasse Reichstein Nielsen
2014/07/15 08:55:59
I like "offset", so I changed it everywhere.
Also
| |
| 58 * | |
| 59 * May be omitted. If present, [source] should also be present. | |
| 60 */ | |
| 61 final int position; | |
| 47 | 62 |
| 48 /** | 63 /** |
| 49 * Creates a new FormatException with an optional error [message]. | 64 * Creates a new FormatException with an optional error [message]. |
| 65 * | |
| 66 * Optionally also supply the [source] that had the incorrect format, and | |
| 67 * even the [position] in the format where this was detected. | |
| 50 */ | 68 */ |
| 51 const FormatException([this.message = ""]); | 69 const FormatException([this.message = "", this.source, this.position]); |
| 52 | 70 |
| 53 String toString() => "FormatException: $message"; | 71 /** |
| 72 * Returns a description of the format exception. | |
| 73 * | |
| 74 * The description always contains the [message]. | |
| 75 * If [source] was provided, the description will contain (at least a part of) | |
| 76 * the source. | |
| 77 * If [position] is also provided, the part of the source included will | |
| 78 * contain that position, and the position will be marked. | |
| 79 * | |
| 80 * If the source contains a line break before position, only the line | |
| 81 * containing position will be included, and its line number will also be | |
| 82 * part of the description. Line and character offsets are 1-based. | |
| 83 */ | |
| 84 String toString() { | |
| 85 String report = "FormatException"; | |
| 86 if (message != null && message.isNotEmpty) { | |
| 87 report = "$report: $message"; | |
| 88 } | |
| 89 int position = this.position; | |
| 90 if (source is! String) { | |
| 91 if (position != null) { | |
| 92 report += " (at position $position)"; | |
| 93 } | |
| 94 return report; | |
| 95 } | |
| 96 if (position != null && (position < 0 || position > source.length)) { | |
| 97 position = null; | |
| 98 } | |
| 99 // Source is string and position is null or valid. | |
| 100 if (position == null) { | |
| 101 String source = this.source; | |
| 102 if (source.length > 78) { | |
| 103 source = source.substring(0, 75) + "..."; | |
| 104 } | |
| 105 return "$report\n$source"; | |
| 106 } | |
| 107 int lineNum = 1; | |
| 108 int lineStart = 0; | |
| 109 bool lastWasCR; | |
| 110 for (int i = 0; i < position; i++) { | |
| 111 int char = source.codeUnitAt(i); | |
| 112 if (char == 0x0a) { | |
| 113 if (lineStart != i || !lastWasCR) { | |
| 114 lineNum++; | |
| 115 } | |
| 116 lineStart = i + 1; | |
| 117 lastWasCR = false; | |
| 118 } else if (char == 0x0d) { | |
| 119 lineNum++; | |
| 120 lineStart = i + 1; | |
| 121 lastWasCR = true; | |
| 122 } | |
| 123 } | |
| 124 if (lineNum > 1) { | |
| 125 report += " (at line $lineNum, character ${position - lineStart + 1})\n"; | |
| 126 } else { | |
| 127 report += " (at character ${position + 1})\n"; | |
| 128 } | |
| 129 int lineEnd = source.length; | |
| 130 for (int i = position; i < source.length; i++) { | |
| 131 int char = source.codeUnitAt(i); | |
| 132 if (char == 0x0a || char == 0x0d) { | |
| 133 lineEnd = i; | |
| 134 break; | |
| 135 } | |
| 136 } | |
| 137 int length = lineEnd - lineStart; | |
| 138 int start = lineStart; | |
| 139 int end = lineEnd; | |
| 140 String prefix = ""; | |
| 141 String postfix = ""; | |
| 142 if (length > 78) { | |
| 143 // Can't show entire line. Try to anchor at the nearest end, if | |
| 144 // one is within reach. | |
| 145 int index = position - lineStart; | |
| 146 if (index < 75) { | |
| 147 end = start + 75; | |
| 148 postfix = "..."; | |
| 149 } else if (end - position < 75) { | |
| 150 start = end - 75; | |
| 151 prefix = "..."; | |
| 152 } else { | |
| 153 // Neither end is near, just pick an area around the position. | |
| 154 start = position - 36; | |
| 155 end = position + 36; | |
| 156 prefix = postfix = "..."; | |
| 157 } | |
| 158 } | |
| 159 String slice = source.substring(start, end); | |
| 160 int markOffset = position - start + prefix.length; | |
| 161 return "$report$prefix$slice$postfix\n${" " * markOffset}^\n"; | |
| 162 } | |
| 54 } | 163 } |
| 55 | 164 |
| 56 class IntegerDivisionByZeroException implements Exception { | 165 class IntegerDivisionByZeroException implements Exception { |
| 57 const IntegerDivisionByZeroException(); | 166 const IntegerDivisionByZeroException(); |
| 58 String toString() => "IntegerDivisionByZeroException"; | 167 String toString() => "IntegerDivisionByZeroException"; |
| 59 } | 168 } |
| OLD | NEW |