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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/client-sdk-nodejs/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {Config} from 'jest';
const config: Config = {
setupFilesAfterEnv: ['jest-extended/all'],
testEnvironment: 'node',
roots: ['<rootDir>/test'],
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
Expand Down
7 changes: 7 additions & 0 deletions packages/client-sdk-nodejs/src/auth-client-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ export interface AuthClientProps {
* controls how the client will get authentication information for connecting to the Momento service
*/
credentialProvider: CredentialProvider;

/**
* Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
*/
throwOnErrors?: boolean;
}
41 changes: 41 additions & 0 deletions packages/client-sdk-nodejs/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface ConfigurationProps {
* Configures middleware functions that will wrap each request
*/
middlewares: Middleware[];
/**
* Configures whether the client should return a Momento Error object or throw an exception when an error occurs.
*/
throwOnErrors: boolean;
}

/**
Expand Down Expand Up @@ -83,19 +87,37 @@ export interface Configuration {
* @returns {Configuration} a new Configuration object with its TransportStrategy updated to use the specified client timeout
*/
withClientTimeoutMillis(clientTimeoutMillis: number): Configuration;

/**
* @returns {boolean} Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
*/
getThrowOnErrors(): boolean;

/**
* Copy constructor for configuring whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
* @param {boolean} throwOnErrors
* @returns {Configuration} a new Configuration object with the specified throwOnErrors setting
*/
withThrowOnErrors(throwOnErrors: boolean): Configuration;
}

export class CacheConfiguration implements Configuration {
private readonly loggerFactory: MomentoLoggerFactory;
private readonly retryStrategy: RetryStrategy;
private readonly transportStrategy: TransportStrategy;
private readonly middlewares: Middleware[];
private readonly throwOnErrors: boolean;

constructor(props: ConfigurationProps) {
this.loggerFactory = props.loggerFactory;
this.retryStrategy = props.retryStrategy;
this.transportStrategy = props.transportStrategy;
this.middlewares = props.middlewares;
this.throwOnErrors = props.throwOnErrors;
}

getLoggerFactory(): MomentoLoggerFactory {
Expand All @@ -112,6 +134,7 @@ export class CacheConfiguration implements Configuration {
retryStrategy: retryStrategy,
transportStrategy: this.transportStrategy,
middlewares: this.middlewares,
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -125,6 +148,7 @@ export class CacheConfiguration implements Configuration {
retryStrategy: this.retryStrategy,
transportStrategy: transportStrategy,
middlewares: this.middlewares,
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -138,6 +162,7 @@ export class CacheConfiguration implements Configuration {
retryStrategy: this.retryStrategy,
transportStrategy: this.transportStrategy,
middlewares: middlewares,
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -147,6 +172,7 @@ export class CacheConfiguration implements Configuration {
retryStrategy: this.retryStrategy,
transportStrategy: this.transportStrategy,
middlewares: [middleware, ...this.middlewares],
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -157,6 +183,21 @@ export class CacheConfiguration implements Configuration {
transportStrategy:
this.transportStrategy.withClientTimeoutMillis(clientTimeout),
middlewares: this.middlewares,
throwOnErrors: this.throwOnErrors,
});
}

getThrowOnErrors(): boolean {
return this.throwOnErrors;
}

withThrowOnErrors(throwOnErrors: boolean): Configuration {
return new CacheConfiguration({
loggerFactory: this.loggerFactory,
retryStrategy: this.retryStrategy,
transportStrategy: this.transportStrategy,
middlewares: this.middlewares,
throwOnErrors: throwOnErrors,
});
}
}
4 changes: 4 additions & 0 deletions packages/client-sdk-nodejs/src/config/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class Laptop extends CacheConfiguration {
retryStrategy: defaultRetryStrategy(loggerFactory),
transportStrategy: transportStrategy,
middlewares: defaultMiddlewares,
throwOnErrors: false,
});
}
}
Expand Down Expand Up @@ -98,6 +99,7 @@ export class Lambda extends CacheConfiguration {
retryStrategy: defaultRetryStrategy(loggerFactory),
transportStrategy: transportStrategy,
middlewares: defaultMiddlewares,
throwOnErrors: false,
});
}
}
Expand Down Expand Up @@ -138,6 +140,7 @@ class InRegionDefault extends CacheConfiguration {
retryStrategy: defaultRetryStrategy(loggerFactory),
transportStrategy: transportStrategy,
middlewares: defaultMiddlewares,
throwOnErrors: false,
});
}
}
Expand Down Expand Up @@ -179,6 +182,7 @@ class InRegionLowLatency extends CacheConfiguration {
retryStrategy: defaultRetryStrategy(loggerFactory),
transportStrategy: transportStrategy,
middlewares: defaultMiddlewares,
throwOnErrors: false,
});
}
}
Expand Down
37 changes: 37 additions & 0 deletions packages/client-sdk-nodejs/src/config/leaderboard-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export interface LeaderboardConfiguration {
withTransportStrategy(
transportStrategy: TransportStrategy
): LeaderboardConfiguration;

/**
* @returns {boolean} Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
*/
getThrowOnErrors(): boolean;

/**
* Copy constructor for configuring whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
* @param {boolean} throwOnErrors
* @returns {Configuration} a new Configuration object with the specified throwOnErrors setting
*/
withThrowOnErrors(throwOnErrors: boolean): LeaderboardConfiguration;
}

