Skip to content

Commit

Permalink
FT-800: Added better message logging for unhandled exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanmanish-atlan committed Nov 28, 2024
1 parent 2e353ad commit 5716358
Showing 1 changed file with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,73 @@

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);
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
List<String> causes = new ArrayList<>();
Throwable currentException = exception;
while (currentException != null) {
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(),
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

0 comments on commit 5716358

Please sign in to comment.