-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server errors aren't throwing exceptions #496
Comments
Hi all, is there an update for this? I would expect this program to throw an error when receiving an error back from the server. using NATS.Client.Services;
using NATS.Net;
var url = Environment.GetEnvironmentVariable("NATS_URL") ?? "nats://127.0.0.1:4222";
await using var nc = new NatsClient(url);
var svc = nc.CreateServicesContext();
var service = await svc.AddServiceAsync(new NatsSvcConfig("divide", "0.0.1")
{
Description = "A simple division service",
});
await service.AddEndpointAsync<int>(name: "divide42", handler: async m =>
{
if (m.Exception != null)
{
await m.ReplyErrorAsync(500, m.Exception.Message);
return;
}
if (m.Data == 0)
{
await m.ReplyErrorAsync(400, "Division by zero");
return;
}
await m.ReplyAsync(42 / m.Data);
});
// Send to the service
try
{
var result = await nc.RequestAsync<int, int>("divide42", data: 0);
Console.WriteLine($"Result: {result}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
await service.StopAsync(); Thanks in advance! |
@Tiebe-Vercoutter thanks for the report. Services errors passed down using message headers try
{
var result = await nc.RequestAsync<int, int>("divide42", data: 0);
// PROPOSAL
result.EnsureServiceSuccess(); // <-- extension to check for Nats-Service-Error headers
Console.WriteLine($"Result: {result}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
} we can also include a (PROPOSAL) |
@mtmk thank you for the quick response and explanation of how I can retrieve the error via the headers! |
Observed behavior
When server responds with an error
-ERR
we're not propagating the error as an exception to relevant calls. One use case is the 'Permissions Violation' error for example when permissions are set on the server per subject.Expected behavior
Relevant calls should throw an exception. This should also propagate through to the higher level APIs like Services.
workaround: You should observe the errors in logs
Server and client version
any
Host environment
any
Steps to reproduce
Setup server with JWT auth and permission sub/pub.
Code above runs without any exceptions and it actually doesn't work. you can observe the errors in log.
Also if the
$SVC
subject is allowed but the endpoint subject isn't, service and the endpoint is registered but the endpoint does not respond.Thanks @mkobaly for finding the issue and digging into it.
The text was updated successfully, but these errors were encountered: