private String assembleErrorMessage(Exception e) {
JSONObject errorMessage = new JSONObject();
Optional<StackTraceElement> stackTraceElement = Arrays.stream(e.getStackTrace()).findFirst();
String cause = e.toString();
String className = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getClassName()).orElse("");
String fileName = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getFileName()).orElse("");
String methodName = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getMethodName()).orElse("");
int lineNumber = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getLineNumber()).orElse(0);
errorMessage.put("cause", cause);
errorMessage.put("className", className);
errorMessage.put("fileName", fileName);
errorMessage.put("methodName", methodName);
errorMessage.put("lineNumber", lineNumber);
return errorMessage.toString();
}
效果