{"record":{"id":"8a1d6487cbdb340d","repo":"apache/shenyu","slug":"response-modify-failure-s","errorCode":null,"errorMessage":"response modify failure. %s","messagePattern":"response modify failure\\. (.+?)","errorType":"exception","errorClass":"ShenyuException","httpStatus":null,"severity":"error","filePath":"shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java","lineNumber":162,"sourceCode":"\n            // reset http status\n            if (this.ruleHandle.getStatusCode() > 0) {\n                this.setStatusCode(HttpStatus.valueOf(this.ruleHandle.getStatusCode()));\n            }\n\n            // reset http headers\n            this.getDelegate().getHeaders().clear();\n            this.getDelegate().getHeaders().putAll(httpHeaders);\n        }\n\n        private byte[] modifyBody(final byte[] responseBody) {\n            try {\n                String bodyStr = modifyBody(new String(responseBody, StandardCharsets.UTF_8));\n                LOG.info(\"the body string {}\", bodyStr);\n                return bodyStr.getBytes(StandardCharsets.UTF_8);\n            } catch (Exception e) {\n                LOG.error(\"modify response error\", e);\n                throw new ShenyuException(String.format(\"response modify failure. %s\", e.getLocalizedMessage()));\n            }\n        }\n\n        private String modifyBody(final String jsonValue) {\n            DocumentContext context = JsonPath.parse(jsonValue);\n            if (CollectionUtils.isNotEmpty(this.ruleHandle.getAddBodyKeys())) {\n                this.ruleHandle.getAddBodyKeys().forEach(info -> context.put(info.getPath(), info.getKey(), info.getValue()));\n            }\n            if (CollectionUtils.isNotEmpty(this.ruleHandle.getReplaceBodyKeys())) {\n                this.ruleHandle.getReplaceBodyKeys().forEach(info -> context.renameKey(info.getPath(), info.getKey(), info.getValue()));\n            }\n            if (CollectionUtils.isNotEmpty(this.ruleHandle.getRemoveBodyKeys())) {\n                this.ruleHandle.getRemoveBodyKeys().forEach(context::delete);\n            }\n            return context.jsonString();\n        }\n    }\n}","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java#L144-L180","documentation":"ModifyResponsePlugin.writeWith reads the upstream response body and modifyBody rewrites the JSON using JsonPath operations (add/replace/remove body keys per the rule handle). Any exception during parsing or modification (invalid JSON, bad JsonPath expressions, missing keys) is wrapped in a ShenyuException with the message 'response modify failure.' plus the original message.","triggerScenarios":"A response passes through ModifyResponsePlugin with a rule handle containing addBodyKeys/replaceBodyKeys/removeBodyKeys, and the upstream body is not valid JSON, or a JsonPath expression in the rule handle does not match/apply to the body structure.","commonSituations":"Upstream returns HTML/plain text or an error page instead of JSON while a modify-response rule expects JSON; operator configures a JsonPath key that doesn't exist in the response; upstream response is empty or truncated; charset mismatches corrupt the body string.","solutions":["Inspect the LOG.error('modify response error', e) stack trace to find the underlying cause (JsonPath parse error vs. path evaluation).","Verify the upstream actually returns JSON for requests matching this modify-response rule; add a condition so non-JSON responses bypass the rule.","Check the rule handle config: confirm every JsonPath key (add/replace/remove) exists in the response body or use safe path operations.","Test the rule with a representative response body using a JsonPath evaluator before deploying."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// validate upstream body is JSON before enabling the rule\ntry {\n    JsonPath.parse(body);\n} catch (Exception e) {\n    // bypass modify-response rule for non-JSON bodies\n}","typeGuard":null,"tryCatchPattern":"try {\n    return chain.writeWith(exchange);\n} catch (ShenyuException e) {\n    LOG.error(\"response modify failed: {}\", e.getLocalizedMessage());\n    return setErrorResponse(exchange, 500);\n}","preventionTips":["Scope modify-response rules with match conditions so only JSON responses are processed.","Validate JsonPath expressions against real response samples before publishing rules.","Use optional/safe JsonPath operations for keys that may be absent.","Watch the 'modify response error' logs to catch rule/body mismatches early."],"tags":["json","response-modify","jsonpath","gateway"],"backgroundTag":"json-parse-error","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}