[CSHARP-400] Improve error message in SetIdMember when memberMap argument is for a different class Created: 17/Feb/12  Updated: 02/Apr/15  Resolved: 18/Feb/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.3.1
Fix Version/s: 1.4

Type: Task Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-398 Can't Map Base Class Field As Id Closed
is related to CSHARP-396 BsonClassMap.AutoMap() does not incor... Closed
Backwards Compatibility: Minor Change

 Description   

When SetIdMember is called with a memberMap argument that is for a different class the current error message is too cryptic.

This can be reproduced using these classes:

public class B
{
    public ObjectId Id;
    public int b;
}
 
public class C : B
{
    public int c;
}

and this code:

BsonClassMap.RegisterClassMap<C>(cm =>
{
    cm.AutoMap();
    cm.SetIdMember(cm.GetMemberMap(c => c.Id)); // wrong: Id is in class B, not class C
});

The call to SetIdMember is incorrect because it is being called when registering class C but Id is defined in class B. SetIdMember should have been called when registering class B instead.

The current error message "Invalid memberMap." is too cryptic. A better error message would provide more of an explanation of what is wrong.



 Comments   
Comment by Robert Stam [ 18/Feb/12 ]

Could be backward breaking if you were relying on the type of the exception thrown or the text of the error message. Not likely since you would never pass invalid arguments on purpose.

Comment by Robert Stam [ 18/Feb/12 ]

The new error message for methods that take a memberMap argument is:

"The memberMap argument must be for class {0}, but was for class {1}."

A similar error message was added for methods that take a memberInfo argument:

"The memberInfo argument must be for class {0}, but was for class {1}."

The exception is now of type ArgumentOutOfRangeException.

All methods now throw ArgumentNullException if called with a null argument (before they ended up throwing a NullReferenceException).

Generated at Wed Feb 07 21:36:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.