From b237a9ba85321d9c0fb86db3346e12de61703a60 Mon Sep 17 00:00:00 2001 From: Sam Kelleher Date: Mon, 16 Nov 2020 13:18:36 +0000 Subject: [PATCH] fix: Don't totally fail if cannot load MongoDB version. Signed-off-by: Sam Kelleher --- package-lock.json | 20 +++++++++---------- package.json | 1 + src/queries/systemInformation.js | 16 ++++++++++++--- src/queries/systemInformation.test.js | 28 +++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 775a3cb..46c2af2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3061,12 +3061,12 @@ "dev": true }, "bunyan": { - "version": "1.8.12", - "resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.12.tgz", - "integrity": "sha1-8VDw9nSKvdcq6uhPBEA74u8RN5c=", + "version": "1.8.14", + "resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.14.tgz", + "integrity": "sha512-LlahJUxXzZLuw/hetUQJmRgZ1LF6+cr5TPpRj6jf327AsiIq2jhYEH4oqUUkVKTor+9w2BT3oxVwhzE5lw9tcg==", "requires": { "dtrace-provider": "~0.8", - "moment": "^2.10.6", + "moment": "^2.19.3", "mv": "~2", "safe-json-stringify": "~1" } @@ -7944,9 +7944,9 @@ "dev": true }, "moment": { - "version": "2.26.0", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.26.0.tgz", - "integrity": "sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==" + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" }, "mongo-object": { "version": "0.1.4", @@ -7978,9 +7978,9 @@ } }, "nan": { - "version": "2.14.1", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.1.tgz", - "integrity": "sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw==", + "version": "2.14.2", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz", + "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==", "optional": true }, "nanomatch": { diff --git a/package.json b/package.json index 85889a0..a70ab43 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "sideEffects": false, "dependencies": { "@reactioncommerce/api-utils": "^1.14.2", + "@reactioncommerce/logger": "^1.1.3", "@reactioncommerce/reaction-error": "^1.0.1" }, "devDependencies": { diff --git a/src/queries/systemInformation.js b/src/queries/systemInformation.js index 306a3dc..63c41b2 100644 --- a/src/queries/systemInformation.js +++ b/src/queries/systemInformation.js @@ -1,3 +1,5 @@ +import Logger from "@reactioncommerce/logger"; + /** * @name queries.systemInformation * @method @@ -12,12 +14,20 @@ export default async function systemInformation(context, shopId) { await context.validatePermissions(`reaction:legacy:shops:${shopId}`, "read", { shopId }); - const mongoAdmin = await db.admin(); - const mongoInfo = await mongoAdmin.serverStatus(); + let mongoVersion = ""; + + try { + const mongoAdmin = await db.admin(); + const mongoInfo = await mongoAdmin.serverStatus(); + mongoVersion = mongoInfo.version; + } catch (error) { + Logger.error(error); + } + const plugins = Object.values(context.app.registeredPlugins).filter((plugin) => plugin.version); return { apiVersion: context.appVersion, - mongoVersion: { version: mongoInfo.version }, + mongoVersion: { version: mongoVersion }, plugins: plugins.map(({ name, version }) => ({ name, version })) }; } diff --git a/src/queries/systemInformation.test.js b/src/queries/systemInformation.test.js index 96bee1e..a5abed9 100644 --- a/src/queries/systemInformation.test.js +++ b/src/queries/systemInformation.test.js @@ -1,7 +1,12 @@ import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js"; import ReactionError from "@reactioncommerce/reaction-error"; +import Logger from "@reactioncommerce/logger"; import systemInformation from "./systemInformation.js"; +jest.mock("@reactioncommerce/logger", () => ({ + error: jest.fn().mockImplementationOnce(() => {}) +})); + const fakeShopId = "FAKE_SHOP_ID"; mockContext.appVersion = "1.2.3"; mockContext.validatePermissions = jest.fn("validatePermissions"); @@ -65,3 +70,26 @@ test("returns system info if user has permission", async () => { return expect(result).toEqual(expectedResult); }); + +test("returns plugins even when mongoVersion fails to load", async () => { + const expectedResult = { + apiVersion: "1.2.3", + mongoVersion: { version: "" }, + plugins: [ + { name: "plugin-a", version: "1.0.0" }, + { name: "plugin-b", version: "1.1.0" }, + { name: "plugin-c", version: "1.0.2" }, + { name: "plugin-d", version: "2.0.0" } + ] + }; + + mockContext.validatePermissions.mockReturnValueOnce(Promise.resolve(undefined)); + mockContext.app.db.admin.mockReturnValueOnce(Promise.resolve(mongoAdmin)); + mongoAdmin.serverStatus.mockRejectedValueOnce(new Error("Simulate serverStatus() error.")); + const simulatedError = new Error("Simulate serverStatus() error."); + + const result = await systemInformation(mockContext, fakeShopId); + + expect(result).toEqual(expectedResult); + expect(Logger.error).toHaveBeenCalledWith(simulatedError); +});