Skip to content

Commit

Permalink
KException when Serialization fails. Closes #55 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
csteppe-kx authored Aug 23, 2021
1 parent bf5ce9c commit 489d33b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
23 changes: 23 additions & 0 deletions kx.Test/Connection/ConnectionSerialisationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ public void ConnectionSerialiseThrowsIfInputIsNull()
}
}

[Test]
public void ConnectionSerialiseThrowsKExceptionWithInnerExceptionIfSerialisationThrowsException()
{
using (var connection = new c(_testVersionNumber))
{

c.Dict dataRow = new c.Dict(
new[]
{
"sym"
},
new object[]
{
new object[] { null },
});

c.Flip f = new c.Flip(dataRow);

var exception = Assert.Throws<KException>(() => connection.Serialize(1, f));
Assert.NotNull(exception.InnerException);

}
}
[Test]
public void ConnectionSerialiseThrowsIfGuidSerialisationIsNotSupported()
{
Expand Down
33 changes: 20 additions & 13 deletions kx/c.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,22 +700,29 @@ public byte[] Serialize(int messageType, object x, bool zip)
$"Unable to serialize data. {nameof(x)} parameter cannot be null");
}

int length = nx(x) + 8;
_writeBuffer = new byte[length];
_writeBuffer[0] = 1;
_writeBuffer[1] = (byte)messageType;
_writePosition = 4;
w(length);
w(x);
try
{
int length = nx(x) + 8;
_writeBuffer = new byte[length];
_writeBuffer[0] = 1;
_writeBuffer[1] = (byte)messageType;
_writePosition = 4;
w(length);
w(x);

if (zip &&
_writePosition > 2000 &&
!_isLoopback)
if (zip &&
_writePosition > 2000 &&
!_isLoopback)
{
Compress();
}

return _writeBuffer;
}
catch (Exception ex)
{
Compress();
throw new KException("Error occurred while trying to serialize object.", ex);
}

return _writeBuffer;
}

/// <summary>
Expand Down

0 comments on commit 489d33b

Please sign in to comment.