You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
617 B

4 years ago
  1. 'use strict';
  2. const errorEx = require('error-ex');
  3. const fallback = require('json-parse-better-errors');
  4. const JSONError = errorEx('JSONError', {
  5. fileName: errorEx.append('in %s')
  6. });
  7. module.exports = (input, reviver, filename) => {
  8. if (typeof reviver === 'string') {
  9. filename = reviver;
  10. reviver = null;
  11. }
  12. try {
  13. try {
  14. return JSON.parse(input, reviver);
  15. } catch (err) {
  16. fallback(input, reviver);
  17. throw err;
  18. }
  19. } catch (err) {
  20. err.message = err.message.replace(/\n/g, '');
  21. const jsonErr = new JSONError(err);
  22. if (filename) {
  23. jsonErr.fileName = filename;
  24. }
  25. throw jsonErr;
  26. }
  27. };