Returning Service result to Controller (Minimal API)
I have a method in my service. If my deletion fails, how do I let the controller know it failed?
Endpoint for reference:
31 Replies
I was thinking to have the return type be Task<bool> but I am just wondering if there is a different way to handle it.
Exceptions would be one way, but its frowned upon by many. Personally, I'd use a proper result type.
why is exceptions frowned upon?
ie, your
Delete
method would return a Task<Result>
, where Result
has a bool and optionally an error message.
Because exceptions should be for exceptional things, not control flow or error messagesif a deletion fails is that not an exception
a lot of us here on the discord like result types, so theres a pretty large collection of libraries that you could use:
$resultlibs
see $resultlib
$resultlib
if it fails, sure. but a non-existing ID isnt really an exceptional failure
Yeah but what if database has exceptions and stuff
then exception is fine
if the DB connection fails, thats 100% an exception
but is me passing a non-existing ID exceptional? I'm not sure it is
Okay ty I see
again, you could solve this with exceptions and try/catch
but I among many others prefer result types for sitatuations like this
Could you maybe provide a code snippet?
Because what I basically want is that if there is an exception, my controller knows my delete didn't delete because of that exception.
and return the proper code.
I can do you one better. I made a youtube video on this topic only a few weeks ago.
https://www.youtube.com/watch?v=gHZNQe9zh6s
omg youre a youtuber
I mean, uploading a few videos is a pretty low bar...
Do you think this would properly handle my case?
kk, I will check it out.
I personally tend to use
Remora.Results
, but its a bit too strict for some people
the authors of most of those libs hang around here, or have done in the past
but even without a library its a pretty simple concept, as my video demonstrates
yes.Why Remora?
Any reason?
because it forces errors to be objects
you absolutely dont have to use remora just necause its my personal preference :p
I also tend to use quite a bit of other Remora libraries in my recent projects, and they all use that internally
so it just makes sense there
Aren't they already objects..?
Exception is an object, no? Or am I completely misunderstanding
No you're fine. Exceptions are objects.
But in cases where you don't have an exception, but still want information about the error
If there's no exception there's no error, right?
Or are exceptions only handled errors?
exceptions are almost always errors, but there are exceptions to most rules. There are also errors that are not exceptions
I mean "error" here as anything that can cause a method to fail
that might be "the user is stupid and sent a string instead of an int"
or "no hit for given id 5 in database"
these may or may not be exceptions, but they are certainly errors
Pobiega
was it... this?
Quoted by
<@105026391237480448> from #help-0 (click here)
React with ❌ to remove this embed.
here is an example
NotFoundError => ok send the user a 404 not found
ValidationError => send the user a 400 bad request
some other error? => send the user a 500 Internal server error
no error? send the user a 200 ok with the mapped result
Mhmmmmmm
Okay I think I'm getting the gist of it