Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(datasource/custom): better logging for jsonata errors #31520

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions lib/modules/datasource/custom/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as httpMock from '../../../../test/http-mock';
import { fs } from '../../../../test/util';
import { CustomDatasource } from './index';
import { logger } from '../../../logger';

Check failure on line 7 in lib/modules/datasource/custom/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

`../../../logger` import should occur before import of `./index`

jest.mock('../../../util/fs');

Expand Down Expand Up @@ -228,6 +229,31 @@
expect(result).toEqual(expected);
});

it('returns null if transformation using jsonata rules fail', async () => {
httpMock
.scope('https://example.com')
.get('/v1')
.reply(200, '1.0.0 \n2.0.0 \n 3.0.0 ', {
'Content-Type': 'text/plain',
});
const result = await getPkgReleases({
datasource: `${CustomDatasource.id}.foo`,
packageName: 'myPackage',
customDatasources: {
foo: {
defaultRegistryUrlTemplate: 'https://example.com/v1',
transformTemplates: ['$[.name = "Alice" and'],
format: 'plain',
},
},
});
expect(result).toBeNull();
expect(logger.debug).toHaveBeenCalledWith(
{ err: expect.any(Object), transformTemplate: '$[.name = "Alice" and' },
'Error while transforming response',
);
});

it('return releases for plain text API when only returns a single version', async () => {
const expected = {
releases: [
Expand Down
12 changes: 10 additions & 2 deletions lib/modules/datasource/custom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ export class CustomDatasource extends Datasource {
logger.trace({ data }, `Custom manager fetcher '${format}' returned data.`);

for (const transformTemplate of transformTemplates) {
const expression = jsonata(transformTemplate);
data = await expression.evaluate(data);
try {
const expression = jsonata(transformTemplate);
data = await expression.evaluate(data);
} catch (err) {
logger.debug(
{ err, transformTemplate },
'Error while transforming response',
);
return null;
}
}

try {
Expand Down
Loading