All files / json / _common.ts

100.00% Branches 3/3
100.00% Lines 9/9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 
 
 
x8
x165
x165
x165
 
x169
x169
x169
 
x169
x165













// Copyright 2018-2025 the Deno authors. MIT license.
import type { JsonValue } from "./types.ts";

/** JSON.parse with detailed error message. */
export function parse(text: string): JsonValue {
  try {
    return JSON.parse(text);
  } catch (error) {
    // Truncate the string so that it is within 30 lengths.
    const truncatedText = 30 < text.length ? `${text.slice(0, 30)}...` : text;
    throw new ((error as Error).constructor as ErrorConstructor)(
      `${(error as Error).message} (parsing: '${truncatedText}')`,
    );
  }
}