Skip to content
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

Open
mtmk opened this issue May 9, 2024 · 3 comments
Open

Server errors aren't throwing exceptions #496

mtmk opened this issue May 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@mtmk
Copy link
Collaborator

mtmk commented May 9, 2024

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.

var opts = new NatsOpts
{
    LoggerFactory = // register your factory here
    Url = "tls://connect.ngs.global:4222",
    Name = "test-service",
    AuthOpts = new NatsAuthOpts { Jwt = "xyz..." }
};
var cnn = new NatsConnection(opts);
var svc = new NatsSvcContext(cnn);
var cfg = new NatsSvcConfig("foo", "1.0.0") { Description = "test service" };
var service = await svc.AddServiceAsync(cfg);
await service.AddEndpointAsync<string>((x) => { return x.ReplyAsync("test"); }, "getstring", "foo.aaa.bbb.test");

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.

@mtmk mtmk added the bug Something isn't working label May 9, 2024
@Tiebe-Vercoutter
Copy link

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!

@mtmk
Copy link
Collaborator Author

mtmk commented Jan 23, 2025

@Tiebe-Vercoutter thanks for the report. Services errors passed down using message headers Nats-Service-Error. So it's services api convention and not a core NATS error, that's why you don't see exceptions. Having said that you're correct we currently don't have a mechanism to detect the error. We can include an Ensure kind of extension to promote an exception e.g.:

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) IsServiceSuccess() extension to have an exception free route. wdyt?

@Tiebe-Vercoutter
Copy link

@mtmk thank you for the quick response and explanation of how I can retrieve the error via the headers!
I think both of those suggestions would be handy to have (similar like this for reference: https://learn.microsoft.com/en-us/dotnet/api/System.Net.Http.HttpResponseMessage.IsSuccessStatusCode?view=net-8.0).

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

No branches or pull requests

2 participants