Skip to content

Commit

Permalink
Fix TypeError in plugin error handler, fixes #2528
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoTheThird committed Mar 24, 2022
1 parent 7f6531b commit fe6f6cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
32 changes: 11 additions & 21 deletions src/core/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,18 @@ class PluginIndex {
}

__pluginErrorHandler(name, error) {
const errorJson = {};
if (error.message) {
try {
errorJson = JSON.parse(error.message);
} catch (e) {
errorJson.message = error.message;
if (e instanceof SyntaxError) {
// pass
} else {
this.log.warn(
`Plugin error handler for plugin ${name} failed to parse error message: ${e}`
);
}
}
} else {
this.log.error(
`Plugin error handler for plugin ${name} failed to parse error: ${error}`
);
errorJson.message = error;
try {
error.message = JSON.stringify({
message: JSON.parse(error.message)?.message || error.message,
name
});
} catch (e) {
error.message = JSON.stringify({
message: error.message,
name
});
}
errorJson.name = name;
throw new Error(JSON.stringify(errorJson));
throw error;
}
}

Expand Down
56 changes: 36 additions & 20 deletions src/core/plugins/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,44 @@ describe("PluginIndex", () => {
});
});
describe("__pluginErrorHandler()", () => {
it("should handle json-encoded errors", () => {
const error = new Error("{'my': 'problem'}");
expect(() =>
pluginIndex.__pluginErrorHandler("jsonencoded", error)
).toThrow(
// Make sure the important parts come out in any order: The plugin name
// and the original error message
/^(?=.*jsonencoded)(?=.*{'my': 'problem'}).*$/
);
it("should handle json-encoded errors", done => {
const error = new Error('{"my": "problem"}');
try {
pluginIndex.__pluginErrorHandler("jsonencoded", error);
} catch (e) {
expect(e).toBe(error);
expect(JSON.parse(e.message)).toEqual({
message: '{"my": "problem"}',
name: "jsonencoded"
});
done();
}
});
it("should handle errors that are just strings", () => {
const error = new Error("AAAAAAAAAH");
expect(() => pluginIndex.__pluginErrorHandler("string", error)).toThrow(
'{"message":"AAAAAAAAAH","name":"string"}'
);
it("should handle json-encoded errors with message", done => {
const error = new Error('{"message": "problem"}');
try {
pluginIndex.__pluginErrorHandler("jsonencoded", error);
} catch (e) {
expect(e).toBe(error);
expect(JSON.parse(e.message)).toEqual({
message: "problem",
name: "jsonencoded"
});
done();
}
});
it("should log when it can't handle the error input", () => {
const error = () => {};
expect(() =>
pluginIndex.__pluginErrorHandler("whatTheHeck", error)
).toThrow('{"name":"whatTheHeck"}');
expect(log.error).toHaveBeenCalledTimes(1);
it("should handle string errors", done => {
const error = new Error("problem");
try {
pluginIndex.__pluginErrorHandler("str", error);
} catch (e) {
expect(e).toBe(error);
expect(JSON.parse(e.message)).toEqual({
message: "problem",
name: "str"
});
done();
}
});
});
});

0 comments on commit fe6f6cf

Please sign in to comment.