Add JSON parse error handling with debug output

This commit is contained in:
John Mizerek 2026-02-12 19:29:00 -08:00
parent ec59f05814
commit 89adfbc92e

View file

@ -350,9 +350,26 @@
<cfset responseText = left(responseText, len(responseText) - 3)>
</cfif>
<cfset responseText = trim(responseText)>
<!--- Remove trailing commas before ] or } --->
<cfset responseText = reReplace(responseText, ",(\s*[\]\}])", "\1", "all")>
<!--- Remove control characters that break JSON --->
<cfset responseText = reReplace(responseText, "[\x00-\x1F]", " ", "all")>
<cfset menuData = deserializeJSON(responseText)>
<!--- Try to parse JSON with error handling --->
<cftry>
<cfset menuData = deserializeJSON(responseText)>
<cfcatch type="any">
<!--- JSON parsing failed - try to extract what we can --->
<!--- Return the raw response for debugging --->
<cfset response["success"] = false>
<cfset response["error"] = "JSON parse error: #cfcatch.message#">
<cfset response["DEBUG_RAW_RESPONSE"] = left(responseText, 2000)>
<cfset response["DEBUG_RESPONSE_LENGTH"] = len(responseText)>
<cfcontent type="application/json" reset="true">
<cfoutput>#serializeJSON(response)#</cfoutput>
<cfabort>
</cfcatch>
</cftry>
<!--- Debug: save raw Claude response before processing --->
<cfset response["DEBUG_RAW_CLAUDE"] = responseText>