Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL state to error message #219

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected boolean isConstraintViolationException(
Throwable throwable, Consumer<String> useMessage) {
if (throwable == null) return false;
if (throwable instanceof SQLException sqlexc) {
useMessage.accept(sqlexc.getMessage());
useMessage.accept("SQL State: %s. %s".formatted(sqlexc.getSQLState(), sqlexc.getMessage()));
/*
* Postgres error codes: https://www.postgresql.org/docs/13/errcodes-appendix.html
*
Expand All @@ -123,7 +123,7 @@ protected boolean isConstraintViolationException(
: false;
}

protected ProblemHint getHint(Throwable throwable) {
protected ProblemHint getHint(Throwable throwable, Consumer<String> passSqlState) {
if (throwable == null) return ProblemHint.NONE;
if (throwable instanceof SQLException sqlexc) {
/*
Expand All @@ -135,11 +135,14 @@ protected ProblemHint getHint(Throwable throwable) {
case "23505" -> ProblemHint.UNIQUE_VIOLATION;
case "23514" -> ProblemHint.MANDATORY_CHECK_FAILED;
case "23502" -> ProblemHint.PROPERTY_MUST_NOT_BE_NULL;
case "40001" -> ProblemHint.RETRY_RECOMMENDED;
default -> ProblemHint.NONE;
case "40000", "40001", "40002", "40003", "40P01" -> ProblemHint.RETRY_RECOMMENDED;
default -> {
if (passSqlState != null) passSqlState.accept(sqlexc.getSQLState());
yield ProblemHint.NONE;
}
};
} else if (throwable.getCause() != null) {
return getHint(throwable.getCause());
return getHint(throwable.getCause(), passSqlState);
} else {
return ProblemHint.NONE;
}
Expand Down Expand Up @@ -174,14 +177,18 @@ private void execInsertUpdate(
} catch (StatementException e) {
AtomicReference<String> constraintMessage = new AtomicReference<>();
if (isConstraintViolationException(e, constraintMessage::set)) {
throw new ValidationException(constraintMessage.get(), getHint(e));
throw new ValidationException(constraintMessage.get(), getHint(e, null));
}

String detailMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
AtomicReference<String> sqlState = new AtomicReference<>("None");
ProblemHint hint = getHint(e, sqlState::set);
throw new RepositoryException(
String.format("SQL exception: %s", detailMessage), e, getHint(e));
String.format("SQL State: %s; exception: %s", sqlState.get(), detailMessage), e, hint);
} catch (JdbiException e) {
throw new RepositoryException(e, getHint(e));
AtomicReference<String> sqlState = new AtomicReference<>("None");
ProblemHint hint = getHint(e, sqlState::set);
throw new RepositoryException("SQL State: %s".formatted(sqlState.get()), e, hint);
}
}

Expand Down
Loading