Skip to content

Commit

Permalink
Reset query status error code and error message after error (#1094)
Browse files Browse the repository at this point in the history
* reset query status error code and error message after error

format

* add sleep timer in test

Co-authored-by: Lorna Barber <lbarber@magnitude.com>
  • Loading branch information
SimbaGithub and Lorna Barber authored Aug 16, 2022
1 parent 3f2772b commit 9758960
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/snowflake/client/core/SFSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,17 @@ else if (ex instanceof SFException) {
else if (isAnError(result)) {
result.setErrorCode(ErrorCode.INTERNAL_ERROR.getMessageCode());
result.setErrorMessage("no_error_code_from_server");
} else if (!isAnError(result)) {
result.setErrorCode(0);
result.setErrorMessage("No error reported");
}
// if an error message has been provided, set appropriate error message.
// This should override the default error message displayed when there is
// an error with no code.
if (!Strings.isNullOrEmpty(errorMessage) && !errorMessage.equalsIgnoreCase("null")) {
result.setErrorMessage(errorMessage);
} else {
result.setErrorMessage("No error reported");
}
return result;
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/ConnectionLatestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,37 @@ public void testAsyncAndSynchronousQueries() throws SQLException {
con.close();
}

/**
* Tests that error message and error code are reset after an error
*
* @throws SQLException
* @throws InterruptedException
*/
@Test
public void testQueryStatusErrorMessageAndErrorCode() throws SQLException, InterruptedException {
// open connection and run asynchronous query
Connection con = getConnection();
Statement statement = con.createStatement();
statement.execute("create or replace table testTable(colA string, colB boolean)");
statement.execute("insert into testTable values ('test', true)");
ResultSet rs1 =
statement.unwrap(SnowflakeStatement.class).executeAsyncQuery("select * from testTable");
QueryStatus status = rs1.unwrap(SnowflakeResultSet.class).getStatus();
// Set the error message and error code so we can confirm they are reset when getStatus() is
// called.
status.setErrorMessage(QueryStatus.FAILED_WITH_ERROR.toString());
status.setErrorCode(2003);
Thread.sleep(300);
status = rs1.unwrap(SnowflakeResultSet.class).getStatus();
// Assert status of query is a success
assertEquals(QueryStatus.SUCCESS, status);
assertEquals("No error reported", status.getErrorMessage());
assertEquals(0, status.getErrorCode());
statement.execute("drop table if exists testTable");
statement.close();
con.close();
}

@Test
public void testPreparedStatementAsyncQuery() throws SQLException {
Connection con = getConnection();
Expand Down

0 comments on commit 9758960

Please sign in to comment.