Skip to content

Commit fb99a49

Browse files
hanslalexeagle
authored andcommitted
feat(@angular-devkit/core): add a parseJsonFile that shows file path on error
1 parent 75d682b commit fb99a49

File tree

1 file changed

+43
-4
lines changed
  • packages/angular_devkit/core/src/json

1 file changed

+43
-4
lines changed

packages/angular_devkit/core/src/json/parser.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,47 @@ import {
2525
Position,
2626
} from './interface';
2727

28+
export class JsonException extends BaseException {}
2829

2930
/**
3031
* A character was invalid in this context.
3132
*/
32-
export class InvalidJsonCharacterException extends BaseException {
33+
export class InvalidJsonCharacterException extends JsonException {
34+
invalidChar: string;
35+
line: number;
36+
character: number;
37+
offset: number;
38+
3339
constructor(context: JsonParserContext) {
3440
const pos = context.previous;
35-
super(`Invalid JSON character: ${JSON.stringify(_peek(context))} `
36-
+ `at ${pos.line}:${pos.character}.`);
41+
const invalidChar = JSON.stringify(_peek(context));
42+
super(`Invalid JSON character: ${invalidChar} at ${pos.line}:${pos.character}.`);
43+
44+
this.invalidChar = invalidChar;
45+
this.line = pos.line;
46+
this.offset = pos.offset;
47+
this.character = pos.character;
3748
}
3849
}
3950

4051

4152
/**
4253
* More input was expected, but we reached the end of the stream.
4354
*/
44-
export class UnexpectedEndOfInputException extends BaseException {
55+
export class UnexpectedEndOfInputException extends JsonException {
4556
constructor(_context: JsonParserContext) {
4657
super(`Unexpected end of file.`);
4758
}
4859
}
4960

61+
/**
62+
* An error happened within a file.
63+
*/
64+
export class PathSpecificJsonException extends JsonException {
65+
constructor(public path: string, public exception: JsonException) {
66+
super(`An error happened at file path ${JSON.stringify(path)}: ${exception.message}`);
67+
}
68+
}
5069

5170
/**
5271
* Context passed around the parser with information about where we currently are in the parse.
@@ -860,3 +879,23 @@ export function parseJson(input: string, mode = JsonParseMode.Default): JsonValu
860879

861880
return parseJsonAst(input, mode).value;
862881
}
882+
883+
/**
884+
* Parse a JSON string into its value. This discards the AST and only returns the value itself.
885+
* It also absorbs JSON parsing errors and return a new error with the path in it. Useful for
886+
* showing errors when parsing from a file.
887+
* @param input The string to parse.
888+
* @param mode The mode to parse the input with. {@see JsonParseMode}.
889+
* @returns {JsonValue} The value represented by the JSON string.
890+
*/
891+
export function parseJsonFile(input: string, mode = JsonParseMode.Default, path: string) {
892+
try {
893+
return parseJson(input, mode);
894+
} catch (e) {
895+
if (e instanceof JsonException) {
896+
throw new PathSpecificJsonException(path, e);
897+
} else {
898+
throw e;
899+
}
900+
}
901+
}

0 commit comments

Comments
 (0)