I'm working on a service (in NodeJS) that uses Google PubSub. I'd like to do decent error handling but cannot figure out where constants might be to check error values from the library calls. Eg I have something like:
import { PubSub, Subscription } from '@google-cloud/pubsub';
...etc...
this.pubsubClient = new PubSub();
await this.pubsubClient.createTopic(this.topicName).catch((err) => {
if (err.code === 6) {
// topic already exists
} else {
throw err;
}
});
Obviously I don't want to hardcode that 6
in there, but I cannot figure out where I should be getting a constant to check out of the Google client libraries...
(or for that matter, what type err
should be. I think that would help as well.)
(also, I suppose I should be rapped on the knuckles for not checking for the topic's existence before attempting to create it. Eg using the exception as control flow.)
The error codes come from the gRPC Status
enum . The type of err
is a grpc.ServiceError
.
In general, it is best to try to make the exception be the exceptional case. If you expect that in most cases the topic will exist, then do a getTopic
first and only create the topic if it doesn't. If the topic will usually not exist, then calls createTopic
and handle the case where it already exists.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.