From 0e6ec72eb4e48c6fa2360fb67c964ae47138003c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20F=C3=B6rster?= Date: Tue, 14 Jan 2025 18:07:58 +0100 Subject: [PATCH] Add SQL state to error message --- .../impl/jdbi/UniqueObjectRepositoryImpl.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/metasvc-server/backend-jdbi/src/main/java/io/github/dbmdz/metadata/server/backend/impl/jdbi/UniqueObjectRepositoryImpl.java b/metasvc-server/backend-jdbi/src/main/java/io/github/dbmdz/metadata/server/backend/impl/jdbi/UniqueObjectRepositoryImpl.java index 88fb4afeb..fb4d48f11 100644 --- a/metasvc-server/backend-jdbi/src/main/java/io/github/dbmdz/metadata/server/backend/impl/jdbi/UniqueObjectRepositoryImpl.java +++ b/metasvc-server/backend-jdbi/src/main/java/io/github/dbmdz/metadata/server/backend/impl/jdbi/UniqueObjectRepositoryImpl.java @@ -107,7 +107,7 @@ protected boolean isConstraintViolationException( Throwable throwable, Consumer 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 * @@ -123,7 +123,7 @@ protected boolean isConstraintViolationException( : false; } - protected ProblemHint getHint(Throwable throwable) { + protected ProblemHint getHint(Throwable throwable, Consumer passSqlState) { if (throwable == null) return ProblemHint.NONE; if (throwable instanceof SQLException sqlexc) { /* @@ -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; } @@ -174,14 +177,18 @@ private void execInsertUpdate( } catch (StatementException e) { AtomicReference 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 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 sqlState = new AtomicReference<>("None"); + ProblemHint hint = getHint(e, sqlState::set); + throw new RepositoryException("SQL State: %s".formatted(sqlState.get()), e, hint); } }