Skip to content

Commit 996c17d

Browse files
committed
Add SQL state to error message
1 parent b3bb925 commit 996c17d

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

metasvc-server/backend-jdbi/src/main/java/io/github/dbmdz/metadata/server/backend/impl/jdbi/UniqueObjectRepositoryImpl.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected boolean isConstraintViolationException(
107107
Throwable throwable, Consumer<String> useMessage) {
108108
if (throwable == null) return false;
109109
if (throwable instanceof SQLException sqlexc) {
110-
useMessage.accept(sqlexc.getMessage());
110+
useMessage.accept("SQL State: %s. %s".formatted(sqlexc.getSQLState(), sqlexc.getMessage()));
111111
/*
112112
* Postgres error codes: https://www.postgresql.org/docs/13/errcodes-appendix.html
113113
*
@@ -123,7 +123,7 @@ protected boolean isConstraintViolationException(
123123
: false;
124124
}
125125

126-
protected ProblemHint getHint(Throwable throwable) {
126+
protected ProblemHint getHint(Throwable throwable, Consumer<String> passSqlState) {
127127
if (throwable == null) return ProblemHint.NONE;
128128
if (throwable instanceof SQLException sqlexc) {
129129
/*
@@ -135,11 +135,14 @@ protected ProblemHint getHint(Throwable throwable) {
135135
case "23505" -> ProblemHint.UNIQUE_VIOLATION;
136136
case "23514" -> ProblemHint.MANDATORY_CHECK_FAILED;
137137
case "23502" -> ProblemHint.PROPERTY_MUST_NOT_BE_NULL;
138-
case "40001" -> ProblemHint.RETRY_RECOMMENDED;
139-
default -> ProblemHint.NONE;
138+
case "40000", "40001", "40002", "40003", "40P01" -> ProblemHint.RETRY_RECOMMENDED;
139+
default -> {
140+
if (passSqlState != null) passSqlState.accept(sqlexc.getSQLState());
141+
yield ProblemHint.NONE;
142+
}
140143
};
141144
} else if (throwable.getCause() != null) {
142-
return getHint(throwable.getCause());
145+
return getHint(throwable.getCause(), passSqlState);
143146
} else {
144147
return ProblemHint.NONE;
145148
}
@@ -174,14 +177,18 @@ private void execInsertUpdate(
174177
} catch (StatementException e) {
175178
AtomicReference<String> constraintMessage = new AtomicReference<>();
176179
if (isConstraintViolationException(e, constraintMessage::set)) {
177-
throw new ValidationException(constraintMessage.get(), getHint(e));
180+
throw new ValidationException(constraintMessage.get(), getHint(e, null));
178181
}
179182

180183
String detailMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
184+
AtomicReference<String> sqlState = new AtomicReference<>("None");
185+
ProblemHint hint = getHint(e, sqlState::set);
181186
throw new RepositoryException(
182-
String.format("SQL exception: %s", detailMessage), e, getHint(e));
187+
String.format("SQL State: %s; exception: %s", sqlState.get(), detailMessage), e, hint);
183188
} catch (JdbiException e) {
184-
throw new RepositoryException(e, getHint(e));
189+
AtomicReference<String> sqlState = new AtomicReference<>("None");
190+
ProblemHint hint = getHint(e, sqlState::set);
191+
throw new RepositoryException("SQL State: %s".formatted(sqlState.get()), e, hint);
185192
}
186193
}
187194

0 commit comments

Comments
 (0)