Skip to content

Commit

Permalink
Merge pull request #3805 from atlanhq/FT-800
Browse files Browse the repository at this point in the history
FT-800 Added better message logging for unhandled exceptions
  • Loading branch information
pavanmanishd authored Dec 5, 2024
2 parents e28866d + 1fba289 commit 59e43a9
Showing 1 changed file with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,80 @@
*/
package org.apache.atlas.web.errors;

import org.apache.atlas.type.AtlasType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

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
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put("errorId", String.format("%016x", id));
errorDetails.put("message", "No exception provided.");
errorDetails.put("causes", new ArrayList<>());
return AtlasType.toJson(errorDetails);
}

// Prepare data for error message
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put("errorId", String.format("%016x", id));
errorDetails.put("message", "There was an error processing your request.");

// Create a list of causes
List<Map<String, String>> causes = new ArrayList<>();
List<Throwable> visited = new ArrayList<>();
Throwable currentException = exception;

while (currentException != null) {
if (visited.contains(currentException)) {
// If circular reference detected, add special entry
Map<String, String> circularCause = new HashMap<>();
circularCause.put("errorType", "CircularReferenceDetected");
circularCause.put("errorMessage", "A circular reference was detected in the exception chain.");
circularCause.put("location", "Unavailable");
causes.add(circularCause);
break;
}
visited.add(currentException);
causes.add(formatCause(currentException));
currentException = currentException.getCause();
}

errorDetails.put("causes", causes);

return AtlasType.toJson(errorDetails);
}

// Helper method to format a single exception cause
private static Map<String, String> formatCause(Throwable exception) {
Map<String, String> cause = new HashMap<>();

// 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());
}

// Populate the cause map
cause.put("errorType", exception.getClass().getName());
cause.put("errorMessage", exception.getMessage() != null ? exception.getMessage() : "No additional information provided");
cause.put("location", location);

return cause;
}

protected static void logException(long id, Exception exception) {
Expand All @@ -36,5 +101,4 @@ protected static void logException(long id, Exception exception) {
protected static String formatLogMessage(long id, Throwable exception) {
return String.format("Error handling a request: %016x", id);
}

}

0 comments on commit 59e43a9

Please sign in to comment.