Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FT-800 Added better message logging for unhandled exceptions #3805

Merged
4 commits merged into from
Dec 5, 2024
Merged
Changes from 3 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 @@ -19,15 +19,88 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

public class ExceptionMapperUtil {
protected static final Logger LOGGER = LoggerFactory.getLogger(ExceptionMapperUtil.class);

@SuppressWarnings("UnusedParameters")
protected static String formatErrorMessage(long id, Exception exception) {
return String.format("There was an error processing your request. It has been logged (ID %016x).", id);
if (exception == null) {
// If the exception is null, return a minimal error message
return "{\n"
+ String.format(" \"errorId\": \"%016x\",\n", id)
+ " \"message\": \"No exception provided\",\n"
+ " \"causes\": []\n"
+ "}";
This conversation was marked as resolved.
Show resolved Hide resolved
}

StringBuilder response = new StringBuilder();

// Add error ID and general error message
response.append("{\n")
.append(String.format(" \"errorId\": \"%016x\",\n", id))
.append(" \"message\": \"There was an error processing your request.\",\n")
.append(" \"causes\": [\n");

// Traverse through the chain of causes, avoiding cycles
List<String> causes = new ArrayList<>();
List<Throwable> visited = new ArrayList<>();
Throwable currentException = exception;
while (currentException != null) {
if (visited.contains(currentException)) {
causes.add(" {\n \"errorType\": \"CircularReferenceDetected\",\n \"errorMessage\": \"A circular reference was detected in the exception chain.\",\n \"location\": \"Unavailable\"\n }");
break;
}
visited.add(currentException);
causes.add(formatCause(currentException));
currentException = currentException.getCause();
}

// Add all formatted causes to the response
for (int i = 0; i < causes.size(); i++) {
response.append(causes.get(i));
if (i < causes.size() - 1) {
response.append(",\n");
}
}

// Close the JSON structure
response.append("\n ]\n")
.append("}");

return response.toString();
}

// Helper method to format a single exception cause
private static String formatCause(Throwable exception) {
StringBuilder cause = new StringBuilder();

// Extract location details from the first stack trace element
StackTraceElement[] stackTrace = exception.getStackTrace();
String location = "Unavailable";
if (stackTrace != null && stackTrace.length > 0) {
StackTraceElement element = stackTrace[0];
location = String.format("%s.%s (%s:%d)",
element.getClassName(),
mehtaanshul marked this conversation as resolved.
Show resolved Hide resolved
element.getMethodName(),
element.getFileName(),
element.getLineNumber());
}

// Build JSON object for this cause
cause.append(" {\n")
.append(" \"errorType\": \"").append(exception.getClass().getName()).append("\",\n")
.append(" \"errorMessage\": \"").append(exception.getMessage() != null ? exception.getMessage() : "No additional information provided").append("\",\n")
.append(" \"location\": \"").append(location).append("\"\n")
.append(" }");

return cause.toString();
}



protected static void logException(long id, Exception exception) {
LOGGER.error(formatLogMessage(id, exception), exception);
}
Expand Down
Loading