Fix error response formatting issue.

pull/540/head
Nipuni Kavindya 3 weeks ago
parent 860470832f
commit 16b7c4f3e7

@ -0,0 +1,55 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.ui.request.interceptor.beans;
public class ErrorResponse {
private int code;
private String data;
private int status;
public ErrorResponse(int code, String data, int status) {
this.code = code;
this.data = data;
this.status = status;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

@ -20,17 +20,16 @@ package io.entgra.device.mgt.core.ui.request.interceptor.util;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.entgra.device.mgt.core.ui.request.interceptor.beans.AuthData;
import io.entgra.device.mgt.core.ui.request.interceptor.beans.ErrorResponse;
import io.entgra.device.mgt.core.ui.request.interceptor.cache.LoginCache;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
@ -227,16 +226,37 @@ public class HandlerUtil {
proxyResponse.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil
.getStatusKey(HandlerConstants.INTERNAL_ERROR_CODE));
}
JsonNode dataNode = proxyResponse.getData();
String responseData = extractDataAsString(dataNode);
resp.setStatus(proxyResponse.getCode());
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
resp.setCharacterEncoding(Consts.UTF_8.name());
proxyResponse.setExecutorResponse(null);
proxyResponse.setData(null);
ErrorResponse errorResponse = new ErrorResponse(
proxyResponse.getCode(),
responseData,
proxyResponse.getStatus()
);
try (PrintWriter writer = resp.getWriter()) {
writer.write(gson.toJson(proxyResponse));
writer.write(gson.toJson(errorResponse));
}
}
/**
* Extracts a string representation from the given JsonNode.
*
* @param dataNode the JsonNode from which to extract the string representation (can be null).
* @return the string representation of the JsonNode, or null if the dataNode is null.
*/
private static String extractDataAsString(JsonNode dataNode) {
if (dataNode == null) {
return null;
}
return dataNode.isTextual() ? dataNode.asText() : dataNode.toString();
}
/**
* Handle error requests with custom error codes.
*
@ -772,9 +792,7 @@ public class HandlerUtil {
try {
finalNode = objectMapper.readTree(content);
} catch (JsonProcessingException e) {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("message", content);
finalNode = objectMapper.valueToTree(objectNode);
finalNode = new TextNode(content);
}
}
return finalNode;

Loading…
Cancel
Save