UNPKG

1.16 kBJavaScriptView Raw
1import { parse, ParseErrorCode } from '@sqs/jsonc-parser/lib/main';
2import { asError, createAggregateError } from './errors';
3/**
4 * Parses the JSON input using an error-tolerant "JSONC" parser. If an error occurs, it is returned as a value
5 * instead of being thrown. This is useful when input is parsed in the background (not in response to any specific
6 * user action), because it makes it easier to save the error and only show it to the user when it matters (for
7 * some interactive user action).
8 */
9export function parseJSONCOrError(input) {
10 try {
11 return parseJSON(input);
12 }
13 catch (err) {
14 return asError(err);
15 }
16}
17/**
18 * Parses the JSON input using an error-tolerant "JSONC" parser.
19 */
20function parseJSON(input) {
21 const errors = [];
22 const o = parse(input, errors, { allowTrailingComma: true, disallowComments: false });
23 if (errors.length > 0) {
24 throw createAggregateError(errors.map(v => (Object.assign({}, v, { code: ParseErrorCode[v.error], message: `Configuration parse error, code: ${v.error} (offset: ${v.offset}, length: ${v.length})` }))));
25 }
26 return o;
27}
28//# sourceMappingURL=util.js.map
\No newline at end of file