export interface LeaderboardConfigurationProps {
Expand All @@ -37,17 +53,24 @@ export interface LeaderboardConfigurationProps {
* Configures low-level options for network interactions with the Momento service
*/
transportStrategy: TransportStrategy;

/**
* Configures whether the client should return a Momento Error object or throw an exception when an error occurs.
*/
throwOnErrors: boolean;
}

export class LeaderboardClientConfiguration
implements LeaderboardConfiguration
{
private readonly loggerFactory: MomentoLoggerFactory;
private readonly transportStrategy: TransportStrategy;
private readonly throwOnErrors: boolean;

constructor(props: LeaderboardConfigurationProps) {
this.loggerFactory = props.loggerFactory;
this.transportStrategy = props.transportStrategy;
this.throwOnErrors = props.throwOnErrors;
}

getLoggerFactory(): MomentoLoggerFactory {
Expand All @@ -65,6 +88,7 @@ export class LeaderboardClientConfiguration
loggerFactory: this.loggerFactory,
transportStrategy:
this.transportStrategy.withClientTimeoutMillis(clientTimeoutMillis),
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -74,6 +98,19 @@ export class LeaderboardClientConfiguration
return new LeaderboardClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: this.throwOnErrors,
});
}

getThrowOnErrors(): boolean {
return this.throwOnErrors;
}

withThrowOnErrors(throwOnErrors: boolean): LeaderboardConfiguration {
return new LeaderboardClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: this.transportStrategy,
throwOnErrors: throwOnErrors,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Laptop extends LeaderboardClientConfiguration {
return new Laptop({
loggerFactory: loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: false,
});
}
}
34 changes: 34 additions & 0 deletions packages/client-sdk-nodejs/src/config/topic-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export interface TopicConfigurationProps {
* Configures logging verbosity and format
*/
loggerFactory: MomentoLoggerFactory;

/**
* Configures whether the client should return a Momento Error object or throw an exception when an error occurs.
*/
throwOnErrors: boolean;
}

/**
Expand All @@ -18,16 +23,45 @@ export interface TopicConfiguration {
* @returns {MomentoLoggerFactory} the current configuration options for logging verbosity and format
*/
getLoggerFactory(): MomentoLoggerFactory;

/**
* @returns {boolean} Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
*/
getThrowOnErrors(): boolean;

/**
* Copy constructor for configuring whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
* @param {boolean} throwOnErrors
* @returns {Configuration} a new Configuration object with the specified throwOnErrors setting
*/
withThrowOnErrors(throwOnErrors: boolean): TopicConfiguration;
}

export class TopicClientConfiguration implements TopicConfiguration {
private readonly loggerFactory: MomentoLoggerFactory;
private readonly throwOnErrors: boolean;

constructor(props: TopicConfigurationProps) {
this.loggerFactory = props.loggerFactory;
this.throwOnErrors = props.throwOnErrors;
}

getLoggerFactory(): MomentoLoggerFactory {
return this.loggerFactory;
}

getThrowOnErrors(): boolean {
return this.throwOnErrors;
}

withThrowOnErrors(throwOnErrors: boolean): TopicConfiguration {
return new TopicClientConfiguration({
loggerFactory: this.loggerFactory,
throwOnErrors: throwOnErrors,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class Default extends TopicClientConfiguration {
): TopicConfiguration {
return new TopicClientConfiguration({
loggerFactory: loggerFactory,
throwOnErrors: false,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export interface VectorIndexConfiguration {
withTransportStrategy(
transportStrategy: TransportStrategy
): VectorIndexConfiguration;

/**
* @returns {boolean} Configures whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
*/
getThrowOnErrors(): boolean;

/**
* Copy constructor for configuring whether the client should return a Momento Error object or throw an exception when an
* error occurs. By default, this is set to false, and the client will return a Momento Error object on errors. Set it
* to true if you prefer for exceptions to be thrown.
* @param {boolean} throwOnErrors
* @returns {Configuration} a new Configuration object with the specified throwOnErrors setting
*/
withThrowOnErrors(throwOnErrors: boolean): VectorIndexConfiguration;
}

export interface VectorIndexConfigurationProps {
Expand All @@ -37,17 +53,23 @@ export interface VectorIndexConfigurationProps {
* Configures low-level options for network interactions with the Momento service
*/
transportStrategy: TransportStrategy;
/**
* Configures whether the client should return a Momento Error object or throw an exception when an error occurs.
*/
throwOnErrors: boolean;
}

export class VectorIndexClientConfiguration
implements VectorIndexConfiguration
{
private readonly loggerFactory: MomentoLoggerFactory;
private readonly transportStrategy: TransportStrategy;
private readonly throwOnErrors: boolean;

constructor(props: VectorIndexConfigurationProps) {
this.loggerFactory = props.loggerFactory;
this.transportStrategy = props.transportStrategy;
this.throwOnErrors = props.throwOnErrors;
}

getLoggerFactory(): MomentoLoggerFactory {
Expand All @@ -65,6 +87,7 @@ export class VectorIndexClientConfiguration
loggerFactory: this.loggerFactory,
transportStrategy:
this.transportStrategy.withClientTimeoutMillis(clientTimeoutMillis),
throwOnErrors: this.throwOnErrors,
});
}

Expand All @@ -74,6 +97,19 @@ export class VectorIndexClientConfiguration
return new VectorIndexClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: this.throwOnErrors,
});
}

getThrowOnErrors(): boolean {
return this.throwOnErrors;
}

withThrowOnErrors(throwOnErrors: boolean): VectorIndexConfiguration {
return new VectorIndexClientConfiguration({
loggerFactory: this.loggerFactory,
transportStrategy: this.transportStrategy,
throwOnErrors: throwOnErrors,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Laptop extends VectorIndexClientConfiguration {
return new Laptop({
loggerFactory: loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: false,
});
}
}
Expand Down Expand Up @@ -100,6 +101,7 @@ export class Browser extends VectorIndexClientConfiguration {
return new Browser({
loggerFactory: loggerFactory,
transportStrategy: transportStrategy,
throwOnErrors: false,
});
}
}
Loading
Loading