- private String serializeErrorsContainerToJson(final ContainerNode errorsContainer) {
- try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- OutputStreamWriter streamStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
- ) {
- return writeNormalizedNode(errorsContainer, outputStream,
- new JsonStreamWriterWithDisabledValidation(databindProvider.currentDatabind(), streamStreamWriter));
+ private static String serializeExceptionToJson(final RestconfDocumentedException exception,
+ final DatabindProvider databindProvider) {
+ try (var outputStream = new ByteArrayOutputStream();
+ var streamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
+ var jsonWriter = JsonWriterFactory.createJsonWriter(streamWriter, DEFAULT_INDENT_SPACES_NUM)) {
+ final var currentDatabindContext = exception.modelContext() != null
+ ? DatabindContext.ofModel(exception.modelContext()) : databindProvider.currentDatabind();
+ jsonWriter.beginObject();
+ final var errors = exception.getErrors();
+ if (errors != null && !errors.isEmpty()) {
+ jsonWriter.name(Errors.QNAME.getLocalName()).beginObject();
+ jsonWriter.name(Error.QNAME.getLocalName()).beginArray();
+ for (final var error : errors) {
+ jsonWriter.beginObject()
+ .name(ERROR_TAG_QNAME.getLocalName()).value(error.getErrorTag().elementBody());
+ final var errorAppTag = error.getErrorAppTag();
+ if (errorAppTag != null) {
+ jsonWriter.name(ERROR_APP_TAG_QNAME.getLocalName()).value(errorAppTag);
+ }
+ final var errorInfo = error.getErrorInfo();
+ if (errorInfo != null) {
+ jsonWriter.name(ERROR_INFO_QNAME.getLocalName()).value(errorInfo);
+ }
+ final var errorMessage = error.getErrorMessage();
+ if (errorMessage != null) {
+ jsonWriter.name(ERROR_MESSAGE_QNAME.getLocalName()).value(errorMessage);
+ }
+ final var errorPath = error.getErrorPath();
+ if (errorPath != null) {
+ jsonWriter.name(ERROR_PATH_QNAME.getLocalName());
+ currentDatabindContext.jsonCodecs().instanceIdentifierCodec()
+ .writeValue(jsonWriter, errorPath);
+ }
+ jsonWriter.name(ERROR_TYPE_QNAME.getLocalName()).value(error.getErrorType().elementBody());
+ jsonWriter.endObject();
+ }
+ jsonWriter.endArray().endObject();
+ }
+ jsonWriter.endObject();
+ streamWriter.flush();
+ return outputStream.toString(StandardCharsets.UTF_8);