Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,51 @@ describe('given a store with basic data', () => {
const result = await facade.get(dataKind.features, newFeature.key);
expect(result).toEqual(descriptor);
});

it('handles batchWrite error during init', async () => {
const mockLogger = {
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
};
const state = new DynamoDBClientState(DEFAULT_CLIENT_OPTIONS);
const errorCore = new DynamoDBCore(DEFAULT_TABLE_NAME, state, mockLogger);
const errorFacade = new AsyncCoreFacade(errorCore);

// Mock batchWrite to throw an error
const error = new Error('Batch write failed');
jest.spyOn(state, 'batchWrite').mockRejectedValueOnce(error);

// Should not throw, but should complete the callback
await expect(errorFacade.init([])).resolves.not.toThrow();

// Verify error was logged
expect(mockLogger.error).toHaveBeenCalledWith(`Error writing to DynamoDB: ${error}`);
});

it('handles get error', async () => {
const mockLogger = {
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
};
const state = new DynamoDBClientState(DEFAULT_CLIENT_OPTIONS);
const errorCore = new DynamoDBCore(DEFAULT_TABLE_NAME, state, mockLogger);
const errorFacade = new AsyncCoreFacade(errorCore);

// Mock get to throw an error
const error = new Error('Get failed');
jest.spyOn(state, 'get').mockRejectedValueOnce(error);

// Should return undefined when get fails
const result = await errorFacade.get(dataKind.features, 'some-key');
expect(result).toBeUndefined();

// Verify error was logged with correct namespace and key
expect(mockLogger.error).toHaveBeenCalledWith(`Error reading features:some-key: ${error}`);
});
});

it('it can calculate size', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export default class DynamoDBClientState {
Key: key,
}),
);

return res.Item;
}

Expand Down
25 changes: 17 additions & 8 deletions packages/store/node-server-sdk-dynamodb/src/DynamoDBCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ export default class DynamoDBCore implements interfaces.PersistentDataStore {
// Always write the initialized token when we initialize.
ops.push({ PutRequest: { Item: this._initializedToken() } });

await this._state.batchWrite(this._tableName, ops);
try {
await this._state.batchWrite(this._tableName, ops);
} catch (error) {
this._logger?.error(`Error writing to DynamoDB: ${error}`);
}
callback();
}

Expand All @@ -176,13 +180,18 @@ export default class DynamoDBCore implements interfaces.PersistentDataStore {
key: string,
callback: (descriptor: interfaces.SerializedItemDescriptor | undefined) => void,
) {
const read = await this._state.get(this._tableName, {
namespace: stringValue(this._state.prefixedKey(kind.namespace)),
key: stringValue(key),
});
if (read) {
callback(this._unmarshalItem(read));
} else {
try {
const read = await this._state.get(this._tableName, {
namespace: stringValue(this._state.prefixedKey(kind.namespace)),
key: stringValue(key),
});
if (read) {
callback(this._unmarshalItem(read));
} else {
callback(undefined);
}
} catch (error) {
this._logger?.error(`Error reading ${kind.namespace}:${key}: ${error}`);
callback(undefined);
}
}
Expand Down