Skip to content

Commit

Permalink
heimdall plugin: handle non-json errors, test init
Browse files Browse the repository at this point in the history
  • Loading branch information
UniversalSuperBox committed Apr 17, 2021
1 parent 67301f9 commit 63dab24
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/core/plugins/heimdall/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ class HeimdallPlugin extends Plugin {
.hasAccess()
.then(() => true)
.catch(error => {
var errorJson = JSON.parse(error.message);
try {
var errorJson = JSON.parse(error.message);
} catch (e) {
this.log.warn(`Heimdall returned a non-json error: ${error}`);
throw error;
}
if (errorJson.error.code === 3221225781) {
this.log.warn(
"Heimdall is missing required DLLs: Is Microsoft Visual C++ 2012 x86 redistributable installed?"
Expand Down
47 changes: 45 additions & 2 deletions src/core/plugins/heimdall/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const mainEvent = { emit: jest.fn() };
beforeEach(() => mainEvent.emit.mockReset());
const log = { warn: jest.fn() };
beforeEach(() => {
mainEvent.emit.mockReset();
log.warn.mockReset();
});

const heimdallPlugin = new (require("./plugin.js"))({}, "a", mainEvent);
const heimdallPlugin = new (require("./plugin.js"))({}, "a", mainEvent, log);

describe("heimdall plugin", () => {
describe("kill()", () => {
Expand All @@ -23,4 +27,43 @@ describe("heimdall plugin", () => {
});
});
});
describe("init()", () => {
it("should execute Heimdall", () => {
const hasAccess = jest
.spyOn(heimdallPlugin.heimdall, "hasAccess")
.mockImplementation(() => Promise.resolve());
return heimdallPlugin.init().then(r => {
expect(r).toEqual(true);
expect(hasAccess).toHaveBeenCalledTimes(1);
});
});
it("should emit user:no-msvc2012x86 when heimdall fails with dll error", () => {
const hasAccess = jest
.spyOn(heimdallPlugin.heimdall, "hasAccess")
.mockImplementation(() =>
Promise.reject(new Error('{"error": {"code": 3221225781}}'))
);
jest.spyOn(heimdallPlugin.log, "warn").mockImplementation(() => null);
return heimdallPlugin.init().then(() => {
expect(mainEvent.emit).toHaveBeenCalledTimes(1);
expect(mainEvent.emit).toHaveBeenCalledWith("user:no-msvc2012x86");
});
});
it("should throw when heimdall throws a json error", () => {
const hasAccess = jest
.spyOn(heimdallPlugin.heimdall, "hasAccess")
.mockImplementation(() => Promise.reject(new Error('{"error": {}}')));
jest.spyOn(heimdallPlugin.log, "warn").mockImplementation(() => null);
return expect(heimdallPlugin.init()).rejects.toThrow('{"error": {}}');
});
it("should throw and log a warning when heimdall throws a non-json error somehow", () => {
const hasAccess = jest
.spyOn(heimdallPlugin.heimdall, "hasAccess")
.mockImplementation(() => Promise.reject(new Error("¯\\_(ツ)_/¯")));
jest.spyOn(heimdallPlugin.log, "warn").mockImplementation(() => null);
return expect(heimdallPlugin.init())
.rejects.toThrow("¯\\_(ツ)_/¯")
.then(() => expect(log.warn).toHaveBeenCalledTimes(1));
});
});
});

0 comments on commit 63dab24

Please sign in to comment.