Unverified 提交 0ff4499b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1409 from katzyn/exception

Map all remaining error codes to custom exception classes
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>PR #1409: Map all remaining error codes to custom exception classes
</li>
<li>Issue #1407: Add a MODE() aggregate function
</li>
<li>PR #1402: Duplicate conditions in column check constraint
......
......@@ -533,7 +533,6 @@ See also <a href="build.html#providing_patches">Providing Patches</a>.
</li><li>Long running transactions: log session id when detected.
</li><li>Optimization: "select id from test" should use the index on id even without "order by".
</li><li>Sybase SQL Anywhere compatibility: SELECT TOP ... START AT ...
</li><li>Use Java 6 SQLException subclasses for more kinds of errors.
</li><li>Issue 390: RUNSCRIPT FROM '...' CONTINUE_ON_ERROR
</li></ul>
......
......@@ -1926,16 +1926,6 @@ public class ErrorCode {
*/
public static final int DATABASE_IS_IN_EXCLUSIVE_MODE = 90135;
/**
* The error with code <code>90136</code> is thrown when
* executing a query that used an unsupported outer join condition.
* Example:
* <pre>
* SELECT * FROM DUAL A LEFT JOIN DUAL B ON B.X=(SELECT MAX(X) FROM DUAL);
* </pre>
*/
public static final int UNSUPPORTED_OUTER_JOIN_CONDITION_1 = 90136;
/**
* The error with code <code>90137</code> is thrown when
* trying to assign a value to something that is not a variable.
......@@ -2019,7 +2009,7 @@ public class ErrorCode {
public static final int AUTHENTICATOR_NOT_AVAILABLE = 90144;
// next is 90145
// next are 90136, 90145
private ErrorCode() {
// utility class
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLNonTransientException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLNonTransientException extends SQLNonTransientException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLNonTransientException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLNonTransientException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLTimeoutException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLTimeoutException extends SQLTimeoutException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLTimeoutException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLTimeoutException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.SQLTransientException;
import org.h2.message.DbException;
/**
* Represents a database exception.
*/
public class JdbcSQLTransientException extends SQLTransientException implements JdbcException {
private static final long serialVersionUID = 1L;
private final String originalMessage;
private final String stackTrace;
private String message;
private String sql;
/**
* Creates a SQLTransientException.
*
* @param message the reason
* @param sql the SQL statement
* @param state the SQL state
* @param errorCode the error code
* @param cause the exception that was the reason for this exception
* @param stackTrace the stack trace
*/
public JdbcSQLTransientException(String message, String sql, String state,
int errorCode, Throwable cause, String stackTrace) {
super(message, state, errorCode);
this.originalMessage = message;
this.stackTrace = stackTrace;
// setSQL() also generates message
setSQL(sql);
initCause(cause);
}
@Override
public String getMessage() {
return message;
}
@Override
public String getOriginalMessage() {
return originalMessage;
}
@Override
public void printStackTrace(PrintWriter s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public void printStackTrace(PrintStream s) {
super.printStackTrace(s);
DbException.printNextExceptions(this, s);
}
@Override
public String getSQL() {
return sql;
}
@Override
public void setSQL(String sql) {
this.sql = sql;
message = DbException.buildMessageForException(this);
}
@Override
public String toString() {
if (stackTrace == null) {
return super.toString();
}
return stackTrace;
}
}
......@@ -180,6 +180,7 @@ import org.h2.test.unit.TestDataPage;
import org.h2.test.unit.TestDate;
import org.h2.test.unit.TestDateIso8601;
import org.h2.test.unit.TestDateTimeUtils;
import org.h2.test.unit.TestDbException;
import org.h2.test.unit.TestExit;
import org.h2.test.unit.TestFile;
import org.h2.test.unit.TestFileLock;
......@@ -956,6 +957,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest(new TestClearReferences());
addTest(new TestDataPage());
addTest(new TestDateIso8601());
addTest(new TestDbException());
addTest(new TestFile());
addTest(new TestFtp());
addTest(new TestInterval());
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.unit;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.SQLException;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcException;
import org.h2.jdbc.JdbcSQLException;
import org.h2.message.DbException;
import org.h2.test.TestBase;
/**
* Tests DbException class.
*/
public class TestDbException extends TestBase {
/**
* Run just this test.
*
* @param a
* ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
testGetJdbcSQLException();
}
private void testGetJdbcSQLException() throws Exception {
for (Field field : ErrorCode.class.getDeclaredFields()) {
if (field.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)) {
int errorCode = field.getInt(null);
SQLException exception = DbException.getJdbcSQLException(errorCode);
if (exception instanceof JdbcSQLException) {
fail("Custom exception expected for " + ErrorCode.class.getName() + '.' + field.getName() + " ("
+ errorCode + ')');
}
if (!(exception instanceof JdbcException)) {
fail("Custom exception for " + ErrorCode.class.getName() + '.' + field.getName() + " (" + errorCode
+ ") should implement JdbcException");
}
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论