Remove code duplication

This commit is contained in:
René Pfeuffer
2018-10-26 15:15:51 +02:00
parent f6fe299bb1
commit 4a307379a2
5 changed files with 21 additions and 40 deletions

View File

@@ -3,15 +3,12 @@ package sonia.scm;
import java.util.List;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining;
public class AlreadyExistsException extends RuntimeException implements ExceptionWithContext {
public class AlreadyExistsException extends ExceptionWithContext {
private static final String CODE = "FtR7UznKU1";
private final List<ContextEntry> context;
public AlreadyExistsException(ModelObject object) {
this(singletonList(new ContextEntry(object.getClass(), object.getId())));
}
@@ -21,12 +18,7 @@ public class AlreadyExistsException extends RuntimeException implements Exceptio
}
private AlreadyExistsException(List<ContextEntry> context) {
super(createMessage(context));
this.context = context;
}
public List<ContextEntry> getContext() {
return unmodifiableList(context);
super(context, createMessage(context));
}
@Override

View File

@@ -3,15 +3,12 @@ package sonia.scm;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining;
public class ConcurrentModificationException extends RuntimeException implements ExceptionWithContext {
public class ConcurrentModificationException extends ExceptionWithContext {
private static final String CODE = "2wR7UzpPG1";
private final List<ContextEntry> context;
public ConcurrentModificationException(Class type, String id) {
this(Collections.singletonList(new ContextEntry(type, id)));
}
@@ -21,12 +18,7 @@ public class ConcurrentModificationException extends RuntimeException implements
}
private ConcurrentModificationException(List<ContextEntry> context) {
super(createMessage(context));
this.context = context;
}
public List<ContextEntry> getContext() {
return unmodifiableList(context);
super(context, createMessage(context));
}
@Override

View File

@@ -2,10 +2,20 @@ package sonia.scm;
import java.util.List;
public interface ExceptionWithContext {
List<ContextEntry> getContext();
import static java.util.Collections.unmodifiableList;
String getMessage();
public abstract class ExceptionWithContext extends RuntimeException {
String getCode();
private final List<ContextEntry> context;
public ExceptionWithContext(List<ContextEntry> context, String message) {
super(message);
this.context = context;
}
public List<ContextEntry> getContext() {
return unmodifiableList(context);
}
public abstract String getCode();
}

View File

@@ -1,21 +1,14 @@
package sonia.scm;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining;
public class NotFoundException extends RuntimeException implements ExceptionWithContext {
public class NotFoundException extends ExceptionWithContext {
private static final String CODE = "AGR7UzkhA1";
private final List<ContextEntry> context;
public NotFoundException(Class type, String id) {
this(Collections.singletonList(new ContextEntry(type, id)));
}
@@ -29,12 +22,7 @@ public class NotFoundException extends RuntimeException implements ExceptionWith
}
private NotFoundException(List<ContextEntry> context) {
super(createMessage(context));
this.context = context;
}
public List<ContextEntry> getContext() {
return unmodifiableList(context);
super(context, createMessage(context));
}
@Override

View File

@@ -1,6 +1,5 @@
package sonia.scm.api.rest;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ExceptionWithContext;
@@ -10,7 +9,7 @@ import sonia.scm.web.VndMediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
public class ContextualExceptionMapper<E extends Throwable & ExceptionWithContext> implements ExceptionMapper<E> {
public class ContextualExceptionMapper<E extends ExceptionWithContext> implements ExceptionMapper<E> {
private static final Logger logger = LoggerFactory.getLogger(ContextualExceptionMapper.class);