Skip to content

Commit 7ffbdaf

Browse files
authored
Sanitize Oracle error messages (#1081)
Fixes #1073 When failure is an OracleDatabaseException, it can be simplified using the string format only. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent b0b97bc commit 7ffbdaf

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.oracleclient.impl;
13+
14+
import io.vertx.core.VertxException;
15+
import oracle.jdbc.OracleDatabaseException;
16+
17+
import java.sql.SQLException;
18+
19+
public class FailureUtil {
20+
21+
public static Throwable sanitize(Throwable t) {
22+
if (t instanceof SQLException) {
23+
SQLException se = (SQLException) t;
24+
Throwable cause = se.getCause();
25+
if (cause instanceof OracleDatabaseException) {
26+
OracleDatabaseException oae = (OracleDatabaseException) cause;
27+
return new VertxException(oae.toString(), true);
28+
}
29+
}
30+
return t;
31+
}
32+
33+
private FailureUtil() {
34+
// Utility
35+
}
36+
}

vertx-oracle-client/src/main/java/io/vertx/oracleclient/impl/Helper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
package io.vertx.oracleclient.impl;
1212

13-
import io.vertx.core.Context;
1413
import io.vertx.core.Future;
1514
import io.vertx.core.Promise;
1615
import io.vertx.core.VertxException;
@@ -24,6 +23,8 @@
2423
import java.util.concurrent.Flow;
2524
import java.util.function.Supplier;
2625

26+
import static io.vertx.oracleclient.impl.FailureUtil.sanitize;
27+
2728
public class Helper {
2829

2930
public static <T> Future<T> completeOrFail(ThrowingSupplier<T> supplier) {
@@ -132,7 +133,7 @@ public void onNext(T item) {
132133

133134
@Override
134135
public void onError(Throwable throwable) {
135-
promise.fail(throwable);
136+
promise.fail(sanitize(throwable));
136137
}
137138

138139
@Override
@@ -162,7 +163,7 @@ public void onNext(T item) {
162163

163164
@Override
164165
public void onError(Throwable throwable) {
165-
context.runOnContext(x -> promise.fail(throwable));
166+
promise.fail(sanitize(throwable));
166167
}
167168

168169
@Override
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
12+
package io.vertx.oracleclient.test;
13+
14+
import io.vertx.ext.unit.TestContext;
15+
import io.vertx.ext.unit.junit.VertxUnitRunner;
16+
import io.vertx.oracleclient.OraclePool;
17+
import io.vertx.oracleclient.test.junit.OracleRule;
18+
import io.vertx.sqlclient.PoolOptions;
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.ClassRule;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
@RunWith(VertxUnitRunner.class)
29+
public class OracleErrorSimpleTest extends OracleTestBase {
30+
31+
@ClassRule
32+
public static OracleRule oracle = OracleRule.SHARED_INSTANCE;
33+
34+
OraclePool pool;
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions());
39+
}
40+
41+
@Test
42+
public void testMetadata(TestContext ctx) {
43+
pool.withConnection(conn -> conn.query("DROP TABLE u_dont_exist").execute(), ctx.asyncAssertFailure(t -> {
44+
assertEquals(0, t.getStackTrace().length);
45+
assertTrue(t.getMessage().contains("ORA-00942") && t.getMessage().contains("u_dont_exist"));
46+
}));
47+
}
48+
49+
@After
50+
public void tearDown(TestContext ctx) throws Exception {
51+
pool.close(ctx.asyncAssertSuccess());
52+
}
53+
}

0 commit comments

Comments
 (0)