C
C#2y ago
Angius

❔ Return gets ignored for some godforsaken reason

Despite the if (cb is null) return NotFound(); the code progresses further, and causes an unhandled exception
40 Replies
333fred
333fred2y ago
Use a debugger
Angius
AngiusOP2y ago
I am The screenshot comes from the debugger, in fact
333fred
333fred2y ago
Well, what happens when you step through the method?
Angius
AngiusOP2y ago
Angius
AngiusOP2y ago
GetClubBar()? It returns null Which should trigger the return NotFound() But... doesn't
333fred
333fred2y ago
I mean, being entirely honest: I don't believe you If a basic is null check was broken, the world would be on fire Set a breakpoint on that if check and see what cb is at that point
Angius
AngiusOP2y ago
You can see it in the screenshot, the inline value, cb is null
333fred
333fred2y ago
Set a breakpoint and see what the value is when the check actually occurs Given that this is an async method, you're past an await barrier, and cb being lifted to a field, it would not surprise me if rider's debugger was lying to you in some fashion where you are right now
Angius
AngiusOP2y ago
Uh, huh, it seems if I place the breakpoint there it never gets hit...? Nor when I place a breakpoint anywhere after Never hits the return of my GetClubBar() method either... so something earlier What, though...
333fred
333fred2y ago
Alright, so probably this screenshot is a broken state of something
Angius
AngiusOP2y ago
Could be
Angius
AngiusOP2y ago
Line 27 bp gets hit, like 49 bp doesn't
Angius
AngiusOP2y ago
So something with the EF query Wtf does Nullable object must have a value mean tho Isn't the purpose of nullable objects that they can not have a value...?
333fred
333fred2y ago
Which of the things that you unconditionally dereference are nullable value types? Or possibly reference types, not sure how that error gets reported from an EF query
Angius
AngiusOP2y ago
// Club.cs
public class Club : BaseModel, IReportableContent
{
public required string Name { get; set; }
public required string Slug { get; set; }
public required string Hook { get; set; }
public required string Description { get; set; }
public required string Icon { get; set; }
public required string IconId { get; set; }
public DateTime CreationDate { get; set; }
public ICollection<ClubMember> ClubMembers { get; set; } = new List<ClubMember>();
public ICollection<OgmaUser> BannedUsers { get; set; } = new List<OgmaUser>();
public ICollection<ClubThread> Threads { get; set; } = new List<ClubThread>();
public ICollection<Folder> Folders { get; set; } = new List<Folder>();
public ICollection<Report> Reports { get; set; } = new List<Report>();
}
// Club.cs
public class Club : BaseModel, IReportableContent
{
public required string Name { get; set; }
public required string Slug { get; set; }
public required string Hook { get; set; }
public required string Description { get; set; }
public required string Icon { get; set; }
public required string IconId { get; set; }
public DateTime CreationDate { get; set; }
public ICollection<ClubMember> ClubMembers { get; set; } = new List<ClubMember>();
public ICollection<OgmaUser> BannedUsers { get; set; } = new List<OgmaUser>();
public ICollection<ClubThread> Threads { get; set; } = new List<ClubThread>();
public ICollection<Folder> Folders { get; set; } = new List<Folder>();
public ICollection<Report> Reports { get; set; } = new List<Report>();
}
// ClubBar.cs
public class ClubBar
{
public required long Id { get; set; }
public required string Name { get; set; }
public required string Slug { get; set; }
public required string Hook { get; set; }
public required string Description { get; set; }
public required string Icon { get; set; }
public required DateTime CreationDate { get; set; }
public required int ClubMembersCount { get; set; }
public required int ThreadsCount { get; set; }
public required int StoriesCount { get; set; }
public bool IsMember => Role is not null;
public required long FounderId { get; set; }
public required EClubMemberRoles? Role { get; set; }
}
// ClubBar.cs
public class ClubBar
{
public required long Id { get; set; }
public required string Name { get; set; }
public required string Slug { get; set; }
public required string Hook { get; set; }
public required string Description { get; set; }
public required string Icon { get; set; }
public required DateTime CreationDate { get; set; }
public required int ClubMembersCount { get; set; }
public required int ThreadsCount { get; set; }
public required int StoriesCount { get; set; }
public bool IsMember => Role is not null;
public required long FounderId { get; set; }
public required EClubMemberRoles? Role { get; set; }
}
Nothing really jumps out at me Okay, I'm getting somewhere...
.Select(c => new ClubBar
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Hook = c.Hook,
Description = c.Description,
Icon = c.Icon,
CreationDate = c.CreationDate,
ThreadsCount = 0, //, c.Threads.Count,
ClubMembersCount = 0,// c.ClubMembers.Count,
StoriesCount = 0,// c.Folders.Sum(f => f.StoriesCount),
FounderId = 7,// c.ClubMembers.First(cm => cm.Role == EClubMemberRoles.Founder).MemberId,
Role = null
// c.ClubMembers.Any(cm => cm.MemberId == _uid)
// ? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
// : null
})
.Select(c => new ClubBar
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Hook = c.Hook,
Description = c.Description,
Icon = c.Icon,
CreationDate = c.CreationDate,
ThreadsCount = 0, //, c.Threads.Count,
ClubMembersCount = 0,// c.ClubMembers.Count,
StoriesCount = 0,// c.Folders.Sum(f => f.StoriesCount),
FounderId = 7,// c.ClubMembers.First(cm => cm.Role == EClubMemberRoles.Founder).MemberId,
Role = null
// c.ClubMembers.Any(cm => cm.MemberId == _uid)
// ? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
// : null
})
zeroing out everything made it work Lemme uncomment those one by one
333fred
333fred2y ago
I wonder if ClubMembers is null Mainly because I see it a bunch there
Angius
AngiusOP2y ago
Uncommented everything down to Role and it works lmao At least with quick reload But I also do see the data actually show up Issue seems to be somewhere her
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: null
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: null
Since uncommenting it broke the site again
333fred
333fred2y ago
Well, return null from the first branch See if it's the call to Role Or could MemberId be null? You didn't share that model definition
Angius
AngiusOP2y ago
public class ClubMember
{
public OgmaUser Member { get; init; }
public long MemberId { get; init; }
public Club Club { get; init; }
public long ClubId { get; init; }
public EClubMemberRoles Role { get; init; }
public DateTime MemberSince { get; init; }
}
public class ClubMember
{
public OgmaUser Member { get; init; }
public long MemberId { get; init; }
public Club Club { get; init; }
public long ClubId { get; init; }
public EClubMemberRoles Role { get; init; }
public DateTime MemberSince { get; init; }
}
It's a long so unlikely
333fred
333fred2y ago
Is EClubMemberRoles a reference type?
Angius
AngiusOP2y ago
An enum
333fred
333fred2y ago
Hmm Try casting the first branch of the ternary to a EClubMemberRoles?
Angius
AngiusOP2y ago
333fred
333fred2y ago
Ignore the warning And see if that fixes it
Angius
AngiusOP2y ago
No cigar
333fred
333fred2y ago
What about removing the null from the other branch And using default(EClubMemberRoles)
Angius
AngiusOP2y ago
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: EClubMemberRoles.User
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: EClubMemberRoles.User
no cigar either
333fred
333fred2y ago
Did you try this?
Angius
AngiusOP2y ago
Yeah, also errors out for some reason
333fred
333fred2y ago
With the same error?
Angius
AngiusOP2y ago
Maybe it's the fault of hot reload, lemme restart the whole thing
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? null // c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: default
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? null // c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: default
doesn't work
333fred
333fred2y ago
I wouldn't use default there
Angius
AngiusOP2y ago
333fred
333fred2y ago
It's not clear what type it binds to Use default(TheActualTypeYouWant)
Angius
AngiusOP2y ago
Ah, huh... I brought everything back to this state and it doesn't work It seems the issue might lie in the FounderID...? When I set the role to null and founder ID is as it is, it errors When I hardcode 7 or something to it, it works Yeah
.Select(c => new ClubBar
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Hook = c.Hook,
Description = c.Description,
Icon = c.Icon,
CreationDate = c.CreationDate,
ThreadsCount = c.Threads.Count,
ClubMembersCount = c.ClubMembers.Count,
StoriesCount = c.Folders.Sum(f => f.StoriesCount),
FounderId = 7,// c.ClubMembers.First(cm => cm.Role == EClubMemberRoles.Founder).MemberId,
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: null
})
.Select(c => new ClubBar
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Hook = c.Hook,
Description = c.Description,
Icon = c.Icon,
CreationDate = c.CreationDate,
ThreadsCount = c.Threads.Count,
ClubMembersCount = c.ClubMembers.Count,
StoriesCount = c.Folders.Sum(f => f.StoriesCount),
FounderId = 7,// c.ClubMembers.First(cm => cm.Role == EClubMemberRoles.Founder).MemberId,
Role = c.ClubMembers.Any(cm => cm.MemberId == _uid)
? c.ClubMembers.First(cm => cm.MemberId == _uid).Role
: null
})
this works
333fred
333fred2y ago
So you have a club with no founder? Might be best to break out a DB explorer and see what data you have
Angius
AngiusOP2y ago
Shit Seems to be the case, yeah Club with ID 1 somehow ended up without a founder
333fred
333fred2y ago
Oops At least we figured that out
Angius
AngiusOP2y ago
Yeah lol How did I get to this state, though... I hate it when projects grow large enough for me to get lost lol
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server