UNPKG

@shopify/cli-kit

Version:

A set of utilities, interfaces, and models that are common across all the platform features

28 lines 922 B
import { AbortError } from '../node/error.js'; /** * Safely parse JSON with helpful error messages. * * @param jsonString - The JSON string to parse. * @param context - Optional context about what's being parsed (e.g., file path, "API response"). * @returns The parsed JSON object. * @throws AbortError if JSON is malformed. * * @example * // Parse with context * const data = parseJSON(jsonString, '/path/to/config.json') * * @example * // Parse without context * const data = parseJSON(jsonString) */ export function parseJSON(jsonString, context) { try { return JSON.parse(jsonString); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); const contextMessage = context ? ` from ${context}` : ''; throw new AbortError(`Failed to parse JSON${contextMessage}.\n${errorMessage}`); } } //# sourceMappingURL=json.js.map