Skip to content

Commit 8a0db88

Browse files
DavideDtsegismont
authored andcommitted
Set SQL state when there is an exception #1385 (#1386)
1 parent bbe4a1e commit 8a0db88

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/MSSQLException.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class MSSQLException extends DatabaseException {
3030
private List<MSSQLException> additional;
3131

3232
public MSSQLException(int number, byte state, byte severity, String errorMessage, String serverName, String procedureName, int lineNumber) {
33-
super(formatMessage(number, state, severity, errorMessage, serverName, procedureName, lineNumber), number, null);
33+
super(formatMessage(number, state, severity, errorMessage, serverName, procedureName, lineNumber), number, generateStateCode(number, state));
3434
this.state = state;
3535
this.severity = severity;
3636
this.errorMessage = errorMessage;
@@ -39,6 +39,41 @@ public MSSQLException(int number, byte state, byte severity, String errorMessage
3939
this.lineNumber = lineNumber;
4040
}
4141

42+
43+
/**
44+
* Generates the JDBC state code based on the error number returned from the database.
45+
*
46+
* This method is derived from the method with the same name in the JDBC driver
47+
* in com.microsoft.sqlserver.jdbc.SQLServerException
48+
*
49+
* @param errNum
50+
* the vendor error number
51+
* @param databaseState
52+
* the database state
53+
* @return the SQL state code (XOPEN or SQL:2003 conventions)
54+
*/
55+
static String generateStateCode(int errNum, int databaseState) {
56+
switch (errNum) {
57+
// case 18456: return EXCEPTION_XOPEN_CONNECTION_CANT_ESTABLISH; //username password wrong at login
58+
case 8152:
59+
return "22001"; // String data right truncation
60+
case 515: // 2.2705
61+
case 547:
62+
case 2601:
63+
case 2627:
64+
return "23000"; // Integrity constraint violation
65+
case 2714:
66+
return "S0001"; // table already exists
67+
case 208:
68+
return "S0002"; // table not found
69+
case 1205:
70+
return "40001"; // deadlock detected
71+
default: {
72+
return "S" + String.format( "%4s", databaseState ).replaceAll( " ", "0" );
73+
}
74+
}
75+
}
76+
4277
public void add(MSSQLException e) {
4378
if (additional == null) {
4479
additional = new ArrayList<>(3);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.vertx.mssqlclient;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
/**
13+
* Test the generation of a SQL state code given a vendor error code
14+
*/
15+
@RunWith(Parameterized.class)
16+
public class MSSQLExceptionTest {
17+
18+
@Parameterized.Parameters
19+
public static Collection<Object[]> data() {
20+
return Arrays.asList( new Object[][] {
21+
// 22001: String data right truncation
22+
{8152, 1, "22001"},
23+
// 23000: Integrity constraint violation
24+
{515, 1, "23000"},
25+
{547, 1, "23000"},
26+
{2601, 1, "23000"},
27+
{2627, 1, "23000"},
28+
// S0001: Integrity constraint violation
29+
{2714, 1, "S0001"},
30+
// S0002: table not found
31+
{208, 1, "S0002"},
32+
// deadlock detected
33+
{1205, 1, "40001"},
34+
{-100, 54, "S0054"},
35+
{0, 5, "S0005"},
36+
{0, 127, "S0127"},
37+
{0, 0, "S0000"}
38+
} );
39+
}
40+
41+
private int number;
42+
private byte databaseState;
43+
private String expectedSqlCode;
44+
45+
public MSSQLExceptionTest(int vendorCode, int state, String sqlStateCode) {
46+
this.number = vendorCode;
47+
this.databaseState = (byte) state;
48+
this.expectedSqlCode = sqlStateCode;
49+
}
50+
51+
@Test
52+
public void testSqlStateCodes() {
53+
MSSQLException mssqlException = new MSSQLException( number, databaseState, (byte) 0, null, null, null, 1 );
54+
assertEquals( expectedSqlCode, mssqlException.getSqlState() );
55+
assertEquals( number, mssqlException.getErrorCode() );
56+
}
57+
}

0 commit comments

Comments
 (0)