-
Notifications
You must be signed in to change notification settings - Fork 386
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
Exception handling helpers #1462
Comments
Ah, and by the way- I'm also aware that we could have the |
Good idea to cleanup and enforce a bit of consistency here, I like your proposal 🙌 I say let it throw and use |
|
This doesn't seem to work when using the char IConvertible.ToChar(IFormatProvider? provider)
{
throw ExceptionHelper.CreateInvalidCastException<Acceleration, char>();
} Doesn't work here either (and even if it did, I wouldn't call it pretty): double IQuantity.As(Enum unit)
{
if (unit is not AccelerationUnit typedUnit)
ExceptionHelper.ThrowArgumentException<AccelerationUnit>(unit, nameof(unit));
return As(typedUnit); // CS0165: Use of unassigned local variable 'typedUnit'
} One obvious solution would be to revive the
And what about the case of the |
I don't quite follow on example 1, what doesn't work here? If |
I'm not sure I understand the question, what about them? Please mind that I really don't remember our codebase very well without looking at it 🤣 If you are referring to example 1, then I refer to my above question about what doesn't work there. |
Yes, the code provided in example 1 is the implementation body of the char IConvertible.ToChar(IFormatProvider? provider)
{
ExceptionHelper.ThrowInvalidCastException<Acceleration, char>();
// still needs to return something here..
} However the question I should have probably be asking is - why do we need to implement I just did a quick test (on the release-v6 version):
And the number of tests is down from 15373 to 12896.. Now, removing the |
I don't recall the use case for it, but it seems From skimming the discussion, I don't really see a justification for adding it.
@tmilnthorp who originally added it hopefully has some context. Example: object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
if(conversionType == typeof(Acceleration))
return this;
else if(conversionType == typeof(AccelerationUnit))
return Unit;
else if(conversionType == typeof(QuantityType))
return Acceleration.QuantityType;
else if(conversionType == typeof(BaseDimensions))
return Acceleration.BaseDimensions;
else
throw new InvalidCastException($"Converting {typeof(Acceleration)} to {conversionType} is not supported.");
} |
In
|
See #1465 as an example of one potential issue related (_not exactly sure how_🤷) to the discussion regarding the I personally am still in favor of removing it, but if not- we should consider adding the following test (in order to complete the coverage): [Fact]
public void Convert_GetTypeCode_Returns_Object()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal(TypeCode.Object, Convert.GetTypeCode(quantity));
}} |
Ok just to circle back a bit. Whether we remove Then we can revisit when we decide on the interface. |
Ok, I am leaning towards the This should cover the generic This is again slightly off-topic but- here are my two cents on the
|
Points 1,2,4 sounds like a good idea 👍 Sentralizing unit conversions in Number 3 I'm a bit unsure about. We do expose |
The property would stay, it's just the method that is moved: IQuantity someQuantity = Mass.Zero;
Enum unit = _untypedUnitOfMass;
IQuantity newMass = UnitConverter.Convert(someQuantity, unit); Although, I wouldn't recommend using this approach anyway (there is almost always a generic way of doing this without dropping back to the |
I think you are right, moving the implementation out (sentralizing to UnitConverter) is better. |
There are currently two types extending the
UnitsNetException
(AmbigiousUnitParseException
and theUnitNotFoundException
) - with a bunch of standard exceptions that are scattered around (a lot of them part of the auto-generated code).Besides the likely inconsistency in the messaging, having the same string format repeated in code is of course also increasing our file size.
Here is the list of exceptions that I think should cover all of our cases:
UnitNotFoundException
: this one is pretty obvious, it is thrown by theUnitConverter
/UnitParser
etc - it is also the exception that I typically use when given an invalid unit, such as(MassUnit)(-1)
QuantityNotFoundException
: this is thrown when given atype
(or type name) that is unknown- this one is thrown by theQuantityInfoLookup
(which is itself called by theQuantity.Parse
or theUnitConverter
)InvalidConversionException
: I'm not sure if this one applies to the current state of thev6
, but in my new implementation of theUnitConverter
this is thrown when attempting to convert one quantity to another, incompatible, quantity typeAmbiguousUnitParseException
: thrown by theUnitParser
In addition, I would create a new solution folder called
Exceptions
(no namespace generation), with all of the above classed inside it (personally, I think it kinda sucks to have all classes in one namespace, but I don't think we can easily move away from that..).Finally, for the more typical exception-throwing scenarios, I'd like to add some static helper methods. Here's what I've come up so far:
We could probably move these strings to a resources dictionary, but I'm not too concerned with this ATM.
What I'm not sure about is whether the helpers should
return
orthrow
the exception.. If I'm not mistaken the second option, while a bit more convenient, would introduce an additional row in the stack trace compared to the first one. I know of the[DoesNotReturn]
attribute but I don't think it affects the stack trace. What do you think?The text was updated successfully, but these errors were encountered: