Add generated error code to exceptions

This commit is contained in:
René Pfeuffer
2018-10-25 16:23:43 +02:00
parent 6eb3c38655
commit 7f0b7c5831
5 changed files with 25 additions and 1 deletions

View File

@@ -8,6 +8,8 @@ import static java.util.stream.Collectors.joining;
public class AlreadyExistsException extends RuntimeException implements ExceptionWithContext { public class AlreadyExistsException extends RuntimeException implements ExceptionWithContext {
private static final String CODE = "FtR7UznKU1";
private final List<ContextEntry> context; private final List<ContextEntry> context;
public AlreadyExistsException(ModelObject object) { public AlreadyExistsException(ModelObject object) {
@@ -27,6 +29,11 @@ public class AlreadyExistsException extends RuntimeException implements Exceptio
return unmodifiableList(context); return unmodifiableList(context);
} }
@Override
public String getCode() {
return CODE;
}
private static String createMessage(List<ContextEntry> context) { private static String createMessage(List<ContextEntry> context) {
return context.stream() return context.stream()
.map(c -> c.getType().toLowerCase() + " with id " + c.getId()) .map(c -> c.getType().toLowerCase() + " with id " + c.getId())

View File

@@ -7,6 +7,9 @@ import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
public class ConcurrentModificationException extends RuntimeException implements ExceptionWithContext { public class ConcurrentModificationException extends RuntimeException implements ExceptionWithContext {
private static final String CODE = "2wR7UzpPG1";
private final List<ContextEntry> context; private final List<ContextEntry> context;
public ConcurrentModificationException(Class type, String id) { public ConcurrentModificationException(Class type, String id) {
@@ -26,6 +29,11 @@ public class ConcurrentModificationException extends RuntimeException implements
return unmodifiableList(context); return unmodifiableList(context);
} }
@Override
public String getCode() {
return CODE;
}
private static String createMessage(List<ContextEntry> context) { private static String createMessage(List<ContextEntry> context) {
return context.stream() return context.stream()
.map(c -> c.getType().toLowerCase() + " with id " + c.getId()) .map(c -> c.getType().toLowerCase() + " with id " + c.getId())

View File

@@ -6,4 +6,6 @@ public interface ExceptionWithContext {
List<ContextEntry> getContext(); List<ContextEntry> getContext();
String getMessage(); String getMessage();
String getCode();
} }

View File

@@ -12,6 +12,8 @@ import static java.util.stream.Collectors.joining;
public class NotFoundException extends RuntimeException implements ExceptionWithContext { public class NotFoundException extends RuntimeException implements ExceptionWithContext {
private static final String CODE = "AGR7UzkhA1";
private final List<ContextEntry> context; private final List<ContextEntry> context;
public NotFoundException(Class type, String id) { public NotFoundException(Class type, String id) {
@@ -35,6 +37,11 @@ public class NotFoundException extends RuntimeException implements ExceptionWith
return unmodifiableList(context); return unmodifiableList(context);
} }
@Override
public String getCode() {
return CODE;
}
private static String createMessage(List<ContextEntry> context) { private static String createMessage(List<ContextEntry> context) {
return context.stream() return context.stream()
.map(c -> c.getType().toLowerCase() + " with id " + c.getId()) .map(c -> c.getType().toLowerCase() + " with id " + c.getId())

View File

@@ -30,6 +30,6 @@ public class ErrorDto {
} }
public static ErrorDto from(ExceptionWithContext exception) { public static ErrorDto from(ExceptionWithContext exception) {
return new ErrorDto(MDC.get("transaction_id"), "todo", exception.getContext(), exception.getMessage()); return new ErrorDto(MDC.get("transaction_id"), exception.getCode(), exception.getContext(), exception.getMessage());
} }
} }