Skip to content

Commit

Permalink
Merge pull request #24 from janis-commerce/JCN-464-validate-invoker-d…
Browse files Browse the repository at this point in the history
…iscovery

Validate invoker response
  • Loading branch information
manuelvilche authored Jan 12, 2024
2 parents 4761c37 + 07d41ad commit 64d77bc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lib/discovery.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { Invoker } = require('@janiscommerce/lambda');
const MicroServiceCallError = require('./microservice-call-error');

let cache = {};

Expand All @@ -12,7 +13,22 @@ module.exports = class Discovery {

if(!cache[cacheKey]) {

const { payload: { baseUrl, path, method: httpMethod } } = await Invoker.serviceCall('discovery', 'GetEndpoint', { service, namespace, method });
const {
payload: { baseUrl, path, method: httpMethod, errorMessage },
functionError
} = await Invoker.serviceCall('discovery', 'GetEndpoint', { service, namespace, method });

const errorMsg = errorMessage || functionError;

if(errorMsg)
throw new MicroServiceCallError(`Service Discovery fails getting endpoint. Error: ${errorMsg}`, MicroServiceCallError.codes.DISCOVERY_ERROR);

if(!baseUrl || !path || !httpMethod) {
throw new MicroServiceCallError(
`Could not get base url, path or method. Base url: ${baseUrl}, path: ${path}, method: ${httpMethod}`,
MicroServiceCallError.codes.DISCOVERY_ERROR
);
}

const endpoint = `${baseUrl}${path}`;

Expand Down
3 changes: 2 additions & 1 deletion lib/microservice-call-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = class MicroServiceCallError extends Error {
return {
MICROSERVICE_FAILED: 2,
REQUEST_LIB_ERROR: 3,
JANIS_SECRET_MISSING: 4
JANIS_SECRET_MISSING: 4,
DISCOVERY_ERROR: 5
};
}

Expand Down
56 changes: 54 additions & 2 deletions tests/microservice-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ describe('MicroService call', () => {
Discovery.cleanCache();
});

const getEndpointStub = result => {
const getEndpointStub = (result, functionError = false) => {

sinon.stub(Invoker, 'serviceCall')
.resolves({ payload: result });
.resolves({
payload: result,
...functionError && { functionError }
});
};

const assertGetEndpoint = (service, namespace, method) => {
Expand Down Expand Up @@ -1428,4 +1431,53 @@ describe('MicroService call', () => {

});

describe('Discovery service fails', () => {

it('Should throw an error if it has an error message', async () => {

getEndpointStub({
errorMessage: 'An error occurred'
});

await assert.rejects(() => ms.call('sample-service', 'sample-entity', 'list'), {
name: 'MicroServiceCallError',
code: MicroServiceCallError.codes.DISCOVERY_ERROR,
message: 'Service Discovery fails getting endpoint. Error: An error occurred'
});

assertGetEndpoint('sample-service', 'sample-entity', 'list');
});

it('Should throw an error if it has functionError property', async () => {

getEndpointStub({}, 'Could not execute lambda function');

await assert.rejects(() => ms.call('sample-service', 'sample-entity', 'list'), {
name: 'MicroServiceCallError',
code: MicroServiceCallError.codes.DISCOVERY_ERROR,
message: 'Service Discovery fails getting endpoint. Error: Could not execute lambda function'
});

assertGetEndpoint('sample-service', 'sample-entity', 'list');
});

it('Should throw an error if it has no base url, path or method', async () => {

const baseUrl = 'https://sample-service.janis-test.in';
const path = '/api/sample-entity';

getEndpointStub({
baseUrl,
path
});

await assert.rejects(() => ms.call('sample-service', 'sample-entity', 'list'), {
name: 'MicroServiceCallError',
code: MicroServiceCallError.codes.DISCOVERY_ERROR,
message: `Could not get base url, path or method. Base url: ${baseUrl}, path: ${path}, method: undefined`
});

assertGetEndpoint('sample-service', 'sample-entity', 'list');
});
});
});

0 comments on commit 64d77bc

Please sign in to comment.