Skip to content

Commit 67df11e

Browse files
committed
Externalize more exception messages and states, simplify creation of exceptions without parameters
1 parent 7bebb57 commit 67df11e

File tree

59 files changed

+298
-229
lines changed

Some content is hidden

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

59 files changed

+298
-229
lines changed

chacha64-plugin/src/main/java/org/firebirdsql/jaybird/chacha64/ChaCha64EncryptionPlugin.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ private Cipher createDecryptionCipher(ChaCha64IV iv) throws SQLException {
8383
private byte[] toChaChaKey(byte[] key) throws SQLException {
8484
if (key.length < 16) {
8585
throw FbExceptionBuilder.forNonTransientException(jb_cryptInvalidKey)
86-
.messageParameter(encryptionIdentifier())
87-
.messageParameter("Key too short")
86+
.messageParameter(encryptionIdentifier(), "Key too short")
8887
.toSQLException();
8988
}
9089
try {
@@ -127,8 +126,7 @@ private class ChaCha64IV implements AutoCloseable {
127126
byte[] iv = cryptSessionConfig.specificData();
128127
if (iv == null || iv.length != 8) {
129128
throw FbExceptionBuilder.forNonTransientException(jb_cryptInvalidKey)
130-
.messageParameter(encryptionIdentifier())
131-
.messageParameter("Wrong IV length, needs 8 bytes")
129+
.messageParameter(encryptionIdentifier(), "Wrong IV length, needs 8 bytes")
132130
.toSQLException();
133131
}
134132

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.nio.ByteBuffer;
3737
import java.sql.SQLException;
3838

39+
import static org.firebirdsql.gds.ISCConstants.isc_segstr_no_op;
3940
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_blobGetSegmentNegative;
4041
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_blobPutSegmentEmpty;
4142

@@ -121,7 +122,7 @@ public final long getBlobId() {
121122
public void open() throws SQLException {
122123
try {
123124
if (isOutput() && getBlobId() != NO_BLOB_ID) {
124-
throw FbExceptionBuilder.forNonTransientException(ISCConstants.isc_segstr_no_op).toSQLException();
125+
throw FbExceptionBuilder.toNonTransientException(isc_segstr_no_op);
125126
}
126127

127128
final BlobParameterBuffer blobParameterBuffer = getBlobParameterBuffer();
@@ -237,7 +238,7 @@ public void put(final byte[] b, final int off, final int len) throws SQLExceptio
237238
try (LockCloseable ignored = withLock()) {
238239
validateBufferLength(b, off, len);
239240
if (len == 0) {
240-
throw FbExceptionBuilder.forException(jb_blobPutSegmentEmpty).toSQLException();
241+
throw FbExceptionBuilder.toException(jb_blobPutSegmentEmpty);
241242
}
242243
checkDatabaseAttached();
243244
checkTransactionActive();

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
import static java.util.Collections.emptySet;
3838
import static org.firebirdsql.gds.ISCConstants.fb_cancel_abort;
39+
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_executeImmediateRequiresNoTransactionDetached;
40+
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_executeImmediateRequiresTransactionAttached;
41+
import static org.firebirdsql.gds.JaybirdErrorCodes.jb_invalidTransactionHandleType;
3942
import static org.firebirdsql.gds.ng.TransactionHelper.checkTransactionActive;
4043

4144
/**
@@ -83,7 +86,7 @@ protected void setDetachedJna() {
8386
@Override
8487
protected void checkConnected() throws SQLException {
8588
if (!isAttached()) {
86-
throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_notAttachedToDatabase).toSQLException();
89+
throw FbExceptionBuilder.toException(JaybirdErrorCodes.jb_notAttachedToDatabase);
8790
}
8891
}
8992

@@ -332,19 +335,15 @@ public void executeImmediate(String statementText, FbTransaction transaction) th
332335
try {
333336
if (isAttached()) {
334337
if (transaction == null) {
335-
throw FbExceptionBuilder
336-
.forException(JaybirdErrorCodes.jb_executeImmediateRequiresTransactionAttached)
337-
.toSQLException();
338+
throw FbExceptionBuilder.toException(jb_executeImmediateRequiresTransactionAttached);
338339
} else if (!(transaction instanceof JnaTransaction)) {
339-
throw FbExceptionBuilder.forNonTransientException(JaybirdErrorCodes.jb_invalidTransactionHandleType)
340+
throw FbExceptionBuilder.forNonTransientException(jb_invalidTransactionHandleType)
340341
.messageParameter(transaction.getClass())
341342
.toSQLException();
342343
}
343344
checkTransactionActive(transaction);
344345
} else if (transaction != null) {
345-
throw FbExceptionBuilder
346-
.forException(JaybirdErrorCodes.jb_executeImmediateRequiresNoTransactionDetached)
347-
.toSQLException();
346+
throw FbExceptionBuilder.toException(jb_executeImmediateRequiresNoTransactionDetached);
348347
}
349348

350349
final byte[] statementArray = getEncoding().encodeToCharset(statementText);
@@ -393,8 +392,7 @@ protected JnaEventHandle validateEventHandle(EventHandle eventHandle) throws SQL
393392
.toSQLException();
394393
}
395394
if (jnaEventHandle.getSize() == -1) {
396-
throw FbExceptionBuilder.forTransientException(JaybirdErrorCodes.jb_eventHandleNotInitialized)
397-
.toSQLException();
395+
throw FbExceptionBuilder.toTransientException(JaybirdErrorCodes.jb_eventHandleNotInitialized);
398396
}
399397
return jnaEventHandle;
400398
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public ServiceRequestBuffer createServiceRequestBuffer() {
8383
@Override
8484
protected void checkConnected() throws SQLException {
8585
if (!isAttached()) {
86-
throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_notAttachedToDatabase).toSQLException();
86+
throw FbExceptionBuilder.toException(JaybirdErrorCodes.jb_notAttachedToDatabase);
8787
}
8888
}
8989

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.firebirdsql.gds.ng.fields.RowValue;
3030
import org.firebirdsql.gds.ng.listeners.DatabaseListener;
3131
import org.firebirdsql.jaybird.util.Cleaners;
32+
import org.firebirdsql.jdbc.SQLStateConstants;
3233
import org.firebirdsql.jna.fbclient.FbClientLibrary;
3334
import org.firebirdsql.jna.fbclient.ISC_STATUS;
3435
import org.firebirdsql.jna.fbclient.XSQLDA;
@@ -137,8 +138,7 @@ public void prepare(String statementText) throws SQLException {
137138
useNulTerminated = true;
138139
} else {
139140
throw FbExceptionBuilder.forException(JaybirdErrorCodes.jb_maxStatementLengthExceeded)
140-
.messageParameter(JnaDatabase.MAX_STATEMENT_LENGTH)
141-
.messageParameter(statementArray.length)
141+
.messageParameter(JnaDatabase.MAX_STATEMENT_LENGTH, statementArray.length)
142142
.toSQLException();
143143
}
144144
}
@@ -217,7 +217,7 @@ public void execute(RowValue parameters) throws SQLException {
217217
try (OperationCloseHandle operationCloseHandle = signalExecute()) {
218218
if (operationCloseHandle.isCancelled()) {
219219
// operation was synchronously cancelled from an OperationAware implementation
220-
throw FbExceptionBuilder.forException(ISCConstants.isc_cancelled).toSQLException();
220+
throw FbExceptionBuilder.toException(ISCConstants.isc_cancelled);
221221
}
222222
if (hasSingletonResult) {
223223
clientLibrary.isc_dsql_execute2(statusVector, getTransaction().getJnaHandle(), handle,
@@ -392,32 +392,35 @@ protected RowValue toRowValue(RowDescriptor rowDescriptor, XSQLDA xSqlDa) {
392392
*/
393393
@Override
394394
public void fetchRows(int fetchSize) throws SQLException {
395-
try (LockCloseable ignored = withLock()) {
395+
try (var ignored = withLock()) {
396396
checkStatementHasOpenCursor();
397397
checkFetchSize(fetchSize);
398398
if (isAfterLast()) return;
399399

400400
try (OperationCloseHandle operationCloseHandle = signalFetch()) {
401401
if (operationCloseHandle.isCancelled()) {
402402
// operation was synchronously cancelled from an OperationAware implementation
403-
throw FbExceptionBuilder.forException(ISCConstants.isc_cancelled).toSQLException();
403+
throw FbExceptionBuilder.toException(ISCConstants.isc_cancelled);
404404
}
405405
final ISC_STATUS fetchStatus = clientLibrary.isc_dsql_fetch(statusVector, handle, outXSqlDa.version,
406406
outXSqlDa);
407407
processStatusVector();
408408

409-
int fetchStatusInt = fetchStatus.intValue();
410-
if (fetchStatusInt == ISCConstants.FETCH_OK) {
409+
switch (fetchStatus.intValue()) {
410+
case ISCConstants.FETCH_OK -> {
411411
queueRowData(toRowValue(getRowDescriptor(), outXSqlDa));
412412
statementListenerDispatcher.fetchComplete(this, FetchDirection.FORWARD, 1);
413-
} else if (fetchStatusInt == ISCConstants.FETCH_NO_MORE_ROWS) {
413+
}
414+
case ISCConstants.FETCH_NO_MORE_ROWS -> {
414415
statementListenerDispatcher.fetchComplete(this, FetchDirection.FORWARD, 0);
415416
setAfterLast();
416417
// Note: we are not explicitly 'closing' the cursor here
417-
} else {
418-
final String message = "Unexpected fetch status (expected 0 or 100): " + fetchStatusInt;
418+
}
419+
default -> {
420+
final String message = "Unexpected fetch status (expected 0 or 100): " + fetchStatus;
419421
log.log(System.Logger.Level.DEBUG, message);
420-
throw new SQLException(message);
422+
throw new SQLException(message, SQLStateConstants.SQL_STATE_GENERAL_ERROR);
423+
}
421424
}
422425
}
423426
} catch (SQLException e) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public Connection getConnection() throws SQLException {
8383
private Connection requireConnection() throws SQLException {
8484
Connection connection = this.connection;
8585
if (connection != null) return connection;
86-
throw FbExceptionBuilder
87-
.forNonTransientConnectionException(JaybirdErrorCodes.jb_pooledConnectionClosed)
88-
.toSQLException();
86+
throw FbExceptionBuilder.toNonTransientConnectionException(JaybirdErrorCodes.jb_pooledConnectionClosed);
8987
}
9088

9189
void resetConnection(Connection connection) throws SQLException {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ public void setDescription(String description) {
201201
}
202202

203203
/**
204-
* Get underlying connection factory (in our case instance of {@link FBDataSource} class) that will provide JDBC
205-
* connections.
204+
* Get underlying connection factory (an instance of {@link FBDataSource} class) that will provide JDBC connections.
206205
*
207206
* @return JDBC connection factory.
208207
* @throws SQLException
@@ -227,7 +226,7 @@ protected DataSource getDataSource() throws SQLException {
227226
}
228227

229228
if (trimToNull(mcf.getDatabaseName()) == null) {
230-
throw new SQLException("Database was not specified. Cannot provide connections.");
229+
throw FbExceptionBuilder.toNonTransientConnectionException(JaybirdErrorCodes.jb_databasePathRequired);
231230
}
232231
return ds = (FBDataSource) mcf.createConnectionFactory();
233232
} finally {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ boolean inDistributedTransaction() throws SQLException {
6969
private FBManagedConnection getManagedConnection() throws SQLException {
7070
FBManagedConnection managedConnection = mc.get();
7171
if (managedConnection == null) {
72-
throw FbExceptionBuilder.forNonTransientConnectionException(JaybirdErrorCodes.jb_noManagedConnection)
73-
.toSQLException();
72+
throw FbExceptionBuilder.toNonTransientConnectionException(JaybirdErrorCodes.jb_noManagedConnection);
7473
}
7574
return managedConnection;
7675
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
9393
}
9494
} else if (isClosed() && !method.equals(CONNECTION_CLOSE)) {
9595
throw forcedClose
96-
? FbExceptionBuilder.forNonTransientConnectionException(jb_logicalConnectionForciblyClosed).toSQLException()
96+
? FbExceptionBuilder.toNonTransientConnectionException(jb_logicalConnectionForciblyClosed)
9797
: FbExceptionBuilder.connectionClosed();
9898
}
9999

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
8585
} else if (method.equals(STATEMENT_IS_CLOSED) || method.equals(FIREBIRD_STATEMENT_IS_CLOSED)) {
8686
return isClosed();
8787
} else if (isClosed() && !method.equals(STATEMENT_CLOSE)) {
88-
throw FbExceptionBuilder.forNonTransientException(JaybirdErrorCodes.jb_stmtClosed).toSQLException();
88+
throw FbExceptionBuilder.toNonTransientException(JaybirdErrorCodes.jb_stmtClosed);
8989
}
9090

9191
// Methods of statement and subinterfaces

0 commit comments

Comments
 (0)