{"record":{"id":"fc4a4f6892d1d9f8","repo":"Semantic-Org/Semantic-UI","slug":"json-could-not-be-parsed-during-error-handling","errorCode":null,"errorMessage":"JSON could not be parsed during error handling","messagePattern":"JSON could not be parsed during error handling","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/definitions/behaviors/api.js","lineNumber":1130,"sourceCode":"  onComplete  : function(response, $module) {},\n\n  // failed JSON success test\n  onFailure   : function(response, $module) {},\n\n  // server error\n  onError     : function(errorMessage, $module) {},\n\n  // request aborted\n  onAbort     : function(errorMessage, $module) {},\n\n  successTest : false,\n\n  // errors\n  error : {\n    beforeSend        : 'The before send function has aborted the request',\n    error             : 'There was an error with your request',\n    exitConditions    : 'API Request Aborted. Exit conditions met',\n    JSONParse         : 'JSON could not be parsed during error handling',\n    legacyParameters  : 'You are using legacy API success callback names',\n    method            : 'The method you called is not defined',\n    missingAction     : 'API action used but no url was defined',\n    missingSerialize  : 'jquery-serialize-object is required to add form data to an existing data object',\n    missingURL        : 'No URL specified for api event',\n    noReturnedValue   : 'The beforeSend callback must return a settings object, beforeSend ignored.',\n    noStorage         : 'Caching responses locally requires session storage',\n    parseError        : 'There was an error parsing your request',\n    requiredParameter : 'Missing a required URL parameter: ',\n    statusMessage     : 'Server gave an error: ',\n    timeout           : 'Your request timed out'\n  },\n\n  regExp  : {\n    required : /\\{\\$*[A-z0-9]+\\}/g,\n    optional : /\\{\\/\\$*[A-z0-9]+\\}/g,\n  },\n","sourceCodeStart":1112,"sourceCodeEnd":1148,"githubUrl":"https://github.com/Semantic-Org/Semantic-UI/blob/597843ab84d565cccfd8fd7719416350ae73e734/src/definitions/behaviors/api.js#L1112-L1148","documentation":"This error message is defined in the API module's settings.error dictionary under the 'JSONParse' key. It is intended for situations where the response body cannot be parsed as JSON during error handling — i.e., the server returned a non-JSON response (often an HTML error page) when JSON was expected. While it is part of the error vocabulary, it is not directly invoked via module.error() in the current source version but is available for custom error handling.","triggerScenarios":"The server returns an error response with an HTML body (e.g., a 500 error page, a reverse proxy error page) instead of JSON. The API module's response decoder (module.decode.json) fails to JSON.parse the response during error handling, leaving the raw string.","commonSituations":"Production servers with custom error pages (HTML) for 500/502/503 errors. Reverse proxies (nginx, HAProxy) returning HTML error pages. ASP.NET or Rails serving exception pages in development mode. Content-Type header mismatches where the server claims JSON but returns HTML.","solutions":["Configure the server to return JSON error responses even for error status codes.","Check the Content-Type header of error responses and ensure it matches application/json.","Use settings.successTest to validate response structure before processing.","Add a response interceptor that detects HTML error pages and extracts a meaningful error message."],"exampleFix":"// before\n$('.api-element').api({\n  url: '/api/data'\n  // server returns HTML 500 page, JSON parse fails silently\n});\n\n// after\n$('.api-element').api({\n  url: '/api/data',\n  successTest: function(response) {\n    return response && response.success !== undefined;\n  },\n  onError: function(errorMessage, $module, xhr) {\n    var ct = xhr.getResponseHeader('content-type') || '';\n    if (ct.indexOf('text/html') !== -1) {\n      errorMessage = 'Server returned an HTML error page instead of JSON.';\n    }\n    console.error(errorMessage);\n  }\n});","handlingStrategy":"validation","validationCode":"// Pre-validate server response format before processing\n$.get('/api/data').done(function(response, status, xhr) {\n  var ct = xhr.getResponseHeader('content-type') || '';\n  if (ct.indexOf('application/json') === -1) {\n    console.error('Expected JSON, got:', ct);\n    return;\n  }\n  try { JSON.parse(xhr.responseText); }\n  catch(e) { console.error('Malformed JSON response'); }\n});","typeGuard":"// Verify a response is valid JSON\nfunction isJsonResponse(xhr) {\n  var ct = xhr.getResponseHeader('content-type') || '';\n  if (ct.indexOf('application/json') === -1) return false;\n  try { JSON.parse(xhr.responseText); return true; }\n  catch(e) { return false; }\n}","tryCatchPattern":"// Handle JSON parse failures in onError\n$('.el').api({\n  onError: function(errorMessage, $module, xhr) {\n    try {\n      JSON.parse(xhr.responseText);\n    } catch(e) {\n      console.error('Server returned non-JSON response during error handling');\n    }\n  }\n});","preventionTips":["Configure the server to always return JSON, even for error responses.","Set Content-Type: application/json on all API responses.","Disable HTML error pages for API routes."],"tags":["api","json","parse-error","server-error","semantic-ui"],"backgroundTag":null,"analyzedSha":"597843ab84d565cccfd8fd7719416350ae73e734","analyzedAt":"2026-08-13T01:44:23.827Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}