|
17 | 17 |
|
18 | 18 | package io.vertx.pgclient;
|
19 | 19 |
|
| 20 | +import io.vertx.core.json.Json; |
| 21 | + |
20 | 22 | /**
|
| 23 | + * PostgreSQL error including all <a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">fields |
| 24 | + * of the ErrorResponse message</a> of the PostgreSQL frontend/backend protocol. |
| 25 | + * |
21 | 26 | * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
|
22 | 27 | */
|
23 | 28 | public class PgException extends RuntimeException {
|
24 | 29 |
|
| 30 | + private final String errorMessage; |
25 | 31 | private final String severity;
|
26 | 32 | private final String code;
|
27 | 33 | private final String detail;
|
| 34 | + private final String hint; |
| 35 | + private final String position; |
| 36 | + private final String internalPosition; |
| 37 | + private final String internalQuery; |
| 38 | + private final String where; |
| 39 | + private final String file; |
| 40 | + private final String line; |
| 41 | + private final String routine; |
| 42 | + private final String schema; |
| 43 | + private final String table; |
| 44 | + private final String column; |
| 45 | + private final String dataType; |
| 46 | + private final String constraint; |
| 47 | + |
| 48 | + public PgException(String errorMessage, String severity, String code, String detail) { |
| 49 | + this.errorMessage = errorMessage; |
| 50 | + this.severity = severity; |
| 51 | + this.code = code; |
| 52 | + this.detail = detail; |
| 53 | + this.hint = null; |
| 54 | + this.position = null; |
| 55 | + this.internalPosition = null; |
| 56 | + this.internalQuery = null; |
| 57 | + this.where = null; |
| 58 | + this.file = null; |
| 59 | + this.line = null; |
| 60 | + this.routine = null; |
| 61 | + this.schema = null; |
| 62 | + this.table = null; |
| 63 | + this.column = null; |
| 64 | + this.dataType = null; |
| 65 | + this.constraint = null; |
| 66 | + } |
28 | 67 |
|
29 |
| - public PgException(String message, String severity, String code, String detail) { |
30 |
| - super(message); |
| 68 | + public PgException(String errorMessage, String severity, String code, String detail, String hint, String position, |
| 69 | + String internalPosition, String internalQuery, String where, String file, String line, String routine, |
| 70 | + String schema, String table, String column, String dataType, String constraint) { |
| 71 | + this.errorMessage = errorMessage; |
31 | 72 | this.severity = severity;
|
32 | 73 | this.code = code;
|
33 | 74 | this.detail = detail;
|
| 75 | + this.hint = hint; |
| 76 | + this.position = position; |
| 77 | + this.internalPosition = internalPosition; |
| 78 | + this.internalQuery = internalQuery; |
| 79 | + this.where = where; |
| 80 | + this.file = file; |
| 81 | + this.line = line; |
| 82 | + this.routine = routine; |
| 83 | + this.schema = schema; |
| 84 | + this.table = table; |
| 85 | + this.column = column; |
| 86 | + this.dataType = dataType; |
| 87 | + this.constraint = constraint; |
34 | 88 | }
|
35 | 89 |
|
| 90 | + /** |
| 91 | + * @return the primary human-readable error message |
| 92 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'M' field</a>) |
| 93 | + */ |
| 94 | + public String getErrorMessage() { |
| 95 | + // getErrorMessage() avoids name clash with RuntimeException#getMessage() |
| 96 | + return errorMessage; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return the severity: ERROR, FATAL, or PANIC, or a localized translation of one of these |
| 101 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'S' field</a>) |
| 102 | + */ |
36 | 103 | public String getSeverity() {
|
37 | 104 | return severity;
|
38 | 105 | }
|
39 | 106 |
|
| 107 | + /** |
| 108 | + * @return the SQLSTATE code for the error |
| 109 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'S' field</a>, |
| 110 | + * <a href="https://www.postgresql.org/docs/current/errcodes-appendix.html">value list</a>), |
| 111 | + * it is never localized |
| 112 | + */ |
40 | 113 | public String getCode() {
|
41 | 114 | return code;
|
42 | 115 | }
|
43 | 116 |
|
44 | 117 | /**
|
45 |
| - * @return the detail error message |
| 118 | + * @return an optional secondary error message carrying more detail about the problem |
| 119 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'D' field</a>), |
| 120 | + * a newline indicates paragraph break. |
46 | 121 | */
|
47 | 122 | public String getDetail() {
|
48 | 123 | return detail;
|
49 | 124 | }
|
| 125 | + |
| 126 | + /** |
| 127 | + * @return an optional suggestion (advice) what to do about the problem |
| 128 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'H' field</a>), |
| 129 | + * a newline indicates paragraph break. |
| 130 | + */ |
| 131 | + public String getHint() { |
| 132 | + return hint; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return a decimal ASCII integer, indicating an error cursor position as an index into the original |
| 137 | + * query string. (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'P' field</a>). |
| 138 | + * The first character has index 1, and positions are measured in characters not bytes. |
| 139 | + */ |
| 140 | + public String getPosition() { |
| 141 | + return position; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return an indication of the context in which the error occurred |
| 146 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'W' field</a>). |
| 147 | + * Presently this includes a call stack traceback of active procedural language functions and |
| 148 | + * internally-generated queries. The trace is one entry per line, most recent first. |
| 149 | + */ |
| 150 | + public String getWhere() { |
| 151 | + return where; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @return file name of the source-code location where the error was reported |
| 156 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'F' field</a>). |
| 157 | + */ |
| 158 | + public String getFile() { |
| 159 | + return file; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return line number of the source-code location where the error was reported |
| 164 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'L' field</a>). |
| 165 | + */ |
| 166 | + public String getLine() { |
| 167 | + return line; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return name of the source-code routine reporting the error |
| 172 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'R' field</a>). |
| 173 | + */ |
| 174 | + public String getRoutine() { |
| 175 | + return routine; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @return if the error was associated with a specific database object, the name of the schema containing |
| 180 | + * that object, if any |
| 181 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'s' field</a>). |
| 182 | + */ |
| 183 | + public String getSchema() { |
| 184 | + return schema; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @return if the error was associated with a specific table, the name of the table |
| 189 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'t' field</a>). |
| 190 | + */ |
| 191 | + public String getTable() { |
| 192 | + return table; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @return if the error was associated with a specific table column, the name of the column |
| 197 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'c' field</a>). |
| 198 | + */ |
| 199 | + public String getColumn() { |
| 200 | + return column; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @return if the error was associated with a specific data type, the name of the data type |
| 205 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'d' field</a>). |
| 206 | + */ |
| 207 | + public String getDataType() { |
| 208 | + return dataType; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * @return if the error was associated with a specific constraint, the name of the constraint |
| 213 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'n' field</a>). |
| 214 | + */ |
| 215 | + public String getConstraint() { |
| 216 | + return constraint; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * @return a decimal ASCII integer, indicating an error cursor position |
| 221 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'p' field</a>) |
| 222 | + * as an index into the internally generated command (see 'q' field). |
| 223 | + */ |
| 224 | + public String getInternalPosition() { |
| 225 | + return internalPosition; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @return the text of a failed internally-generated command |
| 230 | + * (<a href="https://www.postgresql.org/docs/current/protocol-error-fields.html">'q' field</a>). |
| 231 | + */ |
| 232 | + public String getInternalQuery() { |
| 233 | + return internalQuery; |
| 234 | + } |
| 235 | + |
| 236 | + private static void append(StringBuffer stringBuffer, String key, String value) { |
| 237 | + if (value != null) { |
| 238 | + stringBuffer.append(", \"").append(key).append("\": ").append(Json.encode(value)); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * A serialized JsonObject of all non-null error message fields. |
| 244 | + */ |
| 245 | + @Override |
| 246 | + public String getMessage() { |
| 247 | + StringBuffer sb = new StringBuffer(); |
| 248 | + append(sb, "message", getErrorMessage()); |
| 249 | + append(sb, "severity", getSeverity()); |
| 250 | + append(sb, "code", getCode()); |
| 251 | + append(sb, "detail", getDetail()); |
| 252 | + append(sb, "hint", getHint()); |
| 253 | + append(sb, "position", getPosition()); |
| 254 | + append(sb, "internalPosition", getInternalPosition()); |
| 255 | + append(sb, "internalQuery", getInternalQuery()); |
| 256 | + append(sb, "where", getWhere()); |
| 257 | + append(sb, "file", getFile()); |
| 258 | + append(sb, "line", getLine()); |
| 259 | + append(sb, "routine", getRoutine()); |
| 260 | + append(sb, "schema", getSchema()); |
| 261 | + append(sb, "table", getTable()); |
| 262 | + append(sb, "column", getColumn()); |
| 263 | + append(sb, "dataType", getDataType()); |
| 264 | + append(sb, "constraint", getConstraint()); |
| 265 | + if (sb.length() == 0) { |
| 266 | + return "{}"; |
| 267 | + } |
| 268 | + sb.append(" }"); |
| 269 | + sb.setCharAt(0, '{'); // replace leading comma |
| 270 | + return sb.toString(); |
| 271 | + } |
50 | 272 | }
|
0 commit comments