source: frontend/node_modules/webpack/lib/errors/JSONParseError.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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