How to Manage Configuration and Error Handling in the Momento Node.js SDK
The code below illustrates the simplest way to construct a CacheClient
:
const cacheClient = await CacheClient.create({
credentialProvider: CredentialProvider.fromEnvVar('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});
However, you may pass in a Configuration
object to customize the behavior.
Momento provides several pre-built configurations in the Configurations
module, such as InRegion
(which is configured
with timeouts and connection counts that are appropriate for server-side connectivity from within the same AWS region),
and Lambda
(which is tuned for use in AWS Lambda environments). Here is how you can specify the Lambda
configuration:
const cacheClient = await CacheClient.create({
configuration: Configurations.Lambda.latest(),
credentialProvider: CredentialProvider.fromEnvVar('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});
If you omit the configuration, Momento will use the Laptop
configuration by default. This configuration has relaxed
timeouts, suitable for development or in high-latency environments. (It is not recommended for production, server-side use.)
For more information about Configuration
objects see SDK Configuration Objects.
By default, CacheClient
errors are surfaced to developers as part of the return values of the calls, as opposed to throwing
exceptions. This makes errors more visible when you're writing your code, and allows your IDE to be more helpful in ensuring
you've handled the errors you care about.
Here's an example of how to check for errors on a get
call:
const result = await cacheClient.get('test-cache', 'test-key');
switch (result.type) {
case CacheGetResponse.Hit:
console.log(`Retrieved value for key 'test-key': ${result.valueString()}`);
break;
case CacheGetResponse.Miss:
console.log("Key 'test-key' was not found in cache 'test-cache'");
break;
case CacheGetResponse.Error:
throw new Error(
`An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${result.errorCode()}: ${result.toString()}`
);
}
However, if you prefer for exceptions to be thrown, you can configure the CacheClient
to do so:
const cacheClient = await CacheClient.create({
configuration: Configurations.Lambda.latest().withThrowOnErrors(true),
credentialProvider: CredentialProvider.fromEnvVar('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});
With this configuration setting, any errors that occur will result in an instance of SdkError
being thrown. You may
catch it and use its .errorCode()
method to determine the specific error that occurred:
try {
const result = (await cacheClient.get('test-cache', 'test-key')).value();
if (result !== undefined) {
console.log(`Retrieved value for key 'test-key': ${result}`);
} else {
console.log("Key 'test-key' was not found in cache 'test-cache'");
}
} catch (e) {
const momentoError = e as SdkError;
if (momentoError.errorCode() === MomentoErrorCode.LIMIT_EXCEEDED_ERROR) {
console.log('Request rate limit exceeded, may need to request a limit increase!');
} else {
throw new Error(
`An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${momentoError.errorCode()}: ${momentoError.toString()}`
);
}
}
For more information about error handling in Momento see SDK Error Handling.