Skip to content

Commit

Permalink
chore(value-service): handle configuration error and write
Browse files Browse the repository at this point in the history
Value service is biased to writing a value even if configuration cannot be retrieved. This means that definition schema is a best effort.
  • Loading branch information
tzuge committed Oct 7, 2024
1 parent 6a45953 commit 174791c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
35 changes: 35 additions & 0 deletions apps/value-service/src/values/router/value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,41 @@ describe('event router', () => {
expect(res.send).toHaveBeenCalledWith(expect.arrayContaining([]));
expect(next).not.toHaveBeenCalled();
});

it('can write value on configuration error', async () => {
const req = {
tenantId,
user: {
tenantId,
id: 'test-reader',
roles: [ServiceUserRoles.Reader],
},
params: { namespace: 'test-service', name: 'test-value' },
query: {},
body: {},
getConfiguration: jest.fn(),
};
const res = {
send: jest.fn(),
};
const next = jest.fn();

req.getConfiguration.mockRejectedValueOnce(new Error('oh noes!'));

const value = {};
repositoryMock.writeValue.mockResolvedValueOnce({
tenantId,
timestamp: new Date(),
context: {},
correlationId: null,
value,
});

const handler = writeValue(loggerMock, eventServiceMock, repositoryMock);
await handler(req as unknown as Request, res as unknown as Response, next);
expect(eventServiceMock.send).not.toHaveBeenCalled();
expect(res.send).toHaveBeenCalledWith(expect.objectContaining({ value }));
});
});

describe('readMetrics', () => {
Expand Down
16 changes: 15 additions & 1 deletion apps/value-service/src/values/router/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,21 @@ export function writeValue(logger: Logger, eventService: EventService, repositor
user: `${user.name} (ID: ${user.id})`,
});

const [namespaces] = await req.getConfiguration<Record<string, NamespaceEntity>>(tenantId);
let namespaces: Record<string, NamespaceEntity>;
try {
[namespaces] = await req.getConfiguration<Record<string, NamespaceEntity>>(tenantId);
} catch (err) {
// Catch and log error on configuration retrieval.
// The value service is biased to writing the value even if the definition retrieval fails.
logger.warn(
`Error encountered retrieving value configuration; value will be written without definition: ${err}`,
{
context: 'value-router',
tenantId: tenantId?.toString(),
user: `${user.name} (ID: ${user.id})`,
}
);
}

const results = [];
const valuesToWrite = Array.isArray(req.body) ? req.body : [req.body];
Expand Down

0 comments on commit 174791c

Please sign in to comment.