Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ private void fillResponse(boolean visible, BlockReq request, HttpServletResponse
response.getWriter().println("{}");
}
} catch (IllegalArgumentException e) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Error", e.getMessage());
response.getWriter().println(jsonObject.toJSONString());
Util.processError(e.getMessage(), response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,12 +26,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
}
response.getWriter().println("{\"brokerage\": " + value + "}");
} catch (DecoderException | IllegalArgumentException e) {
try {
response.getWriter()
.println("{\"Error\": " + "\"INVALID address, " + e.getMessage() + "\"}");
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(Util.INVALID_ADDRESS_MSG, response);
} catch (Exception e) {
Util.processError(e, response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,12 +23,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
: "{\"burnTrxAmount\": " + value + "}";
response.getWriter().println(out);
} catch (Exception e) {
logger.error("", e);
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
Comment thread
halibobo1205 marked this conversation as resolved.
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,12 +23,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println(JSON.toJSONString(nodeInfo));

} catch (Exception e) {
logger.error("", e);
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,12 +23,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
: "{\"pendingSize\": " + value + "}";
response.getWriter().println(out);
} catch (Exception e) {
logger.error("", e);
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -29,19 +28,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
: "{\"reward\": " + value + "}";
response.getWriter().println(out);
} catch (DecoderException | IllegalArgumentException e) {
try {
response.getWriter()
.println("{\"Error\": " + "\"INVALID address, " + e.getMessage() + "\"}");
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(Util.INVALID_ADDRESS_MSG, response);
} catch (Exception e) {
logger.error("", e);
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.services.http;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -52,12 +51,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println("{}");
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand All @@ -75,12 +69,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.getWriter().println("{}");
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(Util.printErrorMsg(e));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp)
super.service(req, resp);
Metrics.histogramObserve(requestTimer);
} else {
resp.getWriter()
.println(Util.printErrorMsg(new IllegalAccessException("lack of computing resources")));
Util.processError(Util.RATE_LIMITER_ERROR_MSG, resp);
}
} catch (ServletException | IOException | BadMessageException e) {
throw e;
Expand Down
44 changes: 39 additions & 5 deletions framework/src/main/java/org/tron/core/services/http/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.db.TransactionTrace;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.MaintenanceUnavailableException;
import org.tron.core.services.http.JsonFormat.ParseException;
import org.tron.json.JSON;
import org.tron.json.JSONArray;
Expand All @@ -65,6 +67,10 @@
@Slf4j(topic = "API")
public class Util {

private static final String INTERNAL_SERVER_ERROR = "internal server error";
public static final String RATE_LIMITER_ERROR_MSG = "lack of computing resources";
static final String INVALID_ADDRESS_MSG = "INVALID address";

public static final String EVENTS_DEPRECATED_MSG =
"'events' field is deprecated and no longer supported";

Expand Down Expand Up @@ -114,12 +120,35 @@ public static String printTransactionFee(String transactionFee) {
return jsonObject.toJSONString();
}

public static String printErrorMsg(Exception e) {
public static String printErrorMsg(String msg) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Error", e.getClass() + " : " + e.getMessage());
jsonObject.put("Error", msg);
return jsonObject.toJSONString();
}

private static String clientMessage(Exception e) {
if (e == null) {
return INTERNAL_SERVER_ERROR;
}

Class<?> type = e.getClass();
if (type == IllegalAccessException.class) {
return RATE_LIMITER_ERROR_MSG.equals(e.getMessage())
? RATE_LIMITER_ERROR_MSG : INTERNAL_SERVER_ERROR;
}
if (type == IllegalArgumentException.class) {
return EVENTS_DEPRECATED_MSG.equals(e.getMessage())
? EVENTS_DEPRECATED_MSG : INTERNAL_SERVER_ERROR;
}
if (type == ParseException.class
|| type == ContractValidateException.class
|| type == MaintenanceUnavailableException.class) {
String message = e.getMessage();
return StringUtils.isBlank(message) ? INTERNAL_SERVER_ERROR : message;
}
return INTERNAL_SERVER_ERROR;
}

public static String printBlockList(BlockList list, boolean selfType) {
List<Block> blocks = list.getBlockList();
JSONObject jsonObject = new JSONObject();
Expand Down Expand Up @@ -526,11 +555,16 @@ public static String getMemo(byte[] memo) {
}

public static void processError(Exception e, HttpServletResponse response) {
logger.debug(e.getMessage(), e);
logger.debug("HTTP request failed", e);
processError(clientMessage(e), response);
}

// Bypasses clientMessage: callers must pass audited fixed or pre-existing client texts only.
static void processError(String msg, HttpServletResponse response) {
try {
response.getWriter().println(Util.printErrorMsg(e));
response.getWriter().println(Util.printErrorMsg(msg));
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
logger.debug("Failed to write HTTP error response", ioe);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private String validAddress(String input) {
}
} catch (Exception e) {
result = false;
msg = e.getMessage();
msg = "Invalid address";
}

JSONObject jsonAddress = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String input = request.getParameter("value");
fillResponse(ByteString.copyFrom(ByteArray.fromHexString(input)), visible, response);
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand All @@ -46,12 +41,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
JsonFormat.merge(params.getParams(), build, params.isVisible());
fillResponse(build.build().getValue(), params.isVisible(), response);
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tron.core.services.http.solidity;

import com.google.protobuf.ByteString;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -37,12 +36,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println(JsonFormat.printToString(transInfo, visible));
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand All @@ -60,12 +54,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.getWriter().println(JsonFormat.printToString(transInfo, params.isVisible()));
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void doPostTest() throws IOException {
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
Assert.assertTrue(sb.toString().contains("null"));
Assert.assertTrue(sb.toString().contains("{\"Error\":\"internal server error\"}"));
httpUrlConnection.disconnect();
}
}
Loading
Loading