Skip to content

Commit

Permalink
Log more robustly when JSON parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Nov 30, 2022
1 parent ca7a668 commit 0430284
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
3 changes: 1 addition & 2 deletions packages/json/src/index.js
Expand Up @@ -24,8 +24,7 @@ export default function json(options = {}) {
};
} catch (err) {
const message = 'Could not parse JSON file';
const position = parseInt(/[\d]/.exec(err.message)[0], 10);
this.warn({ message, id, position });
this.warn({ message, id, cause: err });
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/json/test/test.js
Expand Up @@ -81,11 +81,11 @@ test('handles garbage', async (t) => {
onwarn: (warning) => warns.push(warning)
}).catch(() => {});

const [{ message, id, position, plugin }] = warns;
const [{ message, id, cause, plugin }] = warns;

t.is(warns.length, 1);
t.is(plugin, 'json');
t.is(position, 1);
t.true(cause instanceof SyntaxError);
t.is(message, 'Could not parse JSON file');
t.regex(id, /(.*)bad.json$/);
});
Expand Down

0 comments on commit 0430284

Please sign in to comment.