{"record":{"id":"c475372f256ab290","repo":"BoundaryML/baml","slug":"failed-to-get-json-w","errorCode":null,"errorMessage":"failed to get JSON: %w","messagePattern":"failed to get JSON: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_http_body.go","lineNumber":44,"sourceCode":"\nfunc (h *httpBody) Text() (string, error) {\n\tresult, err := raw_objects.CallMethod(h, \"text\", nil)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get text: %w\", err)\n\t}\n\n\ttext, ok := result.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"unexpected type for text: %T\", result)\n\t}\n\n\treturn text, nil\n}\n\nfunc (h *httpBody) JSON() (any, error) {\n\tresult, err := raw_objects.CallMethod(h, \"json\", nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get JSON: %w\", err)\n\t}\n\n\treturn result, nil\n}\n","sourceCodeStart":26,"sourceCodeEnd":49,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_http_body.go#L26-L49","documentation":"HTTPBody.JSON() calls the underlying JS/runtime object's 'json' method via the raw-objects bridge. If the bridge call fails (the runtime object is invalid, detached, or its 'json' method raises), the error is wrapped with 'failed to get JSON'. This means the library could not extract a JSON representation of the HTTP response body.","triggerScenarios":"Calling JSON() on an HTTPBody whose underlying raw object no longer has a callable 'json' method, the body object was garbage-collected/detached from the runtime, or the runtime method itself returned an error (e.g. body is not valid JSON).","commonSituations":"Consuming a response body after the request context was torn down; interop handlers returning objects without the expected 'json' method; runtime/script reloaded between request and body read.","solutions":["Check that the HTTPBody was obtained from a live HTTPRequest/HTTPResponse and read it before the runtime context is disposed","Print/inspect the wrapped cause (%w) to see whether the underlying 'json' method raised or was missing","If the body may not be valid JSON, prefer Text() and unmarshal yourself with error handling","Re-run the request to get a fresh body instead of reusing a stale one"],"exampleFix":"// before\nval, err := body.JSON()\n// after\nif txt, err := body.Text(); err == nil {\n    var val any\n    if jerr := json.Unmarshal([]byte(txt), &val); jerr != nil {\n        log.Fatalf(\"body is not valid JSON: %v\", jerr)\n    }\n} else {\n    log.Fatalf(\"failed to read body: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"// Go has no pre-check; ensure the body comes from a live dispatch:\nif body == nil { return errors.New(\"no body available\") }","typeGuard":"func validBody(b HTTPBody) bool { return b != nil }","tryCatchPattern":"val, err := body.JSON()\nif err != nil {\n    var txt string\n    if terr := body.Text(); terr == nil { txt = terr2txt(txt, terr) }\n    return fmt.Errorf(\"JSON body unavailable: %w\", err)\n}","preventionTips":["Read the body inside the same request lifecycle/handler that produced it","Fall back to Text() + json.Unmarshal when the body may be non-JSON","Avoid caching HTTPBody objects across runtime invocations","Log the wrapped cause to distinguish missing method vs runtime exception"],"tags":["go","runtime-bridge","json","http-body"],"backgroundTag":"method-not-implemented","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}