MySQLErrPacketFactory { public static MySQLErrPacket newInstance(final Exception cause) { if (cause instanceof SQLException) { SQLException sqlException = (SQLException) cause; return null != sqlException.getSQLState() ? new MySQLErrPacket(1, sqlException.getErrorCode(), sqlException.getSQLState(), sqlException.getMessage()) : new MySQLErrPacket(1, MySQLServerErrorCode.ER_INTERNAL_ERROR, cause.getMessage()); } if (cause instanceof ShardingCTLException) { ShardingCTLException shardingCTLException = (ShardingCTLException) cause; return new MySQLErrPacket(1, ShardingCTLErrorCode.valueOf(shardingCTLException), shardingCTLException.getShardingCTL()); } if (cause instanceof TableModifyInTransactionException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE, ((TableModifyInTransactionException) cause).getTableName()); } if (cause instanceof UnknownDatabaseException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_BAD_DB_ERROR, ((UnknownDatabaseException) cause).getDatabaseName()); } if (cause instanceof NoDatabaseSelectedException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_NO_DB_ERROR); } if (cause instanceof DBCreateExistsException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_DB_CREATE_EXISTS_ERROR, ((DBCreateExistsException) cause).getDatabaseName()); } if (cause instanceof DBDropExistsException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_DB_DROP_EXISTS_ERROR, ((DBDropExistsException) cause).getDatabaseName()); } if (cause instanceof TableExistsException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_TABLE_EXISTS_ERROR, ((TableExistsException) cause).getTableName()); } if (cause instanceof CircuitBreakException) { return new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE); } if (cause instanceof UnsupportedCommandException) { return new MySQLErrPacket(1, CommonErrorCode.UNSUPPORTED_COMMAND, ((UnsupportedCommandException) cause).getCommandType()); } if (cause instanceof UnsupportedPreparedStatementException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_UNSUPPORTED_PS); } if (cause instanceof ShardingSphereConfigurationException || cause instanceof SQLParsingException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_NOT_SUPPORTED_YET, cause.getMessage()); } if (cause instanceof RuleNotExistsException) { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_SP_DOES_NOT_EXIST); } return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage()); }  static MySQLErrPacket newInstance(final Exception cause);  }
@Test public void assertNewInstanceWithSQLException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new SQLException("No reason", "XXX", 9999, new RuntimeException(""))); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(9999)); assertThat(actual.getSqlState(), is("XXX")); assertThat(actual.getErrorMessage(), is("No reason")); }
@Test public void assertNewInstanceWithSQLExceptionOfNullSQLState() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new SQLException(new RuntimeException("No reason"))); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1815)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), endsWith("No reason")); }
@Test public void assertNewInstanceWithSQLExceptionOfNullParam() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new SQLException("")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1815)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), startsWith("Internal error")); }
@Test public void assertNewInstanceWithInvalidShardingCTLFormatException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new InvalidShardingCTLFormatException("test")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(11000)); assertThat(actual.getSqlState(), is("S11000")); assertThat(actual.getErrorMessage(), is("Invalid format for sharding ctl [test].")); }
@Test public void assertNewInstanceWithUnsupportedShardingCTLTypeException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new UnsupportedShardingCTLTypeException("sctl:set xxx=xxx")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(11001)); assertThat(actual.getSqlState(), is("S11001")); assertThat(actual.getErrorMessage(), is("Could not support sctl type [sctl:set xxx=xxx].")); }
@Test public void assertNewInstanceWithTableModifyInTransactionException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new TableModifyInTransactionException("tbl")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(3176)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), is("Please do not modify the tbl table with an XA transaction. This is an internal system table used to store GTIDs for committed transactions. " + "Although modifying it can lead to an inconsistent GTID state, if neccessary you can modify it with a non-XA transaction.")); }
@Test public void assertNewInstanceWithUnknownDatabaseException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new UnknownDatabaseException("ds")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1049)); assertThat(actual.getSqlState(), is("42000")); assertThat(actual.getErrorMessage(), is("Unknown database 'ds'")); }
@Test public void assertNewInstanceWithNoDatabaseSelectedException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new NoDatabaseSelectedException()); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1046)); assertThat(actual.getSqlState(), is("3D000")); assertThat(actual.getErrorMessage(), is("No database selected")); }
@Test public void assertNewInstanceWithOtherException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new RuntimeException("No reason")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(10002)); assertThat(actual.getSqlState(), is("C10002")); assertThat(actual.getErrorMessage(), is("Unknown exception: [No reason]")); }
@Test public void assertNewInstanceWithDBCreateExistsException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new DBCreateExistsException("No reason")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1007)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), is("Can't create database 'No reason'; database exists")); }
@Test public void assertNewInstanceWithDBDropExistsException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new DBDropExistsException("No reason")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1008)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), is("Can't drop database 'No reason'; database doesn't exist")); }
@Test public void assertNewInstanceWithTableExistsException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new TableExistsException("table_name")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1050)); assertThat(actual.getSqlState(), is("42S01")); assertThat(actual.getErrorMessage(), is("Table 'table_name' already exists")); }
@Test public void assertNewInstanceWithCircuitBreakException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new CircuitBreakException()); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(10000)); assertThat(actual.getSqlState(), is("C10000")); assertThat(actual.getErrorMessage(), is("Circuit break mode is ON.")); }
@Test public void assertNewInstanceWithShardingSphereConfigurationException() { assertCommonException(MySQLErrPacketFactory.newInstance(new ShardingSphereConfigurationException("No reason"))); }
@Test public void assertNewInstanceWithSQLParsingException() { assertCommonException(MySQLErrPacketFactory.newInstance(new SQLParsingException("No reason"))); }
@Test public void assertNewInstanceWithUnsupportedCommandException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new UnsupportedCommandException("No reason")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(10001)); assertThat(actual.getSqlState(), is("C10001")); assertThat(actual.getErrorMessage(), is("Unsupported command: [No reason]")); }
@Test public void assertNewInstanceWithUnsupportedPreparedStatementException() { MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new UnsupportedPreparedStatementException()); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1295)); assertThat(actual.getSqlState(), is("HY000")); assertThat(actual.getErrorMessage(), is("This command is not supported in the prepared statement protocol yet")); }