Skip to content
New issue

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

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

Already on GitHub? # to your account

Result.Error returns null instead of Result when returned from a method called from a controller #186

Closed
selaromdotnet opened this issue May 22, 2024 · 5 comments · Fixed by #189
Assignees
Labels
bug Something isn't working

Comments

@selaromdotnet
Copy link

I'm having the weirdest issue, and I'm sure I'm probably just setting this up or using it wrong, but I can't figure out what that is.

I created a controller that I want to use to call a service that returns a result. If the result is successful, I get that result just fine. However if the service fails for whatever reason, and I attempt to return an error, that error result is null instead.

I created a super simple solution that mimics what I'm doing and does demonstrate the described behavior here: https://1drv.ms/u/s!Ahj4lgIVz2Ld6qwyH38Csz1FKpGY0g?e=p9Ac2D

It's just a controller with an injected service that returns one of the two results. The Success result works, but the Error result throws a null reference exception back in the calling controller because null is returned instead.

What am I doing wrong? this weirdly seems to work in other spots using razor pages, so I think maybe there's something wrong with my controller context or setup?

Any ideas? Much appreciated!!

@selaromdotnet
Copy link
Author

selaromdotnet commented May 22, 2024

edit: previously thought it might be the latest vs that broke it, but tried again with older version and got the same error. Funnily enough, CriticalError also works, in that I get a Result back of that type. Error, however, always returns null!

@ibrhmklc
Copy link

I just added this package to my project. In my first tests, I got a null error when I wanted to return the Error class. There are no problems with other return types. However, when returning with Error, a null result is returned to the person calling the method.

@GiampaoloGabba
Copy link

GiampaoloGabba commented May 25, 2024

I can confirm this.
When i return Result.Error or Result.CriticalError from a method and then i to .ToMinimalApiResult i get a NullReferenceException. and the API breaks.

Trying to debug the issue, the result input parameter is null:

image

But the problem is not only in .ToMinimalApiResult , is the whole Result.Error problematic. If return a Result.Error from a method and try to access it i get a nullrefrenceException no matter what

EDIT:
downgrading to 8.0 fixes the problem.

@ardalis ardalis added the bug Something isn't working label May 25, 2024
@ardalis ardalis self-assigned this May 25, 2024
ardalis added a commit that referenced this issue May 25, 2024
Fix issue with a new extension method
ardalis added a commit that referenced this issue May 25, 2024
Fix issue with a new extension method
@selaromdotnet
Copy link
Author

selaromdotnet commented May 25, 2024 via email

@ardalis
Copy link
Owner

ardalis commented May 25, 2024

It's live!
https://www.nuget.org/packages/Ardalis.Result

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants