{"record":{"id":"d10437ffcd5dcf0d","repo":"SonarSource/sonarqube","slug":"error-while-writing-protobuf-message","errorCode":null,"errorMessage":"Error while writing protobuf message","messagePattern":"Error while writing protobuf message","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java","lineNumber":50,"sourceCode":"import static org.sonarqube.ws.MediaTypes.JSON;\nimport static org.sonarqube.ws.MediaTypes.PROTOBUF;\n\npublic interface WsUtils {\n\n  static void writeProtobuf(Message msg, Request request, Response response) {\n    OutputStream output = response.stream().output();\n    try {\n      if (request.getMediaType().equals(PROTOBUF)) {\n        response.stream().setMediaType(PROTOBUF);\n        msg.writeTo(output);\n      } else {\n        response.stream().setMediaType(JSON);\n        try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) {\n          ProtobufJsonFormat.write(msg, writer);\n        }\n      }\n    } catch (Exception e) {\n      throw new IllegalStateException(\"Error while writing protobuf message\", e);\n    } finally {\n      IOUtils.closeQuietly(output);\n    }\n  }\n\n  static String createHtmlExternalLink(String url, String text) {\n    return String.format(\"<a href=\\\"%s\\\" target=\\\"_blank\\\" rel=\\\"noopener noreferrer\\\">%s</a>\", url, text);\n  }\n}\n","sourceCodeStart":32,"sourceCodeEnd":60,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-ws/src/main/java/org/sonar/server/ws/WsUtils.java#L32-L60","documentation":"WsUtils.writeProtobuf serializes a protobuf Message either as binary protobuf or as JSON (via ProtobufJsonFormat + JsonWriter) into the HTTP response. Any exception thrown during that serialization is wrapped in an IllegalStateException with this message, since response serialization failures indicate an internal bug rather than a client error.","triggerScenarios":"Writing a web service response whose protobuf message fails ProtobufJsonFormat.write() (e.g. invalid UTF-8 or unserializable field state) or when the underlying OutputStream/JsonWriter throws (client disconnected, broken pipe, IO error).","commonSituations":"Client aborting the connection mid-response causing a broken pipe; a protobuf field holding invalid state (e.g. bytes that violate the JSON format); custom plugin web services emitting malformed protobufs.","solutions":["Inspect the wrapped cause (e.getCause()) to identify whether it is an IO problem or a protobuf serialization problem","If the cause is a broken pipe/IO error, it is a client disconnect and can be ignored or logged at debug","If the cause is ProtobufFormat/serialization, fix the message construction in the WS handler (e.g. invalid enum or bytes field)"],"exampleFix":"// before\nProtobufFormat.write(msg, output); // raw bytes when client expects JSON\n// after\nresponse.stream().setMediaType(JSON);\ntry (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) {\n  ProtobufJsonFormat.write(msg, writer);\n}","handlingStrategy":"try-catch","validationCode":"if (!msg.isInitialized()) throw new Error('Protobuf message not initialized before write');","typeGuard":"const isWritableMessage = (m) => m && typeof m.isInitialized === 'function' && m.isInitialized();","tryCatchPattern":"try { writeProtobuf(msg, request, response); } catch (IllegalStateException e) { if (e.getCause() instanceof IOException) { log.debug('Client aborted', e); } else { throw e; } }","preventionTips":["Always fully populate required protobuf fields before responding","Handle client disconnects (broken pipe) as recoverable IO errors","For custom WS plugins, unit-test serialization via WsUtils.TestRequest/Response"],"tags":["protobuf","serialization","io"],"backgroundTag":"protobuf-unmarshal-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}