@Override
    public void writeTo(MysqlSerializer serializer) {
        MysqlCapability capability = serializer.getCapability();

        serializer.writeInt1(ERROR_PACKET_INDICATOR);
        serializer.writeInt2(errorCode);
        if (capability.isProtocol41()) {
            serializer.writeByte((byte) '#');
            serializer.writeBytes(sqlState, 0, 5);
        }
        if (errorMessage == null || errorMessage.isEmpty()) {
            // NOTICE: if write "" or "\0", the client will be show "Query OK"
            // SO we need write no-empty string
            serializer.writeEofString("Unknown error");
        } else {
            serializer.writeEofString(errorMessage);
        }
    }
@Test
    public void testWrite() {
        MysqlSerializer serializer = MysqlSerializer.newInstance(capability);
        QueryState state = new QueryState();
        state.setError("error");

        MysqlErrPacket packet = new MysqlErrPacket(state);
        packet.writeTo(serializer);
        ByteBuffer buffer = serializer.toByteBuffer();

        // assert indicator
        Assert.assertEquals(0xff, MysqlProto.readInt1(buffer));
        // error code
        Assert.assertEquals(1064, MysqlProto.readInt2(buffer));
        // sql state marker
        Assert.assertEquals('#', MysqlProto.readInt1(buffer));
        // sql state
        Assert.assertEquals("HY000", new String(MysqlProto.readFixedString(buffer, 5)));
        // sql state
        Assert.assertEquals("error", new String(MysqlProto.readEofString(buffer)));

        Assert.assertEquals(0, buffer.remaining());
    }