Skip to content

Commit

Permalink
Improve handling of null software and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Fried Hoeben committed Dec 12, 2023
1 parent 32b9b88 commit 617e05c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class DiscoveryMutationHelper {
const ci = this.createCi(asset, mappedProduct);
mappedProduct.configurationItems.push(ci);
} catch (e) {
console.error(`Error processing: ${key}`);
throw new LoggedError(e);
const msg = `Error processing: ${key}`;
console.error('%s.\nAsset:\n%j', msg, asset);
console.info(e);
throw new LoggedError(msg);
}
}

Expand Down Expand Up @@ -159,11 +161,12 @@ class DiscoveryMutationHelper {
}

mapSoftware(softwares) {
return this.mapSoftwareName(softwares.map(s => s.name));
return this.mapSoftwareName(softwares.map(s => s && s.name));
}

mapSoftwareName(softwareNames) {
return softwareNames
.filter(n => !!n)
.map(n => this.lansweeperHelper.cleanupName(n))
.map(n => this.referenceData.softwareCis.get(n))
.filter(id => !!id);
Expand Down
4 changes: 3 additions & 1 deletion lansweeper/aws/integration-lambda/lansweeper_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class LansweeperIntegration {
// no need to keep process going
throw e;
}
console.error(e);
if (!(e instanceof LoggedError)) {
console.error(e);
}
errors.push(`Unable to upload assets to 4me.`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const DiscoveryMutationHelper = require('../discovery_mutation_helper');
const assetArray = require('./assets/asset_array.json');
const LansweeperHelper = require('../lansweeper_helper');
const LoggedError = require('../../../../library/helpers/errors/logged_error');
const referenceData = {
softwareCis: new Map(),
users: new Map(),
Expand Down Expand Up @@ -258,3 +259,28 @@ it('maps stateNames', () => {

expect(helper.mapState('foo')).toEqual('installed');
});

it('handles null software elements', () => {
const helper = new DiscoveryMutationHelper(referenceData, false, []);
const asset = assetArray[0];
asset.softwares = [undefined];

expect(() => helper.toDiscoveryUploadInput('a', [asset])).not.toThrow();
});

it('logs errors', () => {
const helper = new DiscoveryMutationHelper(null, false, []);
const asset = assetArray[0];
asset.softwares = [undefined];
const consoleErrorSpy = jest.spyOn(console, 'error');
const infoCalls = [];
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation((a) => infoCalls.push(a));

expect(() => helper.toDiscoveryUploadInput('a', [asset])).toThrow(LoggedError);

expect(consoleErrorSpy).toHaveBeenCalledWith('%s.\nAsset:\n%j',
'Error processing: MT1Bc3NldC02023hZWZiZi1mMjYzLTRkOGQtOGMzOC04OGRhZTQ2MTA3NWN=',
asset);
expect(consoleInfoSpy).toHaveBeenCalled();
expect(infoCalls[0].message).toEqual("Cannot read properties of null (reading 'users')");
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const TimeHelper = require('../../../../library/helpers/time_helper');
const LansweeperAuthorizationError = require('../errors/lansweeper_authorization_error');
const Js4meAuthorizationError = require('../../../../library/helpers/errors/js_4me_authorization_error');
const LansweeperGraphQLError = require('../errors/lansweeper_graphql_error');
const LoggedError = require('../../../../library/helpers/errors/logged_error');
jest.mock('../../../../library/helpers/time_helper');

describe('processSite', () => {
Expand Down Expand Up @@ -118,6 +119,76 @@ describe('processSite', () => {
expect(result).toEqual({uploadCount: uploadCount});
});

it('handles logged error preparing upload', async () => {
const siteId = 'abddda';
TimeHelper.mockImplementation(() => ({
getMsSinceEpoch: () => Date.UTC(2021, 8, 29, 7, 12, 33),
}));

DiscoveryMutationHelper.mockImplementation((referenceData, generateLabels) => {
expect(generateLabels).toBe(false);
return ({
toDiscoveryUploadInput: () => { throw new LoggedError('broken') },
});
});

LansweeperClient.mockImplementationOnce(() => ({
getSiteName: async (id) => {
expect(id).toBe(siteId);
return 'site 2';
},
getAssetsPaged: async (id, cutOffDate, handler) => {
expect(id).toBe(siteId);
return await handler(assetArray);
},
}));

const consoleErrorSpy = jest.spyOn(console, 'error');
const integration = new LansweeperIntegration('client id',
'secret',
'refresh token',
null);

const result = await integration.processSite(siteId, false, false, installations[0], installations, null);
expect(result).toEqual({errors: ['Unable to upload assets to 4me.']});
expect(consoleErrorSpy).not.toHaveBeenCalled();
});

it('handles non-logged error preparing upload', async () => {
const siteId = 'sadsa';
TimeHelper.mockImplementation(() => ({
getMsSinceEpoch: () => Date.UTC(2021, 8, 29, 7, 12, 33),
}));

DiscoveryMutationHelper.mockImplementation((referenceData, generateLabels) => {
expect(generateLabels).toBe(false);
return ({
toDiscoveryUploadInput: () => { throw new Error('broken') },
});
});

LansweeperClient.mockImplementationOnce(() => ({
getSiteName: async (id) => {
expect(id).toBe(siteId);
return 'site 2';
},
getAssetsPaged: async (id, cutOffDate, handler) => {
expect(id).toBe(siteId);
return await handler(assetArray);
},
}));
const consoleErrorSpy = jest.spyOn(console, 'error');

const integration = new LansweeperIntegration('client id',
'secret',
'refresh token',
null);

const result = await integration.processSite(siteId, false, false, installations[0], installations, null);
expect(result).toEqual({errors: ['Unable to upload assets to 4me.']});
expect(consoleErrorSpy).toHaveBeenCalled();
});

it('handles error on first page upload', async () => {
const siteId = 'abddda';
TimeHelper.mockImplementation(() => ({
Expand Down

0 comments on commit 617e05c

Please sign in to comment.