1
0

Optimierung: Catch-all-Exception-Behandlung an technischen Grenzen

gezielt geschärft
This commit is contained in:
2026-04-06 07:45:01 +02:00
parent 00daa9cb74
commit 6437ef38af
3 changed files with 60 additions and 53 deletions

View File

@@ -92,11 +92,6 @@ public class Sha256FingerprintAdapter implements FingerprintPort {
candidate.uniqueIdentifier()); candidate.uniqueIdentifier());
logger.error(errorMsg, e); logger.error(errorMsg, e);
return new FingerprintTechnicalError(errorMsg, e); return new FingerprintTechnicalError(errorMsg, e);
} catch (Exception e) {
String errorMsg = String.format("Unexpected error computing fingerprint for '%s': %s",
candidate.uniqueIdentifier(), e.getMessage());
logger.error(errorMsg, e);
return new FingerprintTechnicalError(errorMsg, e);
} }
} }

View File

@@ -49,8 +49,19 @@ public class SqliteUnitOfWorkAdapter implements UnitOfWorkPort {
connection.commit(); connection.commit();
logger.debug("Transaction committed successfully"); logger.debug("Transaction committed successfully");
} catch (Exception e) { } catch (DocumentPersistenceException e) {
// Rollback for ANY exception, not just SQLException // Re-throw document-level persistence errors as-is, but still rollback
if (connection != null) {
try {
connection.rollback();
logger.debug("Transaction rolled back due to document error: {}", e.getMessage());
} catch (SQLException rollbackEx) {
logger.error("Failed to rollback transaction: {}", rollbackEx.getMessage(), rollbackEx);
}
}
throw e;
} catch (RuntimeException e) {
// Rollback on any RuntimeException and wrap in DocumentPersistenceException
if (connection != null) { if (connection != null) {
try { try {
connection.rollback(); connection.rollback();
@@ -59,9 +70,16 @@ public class SqliteUnitOfWorkAdapter implements UnitOfWorkPort {
logger.error("Failed to rollback transaction: {}", rollbackEx.getMessage(), rollbackEx); logger.error("Failed to rollback transaction: {}", rollbackEx.getMessage(), rollbackEx);
} }
} }
// Re-throw as DocumentPersistenceException if not already throw new DocumentPersistenceException("Transaction failed: " + e.getMessage(), e);
if (e instanceof DocumentPersistenceException) { } catch (SQLException e) {
throw (DocumentPersistenceException) e; // Rollback for any SQL error
if (connection != null) {
try {
connection.rollback();
logger.debug("Transaction rolled back due to error: {}", e.getMessage());
} catch (SQLException rollbackEx) {
logger.error("Failed to rollback transaction: {}", rollbackEx.getMessage(), rollbackEx);
}
} }
throw new DocumentPersistenceException("Transaction failed: " + e.getMessage(), e); throw new DocumentPersistenceException("Transaction failed: " + e.getMessage(), e);
} finally { } finally {
@@ -84,53 +102,47 @@ public class SqliteUnitOfWorkAdapter implements UnitOfWorkPort {
@Override @Override
public void saveProcessingAttempt(ProcessingAttempt attempt) { public void saveProcessingAttempt(ProcessingAttempt attempt) {
try { // Repository methods declare DocumentPersistenceException as the only thrown exception.
// Reuse the existing repository logic but with shared connection // Any other exception (NullPointerException, etc.) will propagate to the outer try-catch
SqliteProcessingAttemptRepositoryAdapter repo = // and be caught there.
new SqliteProcessingAttemptRepositoryAdapter(jdbcUrl) { SqliteProcessingAttemptRepositoryAdapter repo =
@Override new SqliteProcessingAttemptRepositoryAdapter(jdbcUrl) {
protected Connection getConnection() throws SQLException { @Override
return connection; protected Connection getConnection() throws SQLException {
} return connection;
}; }
repo.save(attempt); };
} catch (Exception e) { repo.save(attempt);
throw new DocumentPersistenceException("Failed to save processing attempt: " + e.getMessage(), e);
}
} }
@Override @Override
public void createDocumentRecord(DocumentRecord record) { public void createDocumentRecord(DocumentRecord record) {
try { // Repository methods declare DocumentPersistenceException as the only thrown exception.
// Reuse the existing repository logic but with shared connection // Any other exception (NullPointerException, etc.) will propagate to the outer try-catch
SqliteDocumentRecordRepositoryAdapter repo = // and be caught there.
new SqliteDocumentRecordRepositoryAdapter(jdbcUrl) { SqliteDocumentRecordRepositoryAdapter repo =
@Override new SqliteDocumentRecordRepositoryAdapter(jdbcUrl) {
protected Connection getConnection() throws SQLException { @Override
return connection; protected Connection getConnection() throws SQLException {
} return connection;
}; }
repo.create(record); };
} catch (Exception e) { repo.create(record);
throw new DocumentPersistenceException("Failed to create document record: " + e.getMessage(), e);
}
} }
@Override @Override
public void updateDocumentRecord(DocumentRecord record) { public void updateDocumentRecord(DocumentRecord record) {
try { // Repository methods declare DocumentPersistenceException as the only thrown exception.
// Reuse the existing repository logic but with shared connection // Any other exception (NullPointerException, etc.) will propagate to the outer try-catch
SqliteDocumentRecordRepositoryAdapter repo = // and be caught there.
new SqliteDocumentRecordRepositoryAdapter(jdbcUrl) { SqliteDocumentRecordRepositoryAdapter repo =
@Override new SqliteDocumentRecordRepositoryAdapter(jdbcUrl) {
protected Connection getConnection() throws SQLException { @Override
return connection; protected Connection getConnection() throws SQLException {
} return connection;
}; }
repo.update(record); };
} catch (Exception e) { repo.update(record);
throw new DocumentPersistenceException("Failed to update document record: " + e.getMessage(), e);
}
} }
} }
} }

View File

@@ -211,7 +211,7 @@ public class DefaultBatchRunProcessingUseCase implements BatchRunProcessingUseCa
try { try {
runLockPort.release(); runLockPort.release();
logger.debug("Run lock released."); logger.debug("Run lock released.");
} catch (Exception e) { } catch (RuntimeException e) {
logger.warn("Warning: Failed to release run lock.", e); logger.warn("Warning: Failed to release run lock.", e);
} }
} }