Skip to content

Commit

Permalink
Improve error messages for assertion failures
Browse files Browse the repository at this point in the history
Help developers realize better what they did wrong.
'Assertion failure' is the the most user-friendly message.
  • Loading branch information
Geod24 committed Jul 5, 2019
1 parent e3dd483 commit eeefb3c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/geod24/bitblob.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ public struct BitBlob (size_t Bits)

public this (scope const ubyte[] bin, bool isLE = true)
{
assert(bin.length == Width);
enum W = Width; // Make sure the value is shown, not the symbol
if (bin.length != Width)
assert(0, "ubyte[] argument to " ~ typeof(this).stringof
~ " constructor does not match the expected size of "
~ W.stringof);

this.data[] = bin[];
if (!isLE)
{
Expand Down Expand Up @@ -161,7 +166,9 @@ public struct BitBlob (size_t Bits)

public this (scope const(char)[] hexstr)
{
enum ErrorMsg = "Wrong string size passed to ctor";
enum W = Width; // Make sure the value is shown, not the symbol
enum ErrorMsg = "Length of string passed to " ~ typeof(this).stringof
~ " constructor does not match the expected size of " ~ W.stringof;
if (hexstr.length == (Width * 2) + "0x".length)
{
assert(hexstr[0] == '0', ErrorMsg);
Expand Down

0 comments on commit eeefb3c

Please sign in to comment.