@Test void translateInvalidIntegerExceptionInteger() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for integer: \"str1\""); InvalidValueTypeException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException( mock(Throwable.class), new PSQLException(serverErrorMessage)); assertEquals("value:str1 type:INT or LONG", e.getMessage()); }
static InvalidValueTypeException translateInvalidIntegerException( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"") .matcher(message); boolean matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String postgreSqlType = matcher.group(1); String type; switch (postgreSqlType) { case "boolean": type = BOOL.toString(); break; case "date": type = DATE.toString(); break; case "timestamp with time zone": type = DATE_TIME.toString(); break; case "double precision": type = DECIMAL.toString(); break; case "integer": type = INT.toString() + " or " + LONG.toString(); break; default: type = postgreSqlType; break; } String value = matcher.group(2); return new InvalidValueTypeException(value, type, sourceThrowable); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { static InvalidValueTypeException translateInvalidIntegerException( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"") .matcher(message); boolean matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String postgreSqlType = matcher.group(1); String type; switch (postgreSqlType) { case "boolean": type = BOOL.toString(); break; case "date": type = DATE.toString(); break; case "timestamp with time zone": type = DATE_TIME.toString(); break; case "double precision": type = DECIMAL.toString(); break; case "integer": type = INT.toString() + " or " + LONG.toString(); break; default: type = postgreSqlType; break; } String value = matcher.group(2); return new InvalidValueTypeException(value, type, sourceThrowable); } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { static InvalidValueTypeException translateInvalidIntegerException( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"") .matcher(message); boolean matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String postgreSqlType = matcher.group(1); String type; switch (postgreSqlType) { case "boolean": type = BOOL.toString(); break; case "date": type = DATE.toString(); break; case "timestamp with time zone": type = DATE_TIME.toString(); break; case "double precision": type = DECIMAL.toString(); break; case "integer": type = INT.toString() + " or " + LONG.toString(); break; default: type = postgreSqlType; break; } String value = matcher.group(2); return new InvalidValueTypeException(value, type, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { static InvalidValueTypeException translateInvalidIntegerException( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"") .matcher(message); boolean matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String postgreSqlType = matcher.group(1); String type; switch (postgreSqlType) { case "boolean": type = BOOL.toString(); break; case "date": type = DATE.toString(); break; case "timestamp with time zone": type = DATE_TIME.toString(); break; case "double precision": type = DECIMAL.toString(); break; case "integer": type = INT.toString() + " or " + LONG.toString(); break; default: type = postgreSqlType; break; } String value = matcher.group(2); return new InvalidValueTypeException(value, type, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { static InvalidValueTypeException translateInvalidIntegerException( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"") .matcher(message); boolean matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String postgreSqlType = matcher.group(1); String type; switch (postgreSqlType) { case "boolean": type = BOOL.toString(); break; case "date": type = DATE.toString(); break; case "timestamp with time zone": type = DATE_TIME.toString(); break; case "double precision": type = DECIMAL.toString(); break; case "integer": type = INT.toString() + " or " + LONG.toString(); break; default: type = postgreSqlType; break; } String value = matcher.group(2); return new InvalidValueTypeException(value, type, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }