Skip to content

Commit 7676df6

Browse files
committed
Better error reporting
1 parent c566b4e commit 7676df6

File tree

5 files changed

+165
-98
lines changed

5 files changed

+165
-98
lines changed

package-lock.json

Lines changed: 94 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"sass-loader": "^11.0.1",
5050
"strip-indent": "^3.0.0",
5151
"style-loader": "^2.0.0",
52+
"verror": "^1.10.0",
5253
"webpack": "^5.28.0"
5354
},
5455
"peerDependencies": {

src/bib-renderer.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chalk = require('chalk');
66
const _ = require('lodash');
77
const stripIndent = require('strip-indent');
88
const bibtex = require('@retorquere/bibtex-parser');
9+
const { MultiError } = require('verror');
910

1011
const { PublistStrictAbort } = require('./consts');
1112

@@ -35,19 +36,19 @@ async function allSettled(promises) {
3536
}
3637

3738
function reportErrors(ctx, errors) {
38-
let hasError = false;
39+
let bibErrors = [];
3940
for (const err of errors) {
4041
if (err instanceof BibRendererError) {
41-
hasError = true;
4242
for (const inner of err.errors) {
4343
ctx.log.error(inner);
4444
}
45+
bibErrors.push(err);
4546
} else {
4647
// re-raise anything else, which should be fatal.
4748
throw err;
4849
}
4950
}
50-
return hasError;
51+
return bibErrors;
5152
}
5253

5354
class BibtexParseError extends Error {
@@ -93,20 +94,20 @@ class BibRendererError extends Error {
9394
}
9495

9596
async function bibRenderer(ctx, opts, { path, text }) {
96-
let hasError = false;
97+
let bibErrors = [];
9798
path = pathFn.relative(ctx.source_dir, path);
9899

99100
// parse content as bibtex
100101
const [ entries, errors ] = await parseBibEntries(ctx, opts, { path, text });
101-
hasError |= reportErrors(ctx, errors);
102+
bibErrors.push(...reportErrors(ctx, errors));
102103

103104
// construct list of items
104105
const [items, itemErrors] = await allSettled(entries.map(entry => itemFromEntry(ctx, opts, entry)));
105-
hasError |= reportErrors(ctx, itemErrors);
106+
bibErrors.push(...reportErrors(ctx, itemErrors));
106107

107-
if (hasError) {
108+
if (bibErrors.length > 0) {
108109
if (opts.strict) {
109-
throw new PublistStrictAbort(path);
110+
throw new PublistStrictAbort(path, new MultiError(bibErrors));
110111
} else {
111112
ctx.log.warn(`${path}: there were errors while loading, bib entries may be incomplete.`);
112113
}

src/consts.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const pathFn = require('path');
4+
const { VError, WError } = require('verror');
45

56
module.exports.SELF = pathFn.resolve(__dirname, '..');
67
module.exports.TEMPLATE_DIR = pathFn.resolve(__dirname, '../templates');
@@ -22,20 +23,26 @@ module.exports.DEFAULT_INSTOPTS = {
2223
venues: {},
2324
}
2425

25-
class PublistStrictAbort extends Error {
26-
constructor(file) {
27-
super(`'${file}': aborting because there were errors and the strict mode is enabled`);
28-
this.name = 'PublistStrictAbort';
29-
Error.captureStackTrace(this, PublistStrictAbort);
26+
class PublistStrictAbort extends VError {
27+
constructor(file, cause, info) {
28+
super({
29+
name: 'PublistStrictAbort',
30+
cause,
31+
info,
32+
constructorOpt: PublistStrictAbort,
33+
}, `'${file}': aborting because there were errors and the strict mode is enabled`);
3034
}
3135
}
3236
module.exports.PublistStrictAbort = PublistStrictAbort;
3337

34-
class PublistWebpackError extends Error {
35-
constructor() {
36-
super(`Aborting because there were errors when webpacking`);
37-
this.name = 'PublistWebpackError';
38-
Error.captureStackTrace(this, PublistWebpackError);
38+
class PublistWebpackError extends WError {
39+
constructor(cause, info) {
40+
super({
41+
name: 'PublistWebpackError',
42+
cause,
43+
info,
44+
constructorOpt: PublistWebpackError,
45+
}, `Aborting because there were errors when webpacking`);
3946
}
4047
}
4148
module.exports.PublistWebpackError = PublistWebpackError;

0 commit comments

Comments
 (0)