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

feat: new configuration setting to opt-in to throwing on errors #1098

Merged
merged 15 commits into from
Jan 5, 2024

Conversation

cprice404
Copy link
Contributor

@cprice404 cprice404 commented Dec 30, 2023

This commit adds a new configuration setting to the various Configuration objects: ThrowOnErrors.

The default behavior for all of the Momento clients is to return errors as part of the function's return value, to try to make error handling feel more like a first-class part of the API. However there are some cases where it is easier to allow errors to bubble up the call stack and handle them via a try/catch, and some users may prefer that style of error handling since it is more prevalent in JS.

If the new ThrowOnErrors config option can be set to true, then the clients will throw exceptions rather than returning them as errors.

@cprice404 cprice404 changed the title throw on errors feat: new configuration setting to opt-in to throwing on errors Jan 2, 2024
@cprice404 cprice404 marked this pull request as ready for review January 2, 2024 22:03

constructor(throwOnError: boolean) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`CONSTRUCTING CACHE SERVICE ERROR MAPPER: ${throwOnError}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
console.log(`CONSTRUCTING CACHE SERVICE ERROR MAPPER: ${throwOnError}`);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

private readonly throwOnError: boolean;

constructor(throwOnError: boolean) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

this.cacheServiceErrorMapper.handleError(
err,
e => new DeleteCache.Error(e),
resolve,
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a compilation failure if you accidentally swap the resolve and reject functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. Presumably you would get some int test failures though because nothing would succeed?

Also open to ideas on how to improve this.

Copy link
Contributor

Choose a reason for hiding this comment

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

the idea that comes to mind first is having the function take in an object with named arguments, but I don't think that its a dealbreaker, and that would require changing basically every line in this pr 😅 . I think how it currently is is good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is done

@@ -244,7 +253,7 @@ export class PubsubClient extends AbstractPubsubClient {
serviceError.code === Status.INTERNAL &&
serviceError.details === PubsubClient.RST_STREAM_NO_ERROR_MESSAGE;
const momentoError = new TopicSubscribe.Error(
cacheServiceErrorMapper(serviceError)
this.cacheServiceErrorMapper.convertError(serviceError)
Copy link
Contributor

Choose a reason for hiding this comment

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

We are not throwing here because we might retry the subscription? Do we want to throw when its the first subscription message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a good question, I probably need to look into this part a little more closely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thing here is that our JS topic subscriptions are callback-based already. So what that means is that there is no thread of execution to try to throw the exception into; if an error occurs during a subscription then the onError callback gets called.

I can't immediately think of a behavior change that the ThrowOnErrors flag could reasonably introduce for a callback-based subscription model. In other languages where the subscriptions are iterator-style, there's a thread to throw the execption into, but not for callback-style.

Open to ideas/discussion, though!

bruuuuuuuce
bruuuuuuuce previously approved these changes Jan 3, 2024
Copy link
Contributor

@bruuuuuuuce bruuuuuuuce left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Contributor

@bruuuuuuuce bruuuuuuuce left a comment

Choose a reason for hiding this comment

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

:shipit:

@cprice404 cprice404 merged commit 0528aef into main Jan 5, 2024
13 checks passed
@cprice404 cprice404 deleted the throw-on-errors branch January 5, 2024 20:20
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.

2 participants