Optimierung: Catch-all-Exception-Behandlung an technischen Grenzen
gezielt geschärft
This commit is contained in:
@@ -78,25 +78,20 @@ public class Sha256FingerprintAdapter implements FingerprintPort {
|
|||||||
return new FingerprintSuccess(fingerprint);
|
return new FingerprintSuccess(fingerprint);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
String errorMsg = String.format("Failed to read file for '%s': %s",
|
String errorMsg = String.format("Failed to read file for '%s': %s",
|
||||||
candidate.uniqueIdentifier(), e.getMessage());
|
candidate.uniqueIdentifier(), e.getMessage());
|
||||||
logger.warn(errorMsg, e);
|
logger.warn(errorMsg, e);
|
||||||
return new FingerprintTechnicalError(errorMsg, e);
|
return new FingerprintTechnicalError(errorMsg, e);
|
||||||
} catch (InvalidPathException e) {
|
} catch (InvalidPathException e) {
|
||||||
String errorMsg = String.format("Invalid file path for '%s': %s",
|
String errorMsg = String.format("Invalid file path for '%s': %s",
|
||||||
candidate.uniqueIdentifier(), e.getMessage());
|
candidate.uniqueIdentifier(), e.getMessage());
|
||||||
logger.warn(errorMsg, e);
|
logger.warn(errorMsg, e);
|
||||||
return new FingerprintTechnicalError(errorMsg, e);
|
return new FingerprintTechnicalError(errorMsg, e);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
String errorMsg = String.format("SHA-256 algorithm not available for '%s'",
|
String errorMsg = String.format("SHA-256 algorithm not available for '%s'",
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user