Skip to content

Better error reporting #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/bin/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ async function main() {
}

main().catch(error => {
if (typeof error === 'string') {
github.setOutput('error', error);
} else if ('message' in error && typeof error.message === 'string') {
if ('stack' in error && typeof error.stack === 'string') {
if (error.stack.includes(error.message)) {
github.setOutput('error', error.stack);
} else {
github.setOutput('error', error.message + '\n' + error.stack);
}
} else {
github.setOutput('error', error.message);
}
} else {
try {
github.setOutput('error', JSON.stringify(error));
} catch {
// ignore
}
}

console.error(error);
process.exit(1);
});
102 changes: 88 additions & 14 deletions src/lib/walker.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,115 @@
import { Root } from 'mdast';
import { Heading, Root } from 'mdast';
import markdown from 'remark-parse';
import stringify from 'remark-stringify';
import { Option, assert } from 'ts-std';
import unified from 'unified';
import { Node, Parent } from 'unist';
import { VFile } from 'vfile';


type Handler<Options> = (this: Walker<Options>, node: Node) => Option<Node> | Promise<Option<Node>>;

function isParent(node: Node): node is Parent {
return Array.isArray((node as Node & { children: unknown }).children);
}

const Printer = unified().use(markdown).use(stringify);

export default class Walker<Options> {
private lastNode: Option<Node> = null;
private lastHeading: Option<Heading> = null;

constructor(protected options: Options, protected file: VFile) { }

[key: string]: unknown;

async walk(root: Node): Promise<Root> {
assert(root.type === 'root', `Cannot walk \`${root.type}\` (must be \`root\`)`);

let result = await this.handle(root);
try {
this.lastNode = null;
this.lastHeading = null;

if (Array.isArray(result)) {
assert(result.length === 1, 'Must return a single root node');
result = result[0];
}
let result = await this.handle(root);

if (result) {
assert(result.type === 'root', 'Must return a root');
return result as Root;
} else {
return {
type: 'root',
children: []
};
if (Array.isArray(result)) {
assert(result.length === 1, 'Must return a single root node');
result = result[0];
}

if (result) {
assert(result.type === 'root', 'Must return a root');
return result as Root;
} else {
return {
type: 'root',
children: []
};
}
} catch (error) {
let message: string = 'Encounted an error';

if (this.file.path) {
message += ` while processing ${this.file.path}`;
}

if (this.lastNode?.position) {
let { line, column } = this.lastNode.position.start;
message += ` at L${line}:C${column}`;
}

if (this.lastHeading) {
try {
let heading = Printer.stringify(this.lastHeading);
message += ` (under the section "${heading}")`;
} catch {
// ignore
}
}

if (typeof error === 'string') {
message += `.\nReason:\n${error}`;
} else if ('message' in error && typeof error.message === 'string') {
if ('stack' in error && typeof error.stack === 'string') {
if (error.stack.includes(error.message)) {
message += `.\nReason:\n${error.stack}`;
} else {
message += `.\nReason:\n${error.message}\n${error.stack}`;
}
} else {
message += `.\nReason:\n${error.message}`;
}
}

let reason: Error | string;

if ('stack' in error && typeof error.stack === 'string') {
reason = new Error(message);

if (error.stack.includes(error.message)) {
reason.stack = error.stack.replace(error.message, message);
} else {
reason.stack = error.stack;
}
} else {
reason = message;
}

// upstream type for reason is wrong: should be Error | string
this.file.fail(reason as string, this.lastNode?.position);
} finally {
this.lastNode = null;
this.lastHeading = null;
}
}

protected async handle(node: Node): Promise<Option<Node> | Node[]> {
this.lastNode = node;

if (node.type === 'heading') {
this.lastHeading = node as Heading;
}

let maybeHandler = this[node.type];

if (typeof maybeHandler === 'function') {
Expand Down
1 change: 1 addition & 0 deletions src/markdown/tutorial/part-1/03-automated-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Let's open the generated test file and replace the boilerplate test with our own
import { setupApplicationTest } from 'super-rentals/tests/helpers';
@@ -7,6 +7,12 @@

-fail!!!
- test('visiting /super-rentals', async function (assert) {
- await visit('/super-rentals');
-
Expand Down