Skip to content

Commit b07c28e

Browse files
committed
Externalize some exception messages and states
1 parent e939d5b commit b07c28e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+216
-136
lines changed

jaybird-native/src/main/java/org/firebirdsql/gds/impl/jni/EmbeddedGDSFactoryPlugin.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.firebirdsql.gds.ng.jna.FbEmbeddedDatabaseFactory;
2323

2424
import java.sql.SQLException;
25-
import java.sql.SQLNonTransientConnectionException;
2625
import java.util.List;
2726

2827
public final class EmbeddedGDSFactoryPlugin extends BaseGDSFactoryPlugin {
@@ -72,10 +71,7 @@ public String getDefaultProtocol() {
7271

7372
@Override
7473
public String getDatabasePath(String server, Integer port, String path) throws SQLException {
75-
if (path == null) {
76-
throw new SQLNonTransientConnectionException("Database name/path is required");
77-
}
78-
74+
requirePath(path);
7975
return path;
8076
}
8177

jaybird-native/src/main/java/org/firebirdsql/gds/impl/jni/NativeGDSFactoryPlugin.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.firebirdsql.gds.ng.jna.FbClientDatabaseFactory;
2323

2424
import java.sql.SQLException;
25-
import java.sql.SQLNonTransientConnectionException;
2625
import java.util.List;
2726

2827
public final class NativeGDSFactoryPlugin extends BaseGDSFactoryPlugin {
@@ -77,10 +76,7 @@ public String getDefaultProtocol() {
7776

7877
@Override
7978
public String getDatabasePath(String server, Integer port, String path) throws SQLException {
80-
if (path == null) {
81-
throw new SQLNonTransientConnectionException("Database name/path is required");
82-
}
83-
79+
requirePath(path);
8480
if (server == null) {
8581
return path;
8682
}

jaybird-native/src/main/java/org/firebirdsql/gds/ng/jna/JnaBlob.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@
2323
import com.sun.jna.ptr.ShortByReference;
2424
import org.firebirdsql.gds.BlobParameterBuffer;
2525
import org.firebirdsql.gds.ISCConstants;
26+
import org.firebirdsql.gds.JaybirdErrorCodes;
2627
import org.firebirdsql.gds.ng.AbstractFbBlob;
2728
import org.firebirdsql.gds.ng.FbBlob;
2829
import org.firebirdsql.gds.ng.FbExceptionBuilder;
2930
import org.firebirdsql.gds.ng.LockCloseable;
3031
import org.firebirdsql.gds.ng.listeners.DatabaseListener;
3132
import org.firebirdsql.jaybird.util.ByteArrayHelper;
32-
import org.firebirdsql.jdbc.SQLStateConstants;
3333
import org.firebirdsql.jna.fbclient.FbClientLibrary;
3434
import org.firebirdsql.jna.fbclient.ISC_STATUS;
3535

3636
import java.nio.ByteBuffer;
3737
import java.sql.SQLException;
38-
import java.sql.SQLNonTransientException;
3938

4039
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_blobGetSegmentNegative;
4140
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_blobPutSegmentEmpty;
@@ -203,9 +202,9 @@ protected int get(final byte[] b, final int off, final int len, final int minLen
203202
validateBufferLength(b, off, len);
204203
if (len == 0) return 0;
205204
if (minLen <= 0 || minLen > len ) {
206-
throw new SQLNonTransientException(
207-
"Value out of range 0 < minLen <= %d, minLen was: %d".formatted(len, minLen),
208-
SQLStateConstants.SQL_STATE_INVALID_STRING_LENGTH);
205+
throw FbExceptionBuilder.forNonTransientException(JaybirdErrorCodes.jb_invalidStringLength)
206+
.messageParameter("minLen", len, minLen)
207+
.toSQLException();
209208
}
210209
checkDatabaseAttached();
211210
checkTransactionActive();

jaybird-native/src/main/java/org/firebirdsql/gds/ng/jna/JnaDatabase.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ public void attach() throws SQLException {
119119
}
120120

121121
protected void attachOrCreate(final DatabaseParameterBuffer dpb, final boolean create) throws SQLException {
122-
if (isAttached()) {
123-
throw new SQLException("Already attached to a database");
124-
}
122+
requireNotAttached();
125123
final byte[] dbName = getEncoding().encodeToCharset(connection.getAttachUrl());
126124
final byte[] dpbArray = dpb.toBytesWithType();
127125

jaybird-native/src/main/java/org/firebirdsql/gds/ng/jna/JnaService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,8 @@ public void startServiceAction(ServiceRequestBuffer serviceRequestBuffer) throws
131131
@Override
132132
public void attach() throws SQLException {
133133
try {
134-
if (isAttached()) {
135-
throw new SQLException("Already attached to a service");
136-
}
137-
try (LockCloseable ignored = withLock()) {
134+
requireNotAttached();
135+
try (var ignored = withLock()) {
138136
attachImpl();
139137
setAttached();
140138
afterAttachActions();

jaybird-native/src/main/java/org/firebirdsql/gds/ng/jna/JnaStatement.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.lang.ref.Cleaner;
3838
import java.nio.ByteBuffer;
3939
import java.sql.SQLException;
40-
import java.sql.SQLNonTransientException;
4140
import java.util.Arrays;
4241

4342
import static java.util.Objects.requireNonNull;
@@ -143,13 +142,8 @@ public void prepare(String statementText) throws SQLException {
143142
.toSQLException();
144143
}
145144
}
146-
try (LockCloseable ignored = withLock()) {
147-
checkTransactionActive(getTransaction());
148-
final StatementState initialState = getState();
149-
if (!isPrepareAllowed(initialState)) {
150-
throw new SQLNonTransientException(String.format(
151-
"Current statement state (%s) does not allow call to prepare", initialState));
152-
}
145+
try (var ignored = withLock()) {
146+
final StatementState initialState = checkPrepareAllowed();
153147
resetAll();
154148

155149
final JnaDatabase db = getDatabase();

src/jna-test/org/firebirdsql/gds/ng/jna/JnaDatabaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void doubleAttach() throws Exception {
8989

9090
SQLException exception = assertThrows(SQLException.class, db::attach,
9191
"Second attach should throw exception");
92-
assertThat(exception, message(equalTo("Already attached to a database")));
92+
assertThat(exception, message(startsWith("Already attached to a database")));
9393
}
9494
}
9595

@@ -266,7 +266,7 @@ void testExecuteImmediate_createDatabase() throws Exception {
266266
@Test
267267
public void testOdsVersionInformation() throws Exception {
268268
usesDatabase.createDefaultDatabase();
269-
try (JnaDatabase db = factory.connect(connectionInfo);) {
269+
try (JnaDatabase db = factory.connect(connectionInfo)) {
270270
db.attach();
271271

272272
OdsVersion expectedOds = getDefaultSupportInfo().getDefaultOdsVersion();

src/jna-test/org/firebirdsql/gds/ng/jna/JnaServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void doubleAttach() throws Exception {
8282
//Second attach should throw exception
8383
SQLException exception = assertThrows(SQLException.class, service::attach,
8484
"Second attach should throw exception");
85-
assertThat(exception, message(equalTo("Already attached to a service")));
85+
assertThat(exception, message(startsWith("Already attached to a service")));
8686
}
8787
}
8888

src/main/org/firebirdsql/ds/FBPooledConnection.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.sql.Connection;
2222
import java.sql.SQLException;
23-
import java.sql.SQLNonTransientConnectionException;
2423
import java.util.List;
2524
import java.util.concurrent.CopyOnWriteArrayList;
2625
import java.util.concurrent.locks.Lock;
@@ -31,11 +30,12 @@
3130
import javax.sql.PooledConnection;
3231
import javax.sql.StatementEventListener;
3332

33+
import org.firebirdsql.gds.JaybirdErrorCodes;
34+
import org.firebirdsql.gds.ng.FbExceptionBuilder;
3435
import org.firebirdsql.gds.ng.LockCloseable;
3536
import org.firebirdsql.jaybird.util.SQLExceptionChainBuilder;
3637
import org.firebirdsql.jaybird.xca.FatalErrorHelper;
3738
import org.firebirdsql.jdbc.FirebirdConnection;
38-
import org.firebirdsql.jdbc.SQLStateConstants;
3939

4040
/**
4141
* PooledConnection implementation for {@link FBConnectionPoolDataSource}
@@ -63,28 +63,31 @@ LockCloseable withLock() {
6363

6464
@Override
6565
public Connection getConnection() throws SQLException {
66-
try (LockCloseable ignored = withLock()) {
67-
if (connection == null) {
68-
var ex = new SQLNonTransientConnectionException("The PooledConnection has been closed",
69-
SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
70-
fireFatalConnectionError(ex);
71-
throw ex;
72-
}
66+
try (var ignored = withLock()) {
7367
try {
68+
Connection connection = requireConnection();
7469
if (handler != null) {
7570
handler.close();
7671
}
7772
resetConnection(connection);
73+
handler = createConnectionHandler(connection);
74+
75+
return handler.getProxy();
7876
} catch (SQLException ex) {
7977
fireFatalConnectionError(ex);
8078
throw ex;
8179
}
82-
handler = createConnectionHandler(connection);
83-
84-
return handler.getProxy();
8580
}
8681
}
8782

83+
private Connection requireConnection() throws SQLException {
84+
Connection connection = this.connection;
85+
if (connection != null) return connection;
86+
throw FbExceptionBuilder
87+
.forNonTransientConnectionException(JaybirdErrorCodes.jb_pooledConnectionClosed)
88+
.toSQLException();
89+
}
90+
8891
void resetConnection(Connection connection) throws SQLException {
8992
connection.setAutoCommit(true);
9093
if (connection.isWrapperFor(FirebirdConnection.class)) {

src/main/org/firebirdsql/ds/FBSimpleDataSource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
*/
1919
package org.firebirdsql.ds;
2020

21+
import org.firebirdsql.gds.JaybirdErrorCodes;
2122
import org.firebirdsql.gds.TransactionParameterBuffer;
2223
import org.firebirdsql.gds.impl.GDSFactory;
2324
import org.firebirdsql.gds.impl.GDSType;
25+
import org.firebirdsql.gds.ng.FbExceptionBuilder;
2426
import org.firebirdsql.jaybird.props.def.ConnectionProperty;
2527
import org.firebirdsql.jaybird.xca.FBManagedConnectionFactory;
2628
import org.firebirdsql.jdbc.FBDataSource;
@@ -243,7 +245,9 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
243245
@Override
244246
public <T> T unwrap(Class<T> iface) throws SQLException {
245247
if (!isWrapperFor(iface)) {
246-
throw new SQLException("Unable to unwrap to class " + iface.getName());
248+
throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_unableToUnwrap)
249+
.messageParameter(iface != null ? iface.getName() : "(null)")
250+
.toSQLException();
247251
}
248252

249253
return iface.cast(this);

0 commit comments

Comments
 (0)