C# Controller Friendly Error Notifications with tr...
# package-development
w
With the
tryExecuteAndNotify
helper from Umbraco to help me calling my OpenAPI Spec from my generated TS client. I am trying to return a nice message from my C# Mgmt API Controller in the hope that it gets shown in the error notification but I just get
Copy code
ApiError
A fatal server error occurred. If this continues, please reach out to your administrator.
I would have thought this just magically work and grab the value with the status code ?!
r
Is this happening when you're destructuring the response e.g.
const { data, error } = await tryExecuteAndNotify(...
I'll be honest I haven't actually tried to get the error out from that yet!
w
OK found it !
So not obvious either 🙈
Copy code
csharp
return BadRequest(new Umbraco.Cms.Api.Common.Builders.ProblemDetailsBuilder()
    .WithTitle("Try again")
    .WithDetail("You didnt say the magic word")
    .Build());
I was expecting to just work and show up in the error notification
return BadRequest("You didnt say the magic word");
One of those details thats hidden away :S
Yeh I wanted the tryExecuteAndNotify deal with the error notification, rather than me manually invoke a notification afterwards if I get an error back from that call
So it sends back a JSON object for the response with the 400 Bad Request error code in the response body
Copy code
json
{
    "type": "Error",
    "title": "Try again",
    "status": 400,
    "detail": "You didnt say the magic word"
}
m
I have some "legacy" code with another type of error-responses and I'm not sure I'll migrate this now. For these scenarios I just use
tryExecute
which does not automatically show any notifications.
w
Useful to know there is tryExecute but for now I kind of like the notify approach. But yeh makes sense if you can’t just port over what you have so easily.
32 Views