Skip to content

fix dlang#10806 - std.csv inconsistently overrides Exception.toString #10807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions std/csv.d
Original file line number Diff line number Diff line change
Expand Up @@ -147,45 +147,54 @@ class CSVException : Exception
this.col = col;
}

override string toString() @safe pure const
alias toString = typeof(super).toString; // pull into overload set

override void toString(scope void delegate(in char[]) sink) const
{
return "(Row: " ~ to!string(row) ~
", Col: " ~ to!string(col) ~ ") " ~ msg;
sink("(Row: ");
sink(to!string(row));
sink(", Col: ");
sink(to!string(col));
sink(") ");
super.toString(sink);
}
}

///
@safe unittest
@system unittest
{
import std.exception : collectException;
import std.algorithm.searching : count;
import std.algorithm.searching : count, startsWith, canFind;
string text = "a,b,c\nHello,65";
auto ex = collectException!CSVException(csvReader(text).count);
assert(ex.toString == "(Row: 0, Col: 0) Row 2's length 2 does not match previous length of 3.");
assert(ex.toString.startsWith("(Row: 0, Col: 0) ") &&
ex.toString.canFind("Row 2's length 2 does not match previous length of 3."));
}

///
@safe unittest
@system unittest
{
import std.exception : collectException;
import std.algorithm.searching : count;
import std.algorithm.searching : count, startsWith, canFind;
import std.typecons : Tuple;
string text = "a,b\nHello,65";
auto ex = collectException!CSVException(csvReader!(Tuple!(string,int))(text).count);
assert(ex.toString == "(Row: 1, Col: 2) Unexpected 'b' when converting from type string to type int");
assert(ex.toString.startsWith("(Row: 1, Col: 2) ") &&
ex.toString.canFind("Unexpected 'b' when converting from type string to type int"));
}

// https://issues.dlang.org/show_bug.cgi?id=24478
@safe unittest
@system unittest
{
import std.exception : collectException;
import std.algorithm.searching : count;
import std.algorithm.searching : count, startsWith, canFind;
string text = "A, B\n1, 2, 3";
auto ex = collectException!CSVException(csvReader!(string[string])(text, null).count);
assert(ex.toString == "(Row: 1, Col: 3) row contains more values than header");
assert(ex.toString.startsWith("(Row: 1, Col: 3) ") &&
ex.toString.canFind("row contains more values than header"));
}

@safe pure unittest
@system unittest
{
import std.string;
auto e1 = new Exception("Foobar");
Expand All @@ -204,6 +213,20 @@ class CSVException : Exception
assert(em.indexOf("37") != -1);
}

// https://github.com/dlang/phobos/issues/10806
@system unittest
{
import std.exception : collectException;
import std.algorithm.searching : count, canFind;
string text = "a,b,c\nHello,65";
auto ex = collectException!CSVException(csvReader(text).count);
auto s1 = ex.toString();
string s2;
ex.toString((s) { s2 ~= s; });
assert(s1 == s2);
assert(s1.canFind("CSVException@"));
}

/**
* Exception thrown when a Token is identified to not be completed: a quote is
* found in an unquoted field, data continues after a closing quote, or the
Expand Down
Loading