Debian Codegraph @

src/main/org/h2/message/DbException.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.message;
7
8 import static org.h2.api.ErrorCode.*;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.PrintWriter;
14 import java.lang.reflect.InvocationTargetException;
15 import java.nio.charset.StandardCharsets;
16 import java.sql.DriverManager;
17 import java.sql.SQLException;
18 import java.text.MessageFormat;
19 import java.util.Locale;
20 import java.util.Map.Entry;
21 import java.util.Properties;
22
23 import org.h2.engine.Constants;
24 import org.h2.jdbc.JdbcException;
25 import org.h2.jdbc.JdbcSQLDataException;
26 import org.h2.jdbc.JdbcSQLException;
27 import org.h2.jdbc.JdbcSQLFeatureNotSupportedException;
28 import org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException;
29 import org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException;
30 import org.h2.jdbc.JdbcSQLNonTransientConnectionException;
31 import org.h2.jdbc.JdbcSQLNonTransientException;
32 import org.h2.jdbc.JdbcSQLSyntaxErrorException;
33 import org.h2.jdbc.JdbcSQLTimeoutException;
34 import org.h2.jdbc.JdbcSQLTransactionRollbackException;
35 import org.h2.jdbc.JdbcSQLTransientException;
36 import org.h2.util.SortedProperties;
37 import org.h2.util.StringUtils;
38 import org.h2.util.Utils;
39
40 /**
41 * This exception wraps a checked exception.
42 * It is used in methods where checked exceptions are not supported,
43 * for example in a Comparator.
44 */
45 public class DbException extends RuntimeException {
46
47 private static final long serialVersionUID = 1L;
48
49 /**
50 * If the SQL statement contains this text, then it is never added to the
51 * SQL exception. Hiding the SQL statement may be important if it contains a
52 * passwords, such as a CREATE LINKED TABLE statement.
53 */
54 public static final String HIDE_SQL = "--hide--";
55
56 private static final Properties MESSAGES = new Properties();
57
58 /**
59 * Thrown when OOME exception happens on handle error
60 * inside {@link #convert(java.lang.Throwable)}.
61 */
62 public static final SQLException SQL_OOME =
63 new SQLException("OutOfMemoryError", "HY000", OUT_OF_MEMORY, new OutOfMemoryError());
64 private static final DbException OOME = new DbException(SQL_OOME);
65
66 private Object source;
67
68 static {
69 try {
70 byte[] messages = Utils.getResource("/org/h2/res/_messages_en.prop");
71 if (messages != null) {
72 MESSAGES.load(new ByteArrayInputStream(messages));
73 }
74 String language = Locale.getDefault().getLanguage();
75 if (!"en".equals(language)) {
76 byte[] translations = Utils.getResource(
77 "/org/h2/res/_messages_" + language + ".prop");
78 // message: translated message + english
79 // (otherwise certain applications don't work)
80 if (translations != null) {
81 Properties p = SortedProperties.fromLines(
82 new String(translations, StandardCharsets.UTF_8));
83 for (Entry<Object, Object> e : p.entrySet()) {
84 String key = (String) e.getKey();
85 String translation = (String) e.getValue();
86 if (translation != null && !translation.startsWith("#")) {
87 String original = MESSAGES.getProperty(key);
88 String message = translation + "\n" + original;
89 MESSAGES.put(key, message);
90 }
91 }
92 }
93 }
94 } catch (OutOfMemoryError | IOException e) {
95 DbException.traceThrowable(e);
96 }
97 }
98
99 private DbException(SQLException e) {
100 super(e.getMessage(), e);
101 }
102
103 private static String translate(String key, String... params) {
104 String message = MESSAGES.getProperty(key);
105 if (message == null) {
106 message = "(Message " + key + " not found)";
107 }
108 if (params != null) {
109 for (int i = 0; i < params.length; i++) {
110 String s = params[i];
111 if (s != null && s.length() > 0) {
112 params[i] = quote(s);
113 }
114 }
115 message = MessageFormat.format(message, (Object[]) params);
116 }
117 return message;
118 }
119
120 private static String quote(String s) {
121 int l = s.length();
122 StringBuilder builder = new StringBuilder(l + 2).append('"');
123 for (int i = 0; i < l;) {
124 int cp = s.codePointAt(i);
125 i += Character.charCount(cp);
126 int t = Character.getType(cp);
127 if (t == 0 || t >= Character.SPACE_SEPARATOR && t <= Character.SURROGATE && cp != ' ') {
128 if (cp <= 0xffff) {
129 StringUtils.appendHex(builder.append('\\'), cp, 2);
130 } else {
131 StringUtils.appendHex(builder.append("\\+"), cp, 3);
132 }
133 } else {
134 if (cp == '"' || cp == '\\') {
135 builder.append((char) cp);
136 }
137 builder.appendCodePoint(cp);
138 }
139 }
140 return builder.append('"').toString();
141 }
142
143 /**
144 * Get the SQLException object.
145 *
146 * @return the exception
147 */
148 public SQLException getSQLException() {
149 return (SQLException) getCause();
150 }
151
152 /**
153 * Get the error code.
154 *
155 * @return the error code
156 */
157 public int getErrorCode() {
158 return getSQLException().getErrorCode();
159 }
160
161 /**
162 * Set the SQL statement of the given exception.
163 * This method may create a new object.
164 *
165 * @param sql the SQL statement
166 * @return the exception
167 */
168 public DbException addSQL(String sql) {
169 SQLException e = getSQLException();
170 if (e instanceof JdbcException) {
171 JdbcException j = (JdbcException) e;
172 if (j.getSQL() == null) {
173 j.setSQL(filterSQL(sql));
174 }
175 return this;
176 }
177 e = getJdbcSQLException(e.getMessage(), sql, e.getSQLState(), e.getErrorCode(), e, null);
178 return new DbException(e);
179 }
180
181 /**
182 * Create a database exception for a specific error code.
183 *
184 * @param errorCode the error code
185 * @return the exception
186 */
187 public static DbException get(int errorCode) {
188 return get(errorCode, (String) null);
189 }
190
191 /**
192 * Create a database exception for a specific error code.
193 *
194 * @param errorCode the error code
195 * @param p1 the first parameter of the message
196 * @return the exception
197 */
198 public static DbException get(int errorCode, String p1) {
199 return get(errorCode, new String[] { p1 });
200 }
201
202 /**
203 * Create a database exception for a specific error code.
204 *
205 * @param errorCode the error code
206 * @param cause the cause of the exception
207 * @param params the list of parameters of the message
208 * @return the exception
209 */
210 public static DbException get(int errorCode, Throwable cause,
211 String... params) {
212 return new DbException(getJdbcSQLException(errorCode, cause, params));
213 }
214
215 /**
216 * Create a database exception for a specific error code.
217 *
218 * @param errorCode the error code
219 * @param params the list of parameters of the message
220 * @return the exception
221 */
222 public static DbException get(int errorCode, String... params) {
223 return new DbException(getJdbcSQLException(errorCode, null, params));
224 }
225
226 /**
227 * Create a database exception for an arbitrary SQLState.
228 *
229 * @param sqlstate the state to use
230 * @param message the message to use
231 * @return the exception
232 */
233 public static DbException fromUser(String sqlstate, String message) {
234 // do not translate as sqlstate is arbitrary : avoid "message not found"
235 return new DbException(getJdbcSQLException(message, null, sqlstate, 0, null, null));
236 }
237
238 /**
239 * Create a syntax error exception.
240 *
241 * @param sql the SQL statement
242 * @param index the position of the error in the SQL statement
243 * @return the exception
244 */
245 public static DbException getSyntaxError(String sql, int index) {
246 sql = StringUtils.addAsterisk(sql, index);
247 return get(SYNTAX_ERROR_1, sql);
248 }
249
250 /**
251 * Create a syntax error exception.
252 *
253 * @param sql the SQL statement
254 * @param index the position of the error in the SQL statement
255 * @param message the message
256 * @return the exception
257 */
258 public static DbException getSyntaxError(String sql, int index,
259 String message) {
260 sql = StringUtils.addAsterisk(sql, index);
261 return new DbException(getJdbcSQLException(SYNTAX_ERROR_2, null, sql, message));
262 }
263
264 /**
265 * Create a syntax error exception for a specific error code.
266 *
267 * @param errorCode the error code
268 * @param sql the SQL statement
269 * @param index the position of the error in the SQL statement
270 * @param params the list of parameters of the message
271 * @return the exception
272 */
273 public static DbException getSyntaxError(int errorCode, String sql, int index, String... params) {
274 sql = StringUtils.addAsterisk(sql, index);
275 String sqlstate = getState(errorCode);
276 String message = translate(sqlstate, params);
277 return new DbException(getJdbcSQLException(message, sql, sqlstate, errorCode, null, null));
278 }
279
280 /**
281 * Gets a SQL exception meaning this feature is not supported.
282 *
283 * @param message what exactly is not supported
284 * @return the exception
285 */
286 public static DbException getUnsupportedException(String message) {
287 return get(FEATURE_NOT_SUPPORTED_1, message);
288 }
289
290 /**
291 * Gets a SQL exception meaning this value is invalid.
292 *
293 * @param param the name of the parameter
294 * @param value the value passed
295 * @return the exception
296 */
297 public static DbException getInvalidValueException(String param, Object value) {
298 return get(INVALID_VALUE_2, value == null ? "null" : value.toString(), param);
299 }
300
301 /**
302 * Gets a SQL exception meaning this value is invalid.
303 *
304 * @param cause the cause of the exception
305 * @param param the name of the parameter
306 * @param value the value passed
307 * @return the exception
308 */
309 public static DbException getInvalidValueException(Throwable cause, String param, Object value) {
310 return get(INVALID_VALUE_2, cause, value == null ? "null" : value.toString(), param);
311 }
312
313 /**
314 * Gets a SQL exception meaning this value is too long.
315 *
316 * @param columnOrType
317 * column with data type or data type name
318 * @param value
319 * string representation of value, will be truncated to 80
320 * characters
321 * @param valueLength
322 * the actual length of value, {@code -1L} if unknown
323 * @return the exception
324 */
325 public static DbException getValueTooLongException(String columnOrType, String value, long valueLength) {
326 int length = value.length();
327 int m = valueLength >= 0 ? 22 : 0;
328 StringBuilder builder = length > 80 //
329 ? new StringBuilder(83 + m).append(value, 0, 80).append("...")
330 : new StringBuilder(length + m).append(value);
331 if (valueLength >= 0) {
332 builder.append(" (").append(valueLength).append(')');
333 }
334 return get(VALUE_TOO_LONG_2, columnOrType, builder.toString());
335 }
336
337 /**
338 * Gets a file version exception.
339 *
340 * @param dataFileName the name of the database
341 * @return the exception
342 */
343 public static DbException getFileVersionError(String dataFileName) {
344 return DbException.get(FILE_VERSION_ERROR_1, "Old database: " + dataFileName
345 + " - please convert the database to a SQL script and re-create it.");
346 }
347
348 /**
349 * Gets an internal error.
350 *
351 * @param s the message
352 * @return the RuntimeException object
353 */
354 public static RuntimeException getInternalError(String s) {
355 RuntimeException e = new RuntimeException(s);
356 DbException.traceThrowable(e);
357 return e;
358 }
359
360 /**
361 * Gets an internal error.
362 *
363 * @return the RuntimeException object
364 */
365 public static RuntimeException getInternalError() {
366 return getInternalError("Unexpected code path");
367 }
368
369 /**
370 * Convert an exception to a SQL exception using the default mapping.
371 *
372 * @param e the root cause
373 * @return the SQL exception object
374 */
375 public static SQLException toSQLException(Throwable e) {
376 if (e instanceof SQLException) {
377 return (SQLException) e;
378 }
379 return convert(e).getSQLException();
380 }
381
382 /**
383 * Convert a throwable to an SQL exception using the default mapping. All
384 * errors except the following are re-thrown: StackOverflowError,
385 * LinkageError.
386 *
387 * @param e the root cause
388 * @return the exception object
389 */
390 public static DbException convert(Throwable e) {
391 try {
392 if (e instanceof DbException) {
393 return (DbException) e;
394 } else if (e instanceof SQLException) {
395 return new DbException((SQLException) e);
396 } else if (e instanceof InvocationTargetException) {
397 return convertInvocation((InvocationTargetException) e, null);
398 } else if (e instanceof IOException) {
399 return get(IO_EXCEPTION_1, e, e.toString());
400 } else if (e instanceof OutOfMemoryError) {
401 return get(OUT_OF_MEMORY, e);
402 } else if (e instanceof StackOverflowError || e instanceof LinkageError) {
403 return get(GENERAL_ERROR_1, e, e.toString());
404 } else if (e instanceof Error) {
405 throw (Error) e;
406 }
407 return get(GENERAL_ERROR_1, e, e.toString());
408 } catch (OutOfMemoryError ignore) {
409 return OOME;
410 } catch (Throwable ex) {
411 try {
412 DbException dbException = new DbException(
413 new SQLException("GeneralError", "HY000", GENERAL_ERROR_1, e));
414 dbException.addSuppressed(ex);
415 return dbException;
416 } catch (OutOfMemoryError ignore) {
417 return OOME;
418 }
419 }
420 }
421
422 /**
423 * Convert an InvocationTarget exception to a database exception.
424 *
425 * @param te the root cause
426 * @param message the added message or null
427 * @return the database exception object
428 */
429 public static DbException convertInvocation(InvocationTargetException te,
430 String message) {
431 Throwable t = te.getTargetException();
432 if (t instanceof SQLException || t instanceof DbException) {
433 return convert(t);
434 }
435 message = message == null ? t.getMessage() : message + ": " + t.getMessage();
436 return get(EXCEPTION_IN_FUNCTION_1, t, message);
437 }
438
439 /**
440 * Convert an IO exception to a database exception.
441 *
442 * @param e the root cause
443 * @param message the message or null
444 * @return the database exception object
445 */
446 public static DbException convertIOException(IOException e, String message) {
447 if (message == null) {
448 Throwable t = e.getCause();
449 if (t instanceof DbException) {
450 return (DbException) t;
451 }
452 return get(IO_EXCEPTION_1, e, e.toString());
453 }
454 return get(IO_EXCEPTION_2, e, e.toString(), message);
455 }
456
457 /**
458 * Gets the SQL exception object for a specific error code.
459 *
460 * @param errorCode the error code
461 * @return the SQLException object
462 */
463 public static SQLException getJdbcSQLException(int errorCode) {
464 return getJdbcSQLException(errorCode, (Throwable)null);
465 }
466
467 /**
468 * Gets the SQL exception object for a specific error code.
469 *
470 * @param errorCode the error code
471 * @param p1 the first parameter of the message
472 * @return the SQLException object
473 */
474 public static SQLException getJdbcSQLException(int errorCode, String p1) {
475 return getJdbcSQLException(errorCode, null, p1);
476 }
477
478 /**
479 * Gets the SQL exception object for a specific error code.
480 *
481 * @param errorCode the error code
482 * @param cause the cause of the exception
483 * @param params the list of parameters of the message
484 * @return the SQLException object
485 */
486 public static SQLException getJdbcSQLException(int errorCode, Throwable cause, String... params) {
487 String sqlstate = getState(errorCode);
488 String message = translate(sqlstate, params);
489 return getJdbcSQLException(message, null, sqlstate, errorCode, cause, null);
490 }
491
492 /**
493 * Creates a SQLException.
494 *
495 * @param message the reason
496 * @param sql the SQL statement
497 * @param state the SQL state
498 * @param errorCode the error code
499 * @param cause the exception that was the reason for this exception
500 * @param stackTrace the stack trace
501 * @return the SQLException object
502 */
503 public static SQLException getJdbcSQLException(String message, String sql, String state, int errorCode,
504 Throwable cause, String stackTrace) {
505 sql = filterSQL(sql);
506 // Use SQLState class value to detect type
507 switch (errorCode / 1_000) {
508 case 2:
509 return new JdbcSQLNonTransientException(message, sql, state, errorCode, cause, stackTrace);
510 case 7:
511 case 21:
512 case 42:
513 case 54:
514 return new JdbcSQLSyntaxErrorException(message, sql, state, errorCode, cause, stackTrace);
515 case 8:
516 return new JdbcSQLNonTransientConnectionException(message, sql, state, errorCode, cause, stackTrace);
517 case 22:
518 return new JdbcSQLDataException(message, sql, state, errorCode, cause, stackTrace);
519 case 23:
520 return new JdbcSQLIntegrityConstraintViolationException(message, sql, state, errorCode, cause, stackTrace);
521 case 28:
522 return new JdbcSQLInvalidAuthorizationSpecException(message, sql, state, errorCode, cause, stackTrace);
523 case 40:
524 return new JdbcSQLTransactionRollbackException(message, sql, state, errorCode, cause, stackTrace);
525 }
526 // Check error code
527 switch (errorCode){
528 case GENERAL_ERROR_1:
529 case UNKNOWN_DATA_TYPE_1:
530 case METHOD_NOT_ALLOWED_FOR_QUERY:
531 case METHOD_ONLY_ALLOWED_FOR_QUERY:
532 case SEQUENCE_EXHAUSTED:
533 case OBJECT_CLOSED:
534 case CANNOT_DROP_CURRENT_USER:
535 case UNSUPPORTED_SETTING_COMBINATION:
536 case FILE_RENAME_FAILED_2:
537 case FILE_DELETE_FAILED_1:
538 case IO_EXCEPTION_1:
539 case NOT_ON_UPDATABLE_ROW:
540 case IO_EXCEPTION_2:
541 case TRACE_FILE_ERROR_2:
542 case ADMIN_RIGHTS_REQUIRED:
543 case ERROR_EXECUTING_TRIGGER_3:
544 case COMMIT_ROLLBACK_NOT_ALLOWED:
545 case FILE_CREATION_FAILED_1:
546 case SAVEPOINT_IS_INVALID_1:
547 case SAVEPOINT_IS_UNNAMED:
548 case SAVEPOINT_IS_NAMED:
549 case NOT_ENOUGH_RIGHTS_FOR_1:
550 case DATABASE_IS_READ_ONLY:
551 case WRONG_XID_FORMAT_1:
552 case UNSUPPORTED_COMPRESSION_OPTIONS_1:
553 case UNSUPPORTED_COMPRESSION_ALGORITHM_1:
554 case COMPRESSION_ERROR:
555 case EXCEPTION_IN_FUNCTION_1:
556 case ERROR_ACCESSING_LINKED_TABLE_2:
557 case FILE_NOT_FOUND_1:
558 case INVALID_CLASS_2:
559 case DATABASE_IS_NOT_PERSISTENT:
560 case RESULT_SET_NOT_UPDATABLE:
561 case RESULT_SET_NOT_SCROLLABLE:
562 case METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT:
563 case ACCESS_DENIED_TO_CLASS_1:
564 case RESULT_SET_READONLY:
565 case CURRENT_SEQUENCE_VALUE_IS_NOT_DEFINED_IN_SESSION_1:
566 return new JdbcSQLNonTransientException(message, sql, state, errorCode, cause, stackTrace);
567 case FEATURE_NOT_SUPPORTED_1:
568 return new JdbcSQLFeatureNotSupportedException(message, sql, state, errorCode, cause, stackTrace);
569 case LOCK_TIMEOUT_1:
570 case STATEMENT_WAS_CANCELED:
571 case LOB_CLOSED_ON_TIMEOUT_1:
572 return new JdbcSQLTimeoutException(message, sql, state, errorCode, cause, stackTrace);
573 case FUNCTION_MUST_RETURN_RESULT_SET_1:
574 case INVALID_TRIGGER_FLAGS_1:
575 case SUM_OR_AVG_ON_WRONG_DATATYPE_1:
576 case MUST_GROUP_BY_COLUMN_1:
577 case SECOND_PRIMARY_KEY:
578 case FUNCTION_NOT_FOUND_1:
579 case COLUMN_MUST_NOT_BE_NULLABLE_1:
580 case USER_NOT_FOUND_1:
581 case USER_ALREADY_EXISTS_1:
582 case SEQUENCE_ALREADY_EXISTS_1:
583 case SEQUENCE_NOT_FOUND_1:
584 case VIEW_NOT_FOUND_1:
585 case VIEW_ALREADY_EXISTS_1:
586 case TRIGGER_ALREADY_EXISTS_1:
587 case TRIGGER_NOT_FOUND_1:
588 case ERROR_CREATING_TRIGGER_OBJECT_3:
589 case CONSTRAINT_ALREADY_EXISTS_1:
590 case SUBQUERY_IS_NOT_SINGLE_COLUMN:
591 case INVALID_USE_OF_AGGREGATE_FUNCTION_1:
592 case CONSTRAINT_NOT_FOUND_1:
593 case AMBIGUOUS_COLUMN_NAME_1:
594 case ORDER_BY_NOT_IN_RESULT:
595 case ROLE_ALREADY_EXISTS_1:
596 case ROLE_NOT_FOUND_1:
597 case USER_OR_ROLE_NOT_FOUND_1:
598 case ROLES_AND_RIGHT_CANNOT_BE_MIXED:
599 case METHODS_MUST_HAVE_DIFFERENT_PARAMETER_COUNTS_2:
600 case ROLE_ALREADY_GRANTED_1:
601 case COLUMN_IS_PART_OF_INDEX_1:
602 case FUNCTION_ALIAS_ALREADY_EXISTS_1:
603 case FUNCTION_ALIAS_NOT_FOUND_1:
604 case SCHEMA_ALREADY_EXISTS_1:
605 case SCHEMA_NOT_FOUND_1:
606 case SCHEMA_NAME_MUST_MATCH:
607 case COLUMN_CONTAINS_NULL_VALUES_1:
608 case SEQUENCE_BELONGS_TO_A_TABLE_1:
609 case COLUMN_IS_REFERENCED_1:
610 case CANNOT_DROP_LAST_COLUMN:
611 case INDEX_BELONGS_TO_CONSTRAINT_2:
612 case CLASS_NOT_FOUND_1:
613 case METHOD_NOT_FOUND_1:
614 case COLLATION_CHANGE_WITH_DATA_TABLE_1:
615 case SCHEMA_CAN_NOT_BE_DROPPED_1:
616 case ROLE_CAN_NOT_BE_DROPPED_1:
617 case CANNOT_TRUNCATE_1:
618 case CANNOT_DROP_2:
619 case VIEW_IS_INVALID_2:
620 case TYPES_ARE_NOT_COMPARABLE_2:
621 case CONSTANT_ALREADY_EXISTS_1:
622 case CONSTANT_NOT_FOUND_1:
623 case LITERALS_ARE_NOT_ALLOWED:
624 case CANNOT_DROP_TABLE_1:
625 case DOMAIN_ALREADY_EXISTS_1:
626 case DOMAIN_NOT_FOUND_1:
627 case WITH_TIES_WITHOUT_ORDER_BY:
628 case CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS:
629 case TRANSACTION_NOT_FOUND_1:
630 case AGGREGATE_NOT_FOUND_1:
631 case WINDOW_NOT_FOUND_1:
632 case CAN_ONLY_ASSIGN_TO_VARIABLE_1:
633 case PUBLIC_STATIC_JAVA_METHOD_NOT_FOUND_1:
634 case JAVA_OBJECT_SERIALIZER_CHANGE_WITH_DATA_TABLE:
635 case FOR_UPDATE_IS_NOT_ALLOWED_IN_DISTINCT_OR_GROUPED_SELECT:
636 case INVALID_VALUE_PRECISION:
637 case INVALID_VALUE_SCALE:
638 case CONSTRAINT_IS_USED_BY_CONSTRAINT_2:
639 case UNCOMPARABLE_REFERENCED_COLUMN_2:
640 case GENERATED_COLUMN_CANNOT_BE_ASSIGNED_1:
641 case GENERATED_COLUMN_CANNOT_BE_UPDATABLE_BY_CONSTRAINT_2:
642 case COLUMN_ALIAS_IS_NOT_SPECIFIED_1:
643 case GROUP_BY_NOT_IN_THE_RESULT:
644 return new JdbcSQLSyntaxErrorException(message, sql, state, errorCode, cause, stackTrace);
645 case HEX_STRING_ODD_1:
646 case HEX_STRING_WRONG_1:
647 case INVALID_VALUE_2:
648 case SEQUENCE_ATTRIBUTES_INVALID_7:
649 case INVALID_TO_CHAR_FORMAT:
650 case PARAMETER_NOT_SET_1:
651 case PARSE_ERROR_1:
652 case INVALID_TO_DATE_FORMAT:
653 case STRING_FORMAT_ERROR_1:
654 case SERIALIZATION_FAILED_1:
655 case DESERIALIZATION_FAILED_1:
656 case SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW:
657 case STEP_SIZE_MUST_NOT_BE_ZERO:
658 return new JdbcSQLDataException(message, sql, state, errorCode, cause, stackTrace);
659 case URL_RELATIVE_TO_CWD:
660 case DATABASE_NOT_FOUND_1:
661 case DATABASE_NOT_FOUND_WITH_IF_EXISTS_1:
662 case REMOTE_DATABASE_NOT_FOUND_1:
663 case TRACE_CONNECTION_NOT_CLOSED:
664 case DATABASE_ALREADY_OPEN_1:
665 case FILE_CORRUPTED_1:
666 case URL_FORMAT_ERROR_2:
667 case DRIVER_VERSION_ERROR_2:
668 case FILE_VERSION_ERROR_1:
669 case FILE_ENCRYPTION_ERROR_1:
670 case WRONG_PASSWORD_FORMAT:
671 case UNSUPPORTED_CIPHER:
672 case UNSUPPORTED_LOCK_METHOD_1:
673 case EXCEPTION_OPENING_PORT_2:
674 case DUPLICATE_PROPERTY_1:
675 case CONNECTION_BROKEN_1:
676 case UNKNOWN_MODE_1:
677 case CLUSTER_ERROR_DATABASE_RUNS_ALONE:
678 case CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1:
679 case DATABASE_IS_CLOSED:
680 case ERROR_SETTING_DATABASE_EVENT_LISTENER_2:
681 case OUT_OF_MEMORY:
682 case UNSUPPORTED_SETTING_1:
683 case REMOTE_CONNECTION_NOT_ALLOWED:
684 case DATABASE_CALLED_AT_SHUTDOWN:
685 case CANNOT_CHANGE_SETTING_WHEN_OPEN_1:
686 case DATABASE_IS_IN_EXCLUSIVE_MODE:
687 case INVALID_DATABASE_NAME_1:
688 case AUTHENTICATOR_NOT_AVAILABLE:
689 case METHOD_DISABLED_ON_AUTOCOMMIT_TRUE:
690 return new JdbcSQLNonTransientConnectionException(message, sql, state, errorCode, cause, stackTrace);
691 case ROW_NOT_FOUND_WHEN_DELETING_1:
692 case CONCURRENT_UPDATE_1:
693 case ROW_NOT_FOUND_IN_PRIMARY_INDEX:
694 return new JdbcSQLTransientException(message, sql, state, errorCode, cause, stackTrace);
695 }
696 // Default
697 return new JdbcSQLException(message, sql, state, errorCode, cause, stackTrace);
698 }
699
700 private static String filterSQL(String sql) {
701 return sql == null || !sql.contains(HIDE_SQL) ? sql : "-";
702 }
703
704 /**
705 * Builds message for an exception.
706 *
707 * @param e exception
708 * @return message
709 */
710 public static String buildMessageForException(JdbcException e) {
711 String s = e.getOriginalMessage();
712 StringBuilder buff = new StringBuilder(s != null ? s : "- ");
713 s = e.getSQL();
714 if (s != null) {
715 buff.append("; SQL statement:\n").append(s);
716 }
717 buff.append(" [").append(e.getErrorCode()).append('-').append(Constants.BUILD_ID).append(']');
718 return buff.toString();
719 }
720
721 /**
722 * Prints up to 100 next exceptions for a specified SQL exception.
723 *
724 * @param e SQL exception
725 * @param s print writer
726 */
727 public static void printNextExceptions(SQLException e, PrintWriter s) {
728 // getNextException().printStackTrace(s) would be very slow
729 // if many exceptions are joined
730 int i = 0;
731 while ((e = e.getNextException()) != null) {
732 if (i++ == 100) {
733 s.println("(truncated)");
734 return;
735 }
736 s.println(e.toString());
737 }
738 }
739
740 /**
741 * Prints up to 100 next exceptions for a specified SQL exception.
742 *
743 * @param e SQL exception
744 * @param s print stream
745 */
746 public static void printNextExceptions(SQLException e, PrintStream s) {
747 // getNextException().printStackTrace(s) would be very slow
748 // if many exceptions are joined
749 int i = 0;
750 while ((e = e.getNextException()) != null) {
751 if (i++ == 100) {
752 s.println("(truncated)");
753 return;
754 }
755 s.println(e.toString());
756 }
757 }
758
759 public Object getSource() {
760 return source;
761 }
762
763 public void setSource(Object source) {
764 this.source = source;
765 }
766
767 /**
768 * Write the exception to the driver manager log writer if configured.
769 *
770 * @param e the exception
771 */
772 public static void traceThrowable(Throwable e) {
773 PrintWriter writer = DriverManager.getLogWriter();
774 if (writer != null) {
775 e.printStackTrace(writer);
776 }
777 }
778
779 }