@Override
    public CRParseResult responseMessageForParseDirectory(String responseBody) {
        ErrorCollection errors = new ErrorCollection();
        try {
            ResponseScratch responseMap = parseResponseForMigration(responseBody);
            ParseDirectoryResponseMessage parseDirectoryResponseMessage;

            if (responseMap.target_version == null) {
                errors.addError("Plugin response message", "missing 'target_version' field");
                return new CRParseResult(errors);
            } else if (responseMap.target_version > CURRENT_CONTRACT_VERSION) {
                String message = String.format("'target_version' is %s but the GoCD Server supports %s",
                        responseMap.target_version, CURRENT_CONTRACT_VERSION);
                errors.addError("Plugin response message", message);
                return new CRParseResult(errors);
            } else {
                int version = responseMap.target_version;

                while (version < CURRENT_CONTRACT_VERSION) {
                    version++;
                    responseBody = migrate(responseBody, version);
                }
                // after migration, json should match contract
                parseDirectoryResponseMessage = codec.getGson().fromJson(responseBody, ParseDirectoryResponseMessage.class);
                parseDirectoryResponseMessage.validateResponse(errors);

                errors.addErrors(parseDirectoryResponseMessage.getPluginErrors());

                return new CRParseResult(parseDirectoryResponseMessage.getEnvironments(), parseDirectoryResponseMessage.getPipelines(), errors);
            }
        } catch (Exception ex) {
            StringBuilder builder = new StringBuilder();
            builder.append("Unexpected error when handling plugin response").append('\n');
            builder.append(ex);
            // "location" of error is runtime. This is what user will see in config repo errors list.
            errors.addError("runtime", builder.toString());
            LOGGER.error(builder.toString(), ex);
            return new CRParseResult(errors);
        }
    }
@Test
    public void shouldAppendPluginErrorsToAllErrors() {
        String json = """
                {
                  "target_version" : 1,
                  "pipelines" : [],
                  "errors" : [{"location" : "somewhere", "message" : "failed to parse pipeline.json"}]
                }""";
        CRParseResult result = handler.responseMessageForParseDirectory(json);
        assertThat(result.hasErrors()).isTrue();
    }