-
Notifications
You must be signed in to change notification settings - Fork 337
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
Improve InvalidCastException
message for MySqlDataReader.GetX()
#1135
Comments
I added the following methods (based on my proposed implementations) for testing: public int GetInt32PassName(int ordinal) => GetResultSet().GetCurrentRow().GetInt32(ordinal, null);
public int GetInt32PassName(string name) =>
GetResultSet().GetCurrentRow().GetInt32(GetOrdinal(name), name);
public int GetInt32TryCatch(int ordinal)
{
try
{
return GetInt32(ordinal);
}
catch (InvalidCastException ex)
{
throw new InvalidCastException(ex.Message + $" for column {ordinal}");
}
}
public int GetInt32TryCatch(string name)
{
try
{
return GetInt32(GetOrdinal(name));
}
catch (InvalidCastException ex)
{
throw new InvalidCastException(ex.Message + $" for column {name}");
}
} The implementation of Benchmark results (for a call to
Being 12-15% slower in the successful case is an unacceptable slowdown. |
A revised implementation (for
Benchmarks for
It does seem that the implementation could be rewritten to include the column ordinal in the exception message (in all cases) without a significant hit to performance. This is a little less helpful for users using the |
Upvote from me, even just the ordinal position would be very helpful when tracking down issues. |
Let me throw two approaches in here, that are overkill for just this particular issue, but could be worth considering for better debugging support in general without sacrificing perf:
Such a debugging approach could then provide additional logging, improved exception messages, etc. |
Aside from the problem of bifurcating the ecosystem (will there now be two versions of Pomelo?), my suspicion is that everyone would choose the "production" package, and we'd be in the same situation we are now. Issues like #1092 and #1010 (as well as the high number of click-throughs on URLs currently in exception messages) show that there are a large number of users who need extra information when something goes wrong. These users likely don't know that they are the ones who should use the "debugging" package. If I were to make two packages (which I don't think is a good idea), they would probably be "MySqlConnector" (best for debugging, has more runtime checks) and MySqlConnector.HighPerformance.IKnowWhatImDoing (all runtime checks stripped out); the latter could be used for TechEmpower Framework Benchmarks and other truly high-performance application needs. |
Yeah, I just thought I mention it. This is definitely a controversial subject with no perfect outcome either way. For larger systems, like e.g. Windows itself, it is not uncommon to have dedicated checked builds (Microsoft term for debug builds), so that you can debug in a complex environment with as much assistance as possible. But whether approaches like these are worth it on a package level is questionable. And so far I have not seen wide adaptation on nuget.org for going in this direction. |
From PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1571 (comment)
If
MySqlDataReader.GetX()
is called for the wrong data type, or when the underlying data isNULL
, it will throwInvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
orInvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
.It's suggested that the column name (if
GetX(string)
is called) or ordinal number (forGetX(int)
) be added to the exception message to help novice users diagnose the issue. Note that any implementation should be exceedingly careful to not regress performance:GetX
is called successfully millions of times per day making the (rare) failure have a better message is not worth making the common success case slower.The text was updated successfully, but these errors were encountered: