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

Add two new sentinel errors. ErrNoStream and ErrNoConsumer #760

Merged
merged 3 commits into from Jun 28, 2021

Conversation

actatum
Copy link
Contributor

@actatum actatum commented Jun 23, 2021

These sentinel errors will allow for better handling of managing streams and consumers.
Examples:

// Creating a stream when a stream does not exist.
si, err := js.StreamInfo(cfg.streamName)
if err != nil {
	if !errors.Is(err, nats.ErrNoStream) {
		log.Fatalf("js.StreamInfo(): %v", err)
	}
	_, err = js.AddStream(&nats.StreamConfig{
		Name:     "foo",
		Subjects: []string{"foo.>"},
	})
	if err != nil {
		log.Fatalf("js.AddStream(): %v", err)
	}
}

// Creating a consumer when one does not exist.
ci, err := js.ConsumerInfo(cfg.streamName, cfg.consumerName)
if err != nil {
	if !errors.Is(err, nats.ErrNoConsumer) {
		log.Fatalf("js.ConsumerInfo(): %v", err)
	}
	_, err = js.AddConsumer(cfg.streamName, &nats.ConsumerConfig{
		AckPolicy:     nats.AckExplicitPolicy,
		DeliverPolicy: nats.DeliverNewPolicy,
		Durable:       cfg.consumerName,
	})
	if err != nil {
		log.Fatalf("js.AddConsumer(): %v", err)
	}
}

nats.go Outdated Show resolved Hide resolved
nats.go Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jun 23, 2021

Coverage Status

Coverage decreased (-0.1%) to 86.794% when pulling f830631 on actatum:sentinelErrors into 4b75fc5 on nats-io:master.

test/js_test.go Outdated Show resolved Hide resolved
test/js_test.go Outdated Show resolved Hide resolved
@wallyqs
Copy link
Member

wallyqs commented Jun 24, 2021

Thanks @actatum for making the changes! Could you squash the changes into a single commit?
Also I think would be good to change to ErrStreamNotFound and ErrConsumerNotFound so that they are similar to the errors from the server:

https://github.com/nats-io/nats-server/blob/77d54dabeb97c4f7e2d1cbe98e4cd33108e71560/server/errors.json#L351
https://github.com/nats-io/nats-server/blob/77d54dabeb97c4f7e2d1cbe98e4cd33108e71560/server/errors.json#L95

@actatum
Copy link
Contributor Author

actatum commented Jun 24, 2021

Thanks @actatum for making the changes! Could you squash the changes into a single commit?
Also I think would be good to change to ErrStreamNotFound and ErrConsumerNotFound so that they are similar to the errors from the server:

https://github.com/nats-io/nats-server/blob/77d54dabeb97c4f7e2d1cbe98e4cd33108e71560/server/errors.json#L351
https://github.com/nats-io/nats-server/blob/77d54dabeb97c4f7e2d1cbe98e4cd33108e71560/server/errors.json#L95

No problem, had a lot of fun doing this. Everything should now be squashed into a single commit!

nats.go Outdated Show resolved Hide resolved
nats.go Outdated Show resolved Hide resolved
Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants