Skip to content

Commit

Permalink
code changes, adding utility
Browse files Browse the repository at this point in the history
  • Loading branch information
matcdac committed May 29, 2023
1 parent 104d450 commit 597458e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/TurquoiseSpace/constant/ErrorConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.TurquoiseSpace.constant;

public interface ErrorConstants {

public static final String ERROR_ENCOUNTERED = "Encountered Error -> (class) {} (message) {}";

public static final String ERROR_PARSED = "Parsed Error -> (json) {}";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.TurquoiseSpace.constant;

public interface ExceptionConstants {

public static final String EXCEPTION_ENCOUNTERED = "Encountered Exception -> (class) {} (message) {}";

public static final String EXCEPTION_PARSED = "Parsed Exception -> (json) {}";

}
4 changes: 2 additions & 2 deletions src/main/java/com/TurquoiseSpace/model/GenericException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;
import java.util.TreeMap;

import com.TurquoiseSpace.constant.ThrowableConstants;
import com.TurquoiseSpace.constant.ExceptionConstants;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,7 +24,7 @@ private void addTraceHeirarchy(int index, StackTraceElement stackTraceElement) {
try {
exceptionPoint = new ExceptionPoint(stackTraceElement);
} catch (RuntimeException exception) {
log.error(ThrowableConstants.THROWABLE_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
log.error(ExceptionConstants.EXCEPTION_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
}
this.exceptionTraceHeirarchy.put(index, exceptionPoint);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.TurquoiseSpace.utility;

import com.TurquoiseSpace.constant.ThrowableConstants;
import com.TurquoiseSpace.constant.ExceptionConstants;
import com.TurquoiseSpace.model.GenericException;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -17,7 +17,7 @@ private static GenericException makeGenericException(Exception exception, String
}

public static String getExceptionJson(Exception exception, String customMessage) {
log.debug(ThrowableConstants.THROWABLE_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
log.debug(ExceptionConstants.EXCEPTION_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
GenericException genericException = makeGenericException(exception, customMessage);
return JsonUtil.convertObjectToJson(genericException);
}
Expand All @@ -28,7 +28,7 @@ public static String getExceptionJson(Exception exception) {

public static void logException(Exception exception, String customMessage) {
String exceptionJson = getExceptionJson(exception, customMessage);
log.error(ThrowableConstants.THROWABLE_PARSED, exceptionJson);
log.error(ExceptionConstants.EXCEPTION_PARSED, exceptionJson);
}

public static void logException(Exception exception) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/TurquoiseSpace/utility/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.TurquoiseSpace.utility;

import com.TurquoiseSpace.constant.ThrowableConstants;
import com.TurquoiseSpace.constant.ExceptionConstants;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -26,7 +26,7 @@ public static <T> String convertObjectToJson(T object) {
try {
return OBJECT_MAPPER.writeValueAsString(object);
} catch (Exception exception) {
log.error(ThrowableConstants.THROWABLE_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
log.error(ExceptionConstants.EXCEPTION_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
}
return null;
}
Expand All @@ -36,7 +36,7 @@ public static <T> T convertJsonToObject(String json, Class<T> clazz) {
try {
return OBJECT_MAPPER.readValue(json, clazz);
} catch (Exception exception) {
log.error(ThrowableConstants.THROWABLE_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
log.error(ExceptionConstants.EXCEPTION_ENCOUNTERED, exception.getClass().getName(), exception.getMessage(), exception);
}
return null;
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/TurquoiseSpace/utility/ThrowableLogUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.TurquoiseSpace.utility;

import com.TurquoiseSpace.constant.ThrowableConstants;
import com.TurquoiseSpace.model.GenericException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ThrowableLogUtil {

private static GenericException makeGenericException(Throwable throwable, String customMessage) {
if (null == customMessage || customMessage.trim().isEmpty()) {
return new GenericException(throwable);
} else {
return new GenericException(throwable, customMessage);
}
}

public static String getThrowableJson(Throwable throwable, String customMessage) {
log.debug(ThrowableConstants.THROWABLE_ENCOUNTERED, throwable.getClass().getName(), throwable.getMessage(), throwable);
GenericException genericException = makeGenericException(throwable, customMessage);
return JsonUtil.convertObjectToJson(genericException);
}

public static String getThrowableJson(Throwable throwable) {
return getThrowableJson(throwable, null);
}

public static void logThrowable(Throwable throwable, String customMessage) {
String throwableJson = getThrowableJson(throwable, customMessage);
log.error(ThrowableConstants.THROWABLE_PARSED, throwableJson);
}

public static void logThrowable(Throwable throwable) {
logThrowable(throwable, null);
}

}

0 comments on commit 597458e

Please sign in to comment.