Skip to content

Commit 826a76e

Browse files
committed
refactor: JSON parse error message formatting.
- Refactored the JSON parse error messages in `constructJsonParseErrorMsg` util function to include the original error name, as well as the formatted name, just to ensure the errors have a proper error code for debugging.
1 parent 8564517 commit 826a76e

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

src/utils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,18 @@ function constructJsonParseErrorMsg(filepath: string, fileContent: string, jsonE
7979
return jsonErrors
8080
.map((err, i) => {
8181
// Get the error name from the numeric error code.
82-
// The name is PascalCased, so we need to format it by adding spaces
82+
const errorName = jsonc.printParseErrorCode(err.error);
83+
84+
// The error name is PascalCased, so we need to format it by adding spaces
8385
// before capital letters for readability.
84-
const errorName = jsonc
85-
.printParseErrorCode(err.error)
86-
.replace(/([A-Z])/g, " $1")
87-
.trim();
86+
const errorNameFormatted = errorName.replace(/([A-Z])/g, " $1").trim();
8887

8988
// Calculate line and column numbers from the error offset.
9089
const lineNumber = fileContent.substring(0, err.offset).split("\n").length;
9190
const columnNumber = err.offset - fileContent.lastIndexOf("\n", err.offset - 1);
9291

9392
// Return the formatted error message.
94-
return `\tError ${i + 1} - ${errorName} at "${filepath}:${lineNumber}:${columnNumber}"\n`;
93+
return `\tError ${i + 1} [${errorName}] - ${errorNameFormatted} at "${filepath}:${lineNumber}:${columnNumber}"\n`;
9594
})
9695
.join("\n");
9796
}

0 commit comments

Comments
 (0)