{"record":{"id":"b65f001065d9ee90","repo":"AykutSarac/jsoncrack.com","slug":"error","errorCode":null,"errorMessage":"error","messagePattern":"error","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/www/src/hooks/useJsonQuery.ts","lineNumber":22,"sourceCode":"\nconst useJsonQuery = () => {\n  const getJson = useJson(state => state.getJson);\n  const setContents = useFile(state => state.setContents);\n\n  const transformer = async ({ value }) => {\n    const { run } = await import(\"json_typegen_wasm\");\n    return run(\"Root\", value, JSON.stringify({ output_mode: \"typescript/typealias\" }));\n  };\n\n  const updateJson = async (query: string, cb?: () => void) => {\n    try {\n      const jq = await import(\"jq-web\");\n      const res = await jq.promised.json(JSON.parse(getJson()), query);\n\n      setContents({ contents: JSON.stringify(res, null, 2) });\n      cb?.();\n    } catch (error) {\n      console.error(error);\n      toast.error(\"Unable to process the request.\");\n    }\n  };\n\n  const getJsonType = async () => {\n    const types = await transformer({ value: getJson() });\n    return types;\n  };\n\n  return { updateJson, getJsonType };\n};\n\nexport default useJsonQuery;\n","sourceCodeStart":4,"sourceCodeEnd":36,"githubUrl":"https://github.com/AykutSarac/jsoncrack.com/blob/3c9af69e23c635356293b6b28cf4cd0af10d1059/apps/www/src/hooks/useJsonQuery.ts#L4-L36","documentation":"Logged by the updateJson hook when running a jq query against the current document via the jq-web WASM module. The catch fires when JSON.parse(getJson()) throws on malformed editor content, when the dynamic import(\"jq-web\") rejects (WASM asset blocked/missing), or when the jq query string is syntactically invalid. The WASM engine surfaces a generic Error whose message usually cites the parser position.","triggerScenarios":"Calling updateJson with a malformed jq expression (e.g. unterminated pipe '.users[] | .name'); invoking it while the editor buffer holds non-JSON text so JSON.parse(getJson()) throws SyntaxError; jq-web's .wasm file 404ing behind a CDN/proxy so import(\"jq-web\") rejects.","commonSituations":"Typo in the jq query input box; editor left with a trailing comma or comment that breaks JSON.parse; jq-web version pin where the WASM asset moved on the CDN; pasting a JMESPath/JSONPath expression instead of jq syntax.","solutions":["Pre-validate JSON.parse(getJson()) before importing jq and surface a specific 'invalid JSON' message instead of the generic toast.","Split the catch so dynamic-import rejection (asset load) is distinguishable from jq execution failure.","Include error.message in the toast so the user sees the jq parser position.","Pin or upgrade jq-web if the WASM asset repeatedly fails to load."],"exampleFix":"// before\ntry {\n  const jq = await import(\"jq-web\");\n  const res = await jq.promised.json(JSON.parse(getJson()), query);\n  setContents({ contents: JSON.stringify(res, null, 2) });\n  cb?.();\n} catch (error) {\n  console.error(error);\n  toast.error(\"Unable to process the request.\");\n}\n\n// after\nlet parsed: unknown;\ntry {\n  parsed = JSON.parse(getJson());\n} catch {\n  return toast.error(\"Editor content is not valid JSON.\");\n}\ntry {\n  const jq = await import(\"jq-web\");\n  const res = await jq.promised.json(parsed, query);\n  setContents({ contents: JSON.stringify(res, null, 2) });\n  cb?.();\n} catch (error) {\n  console.error(error);\n  toast.error(error instanceof Error ? error.message : \"Unable to process the request.\");\n}","handlingStrategy":"validation","validationCode":"// Run before invoking jq\nfunction isValidJsonInput(value: string): boolean {\n  try {\n    JSON.parse(value);\n    return true;\n  } catch {\n    return false;\n  }\n}\n\nif (!isValidJsonInput(getJson())) {\n  toast.error(\"Editor content is not valid JSON.\");\n  return;\n}","typeGuard":"function isJqWebModule(mod: unknown): mod is { promised: { json: (input: unknown, q: string) => Promise<unknown> } } {\n  return typeof (mod as any)?.promised?.json === \"function\";\n}","tryCatchPattern":"try {\n  const jq = await import(\"jq-web\");\n  if (!isJqWebModule(jq)) throw new Error(\"jq-web module shape unexpected\");\n  const res = await jq.promised.json(JSON.parse(getJson()), query);\n} catch (error) {\n  if (error instanceof SyntaxError) toast.error(\"Invalid JSON input: \" + error.message);\n  else if (error instanceof Error) toast.error(\"jq error: \" + error.message);\n  else toast.error(\"Unable to process the request.\");\n}","preventionTips":["Pre-validate the editor buffer with JSON.parse before exposing the jq query input.","Pin jq-web to a known-good version and verify the WASM asset URL is reachable in CI.","Show error.message in the toast so users can self-diagnose query typos."],"tags":["jq","wasm","json-parse","dynamic-import"],"backgroundTag":null,"analyzedSha":"3c9af69e23c635356293b6b28cf4cd0af10d1059","analyzedAt":"2026-08-12T19:00:27.891Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}