catch throwable because calcite is throwing an error not exception (#11892)

* catch throwable because calcite is throwing an error not exception

* add test case
This commit is contained in:
TSFenwick 2021-11-10 17:22:04 -08:00 committed by GitHub
parent 13bec7468a
commit cdd1c2876c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -194,11 +194,12 @@ public class SqlResource
endLifecycleWithoutEmittingMetrics(sqlQueryId, lifecycle);
throw (ForbiddenException) serverConfig.getErrorResponseTransformStrategy().transformIfNeeded(e); // let ForbiddenExceptionMapper handle this
}
catch (Exception e) {
// calcite throws a java.lang.AssertionError which is type error not exception. using throwable will catch all
catch (Throwable e) {
log.warn(e, "Failed to handle query: %s", sqlQuery);
endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
final Exception exceptionToReport;
final Throwable exceptionToReport;
if (e instanceof RelOptPlanner.CannotPlanException) {
exceptionToReport = new ISE("Cannot build plan for query: %s", sqlQuery.getQuery());

View File

@ -1048,6 +1048,44 @@ public class SqlResourceTest extends CalciteTestBase
Assert.assertTrue(lifecycleManager.getAll("id").isEmpty());
}
@Test
public void testAssertionErrorThrowsErrorWithFilterResponse() throws Exception
{
resource = new SqlResource(
JSON_MAPPER,
CalciteTests.TEST_AUTHORIZER_MAPPER,
sqlLifecycleFactory,
lifecycleManager,
new ServerConfig() {
@Override
public boolean isShowDetailedJettyErrors()
{
return true;
}
@Override
public ErrorResponseTransformStrategy getErrorResponseTransformStrategy()
{
return new AllowedRegexErrorResponseTransformStrategy(ImmutableList.of());
}
}
);
String errorMessage = "could not assert";
SqlQuery badQuery = EasyMock.createMock(SqlQuery.class);
EasyMock.expect(badQuery.getQuery()).andReturn("SELECT ANSWER TO LIFE");
EasyMock.expect(badQuery.getContext()).andReturn(ImmutableMap.of("sqlQueryId", "id"));
EasyMock.expect(badQuery.getParameterList()).andThrow(new Error(errorMessage));
EasyMock.replay(badQuery);
final QueryException exception = doPost(badQuery).lhs;
Assert.assertNotNull(exception);
Assert.assertNull(exception.getMessage());
Assert.assertNull(exception.getHost());
Assert.assertEquals(exception.getErrorCode(), QueryInterruptedException.UNKNOWN_EXCEPTION);
Assert.assertNull(exception.getErrorClass());
Assert.assertTrue(lifecycleManager.getAll("id").isEmpty());
}
@Test
public void testTooManyRequests() throws Exception
{