JSONParseError.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Error, EXPECTED_ANY, string]>} ObjectDeserializerContext */
  6. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Error, EXPECTED_ANY, string]>} ObjectSerializerContext */
  7. const makeSerializable = require("../util/makeSerializable");
  8. const CONTEXT = 20;
  9. class JSONParseError extends SyntaxError {
  10. /**
  11. * @param {Error} err err
  12. * @param {EXPECTED_ANY} raw raw
  13. * @param {string} txt text
  14. */
  15. constructor(err, raw, txt) {
  16. let originalMessage = err.message;
  17. /** @type {string} */
  18. let message;
  19. /** @type {number} */
  20. let position;
  21. if (typeof raw !== "string") {
  22. message = `Cannot parse ${Array.isArray(raw) && raw.length === 0 ? "an empty array" : String(raw)}`;
  23. position = 0;
  24. } else if (!txt) {
  25. message = `${originalMessage} while parsing empty string`;
  26. position = 0;
  27. } else {
  28. // Node 20 puts single quotes around the token and a comma after it
  29. const UNEXPECTED_TOKEN = /^Unexpected token '?(.)'?(,)? /i;
  30. const badTokenMatch = originalMessage.match(UNEXPECTED_TOKEN);
  31. const badIndexMatch = originalMessage.match(/ position\s+(\d+)/i);
  32. if (badTokenMatch) {
  33. const h = badTokenMatch[1].charCodeAt(0).toString(16).toUpperCase();
  34. const hex = `0x${h.length % 2 ? "0" : ""}${h}`;
  35. originalMessage = originalMessage.replace(
  36. UNEXPECTED_TOKEN,
  37. `Unexpected token ${JSON.stringify(badTokenMatch[1])} (${hex})$2 `
  38. );
  39. }
  40. /** @type {number | undefined} */
  41. let errIdx;
  42. if (badIndexMatch) {
  43. errIdx = Number(badIndexMatch[1]);
  44. } else if (
  45. // doesn't happen in Node 22+
  46. /^Unexpected end of JSON.*/i.test(originalMessage)
  47. ) {
  48. errIdx = txt.length - 1;
  49. }
  50. if (errIdx === undefined) {
  51. message = `${originalMessage} while parsing '${txt.slice(0, CONTEXT * 2)}'`;
  52. position = 0;
  53. } else {
  54. const start = errIdx <= CONTEXT ? 0 : errIdx - CONTEXT;
  55. const end =
  56. errIdx + CONTEXT >= txt.length ? txt.length : errIdx + CONTEXT;
  57. const slice = `${start ? "..." : ""}${txt.slice(start, end)}${end === txt.length ? "" : "..."}`;
  58. message = `${originalMessage} while parsing ${txt === slice ? "" : "near "}${JSON.stringify(slice)}`;
  59. position = errIdx;
  60. }
  61. }
  62. super(message);
  63. /** @type {string} */
  64. this.name = "JSONParseError";
  65. /** @type {string | undefined} */
  66. this.stack = undefined;
  67. /** @type {Error} */
  68. this.systemError = err;
  69. /** @type {EXPECTED_ANY} */
  70. this.raw = raw;
  71. /** @type {string} */
  72. this.txt = txt;
  73. /** @type {number} */
  74. this.position = position;
  75. }
  76. /**
  77. * Serializes this instance into the provided serializer context.
  78. * @param {ObjectSerializerContext} context context
  79. */
  80. serialize(context) {
  81. context.write(this.systemError).write(this.raw).write(this.txt);
  82. }
  83. /**
  84. * Restores this instance from the provided deserializer context.
  85. * @param {ObjectDeserializerContext} context context
  86. * @returns {JSONParseError} DelegatedModule
  87. */
  88. static deserialize(context) {
  89. const systemError = context.read();
  90. const c1 = context.rest;
  91. const raw = c1.read();
  92. const c2 = c1.rest;
  93. return new JSONParseError(systemError, raw, c2.read());
  94. }
  95. }
  96. makeSerializable(JSONParseError, "webpack/lib/errors/JSONParseError");
  97. module.exports = JSONParseError;