Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const jestConfig = {
// },
// },
rootDir: "./",
testMatch: ["<rootDir>/test/*.js"],
testMatch: ["<rootDir>/test/*.js", "<rootDir>/test/agents/*.js"],
testPathIgnorePatterns: [],
transform: {},
verbose: true,
Expand Down
38 changes: 25 additions & 13 deletions lib/agents/kiesel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import fs from "node:fs";
import * as runtimePath from "../runtime-path.js";
import { ConsoleAgent } from "../ConsoleAgent.js";

const errorRe = /^Uncaught exception: (.+?)(?:: (.+))?$/m;
const ERROR_REGEXP = /^Uncaught exception: (?<name>.+?)(?:: (?<message>.+))?$/m;
const STACK_FRAME_REGEXP = /^ {2}at fn (?<functionName>.+)$/;

class KieselAgent extends ConsoleAgent {
static RUNTIME = fs.readFileSync(runtimePath.for("kiesel"), "utf8");
Expand All @@ -27,30 +28,41 @@ class KieselAgent extends ConsoleAgent {
}

parseError(str) {
const match = str.match(errorRe);

if (!match) {
const errorMatch = str.match(ERROR_REGEXP);
if (!errorMatch) {
return null;
}

const name = match[1] ? match[1].trim() : "";
const message = match[2] ? match[2].trim() : "";
const { name, message = "" } = errorMatch.groups;
const stack = [];

const lines = str.slice(errorMatch[0].length).split(/\r?\n/g);
for (const line of lines) {
const stackFrameMatch = line.match(STACK_FRAME_REGEXP);
if (stackFrameMatch) {
const { functionName } = stackFrameMatch.groups;
stack.push({
source: line,
functionName,
});
}
}

return {
name,
message,
stack: [],
stack,
};
}

normalizeResult(result) {
const match = result.stdout.match(errorRe);

if (match) {
result.stdout = result.stdout.replace(errorRe, "");
result.stderr = match[0];
const errorMatch = ERROR_REGEXP.exec(result.stdout);
if (errorMatch) {
const { index } = errorMatch;
const stdout = result.stdout;
result.stdout = stdout.slice(0, index);
result.stderr = stdout.slice(index);
}

return result;
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/agents/kiesel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import KieselAgent from "../../lib/agents/kiesel.js";

describe("KieselAgent", () => {
describe("normalizeResult", () => {
it("works", () => {
const agent = new KieselAgent();
const result = {
stdout: `ok
Uncaught exception: Error: boom
at fn foo
at fn bar
at fn baz
`,
stderr: "",
error: null,
};

const normalized = agent.normalizeResult(result);

expect(normalized).toEqual({
stdout: `ok
`,
stderr: `Uncaught exception: Error: boom
at fn foo
at fn bar
at fn baz
`,
error: null,
});
});
});

describe("parseError", () => {
it("works", () => {
const agent = new KieselAgent();
const parsed = agent.parseError(`Uncaught exception: Error: boom
at fn foo
at fn bar
at fn baz
`);

expect(parsed).toEqual({
name: "Error",
message: "boom",
stack: [
{
source: " at fn foo",
functionName: "foo",
},
{
source: " at fn bar",
functionName: "bar",
},
{
source: " at fn baz",
functionName: "baz",
},
],
});
});
});
});