generator.server.springboot.mvc.internationalized-errors.main.ApplicationException.mustache Maven / Gradle / Ivy
package {{ packageName }}.shared.error.domain;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class {{ baseName }}Exception extends RuntimeException {
private final ErrorKey key;
private final ErrorStatus status;
private final Map parameters;
protected {{ baseName }}Exception({{ baseName }}ExceptionBuilder builder) {
super(buildMessage(builder), builder.cause);
key = buildKey(builder);
status = buildStatus(builder);
parameters = Collections.unmodifiableMap(builder.parameters);
}
private static String buildMessage({{ baseName }}ExceptionBuilder builder) {
Assert.notNull("builder", builder);
if (builder.message == null) {
return "An error occurred";
}
return builder.message;
}
private ErrorKey buildKey({{ baseName }}ExceptionBuilder builder) {
if (builder.key == null) {
return StandardErrorKey.INTERNAL_SERVER_ERROR;
}
return builder.key;
}
private ErrorStatus buildStatus({{ baseName }}ExceptionBuilder builder) {
if (builder.status == null) {
return defaultStatus();
}
return builder.status;
}
private ErrorStatus defaultStatus() {
return Stream.of(Thread.currentThread().getStackTrace())
.map(StackTraceElement::getClassName)
.filter(inProject())
.filter(notCurrentException())
.findFirst()
.filter(inPrimary())
.map(className -> ErrorStatus.BAD_REQUEST)
.orElse(ErrorStatus.INTERNAL_SERVER_ERROR);
}
private Predicate inProject() {
return className -> className.startsWith("{{ packageName }}");
}
private Predicate notCurrentException() {
return className -> !className.contains(this.getClass().getName());
}
private Predicate inPrimary() {
return className -> className.contains(".primary");
}
public static {{ baseName }}ExceptionBuilder internalServerError(ErrorKey key) {
return builder(key).status(ErrorStatus.INTERNAL_SERVER_ERROR);
}
public static {{ baseName }}ExceptionBuilder badRequest(ErrorKey key) {
return builder(key).status(ErrorStatus.BAD_REQUEST);
}
public static {{ baseName }}Exception technicalError(String message) {
return technicalError(message, null);
}
public static {{ baseName }}Exception technicalError(String message, Throwable cause) {
return builder(StandardErrorKey.INTERNAL_SERVER_ERROR).message(message).cause(cause).build();
}
public static {{ baseName }}ExceptionBuilder builder(ErrorKey key) {
return new {{ baseName }}ExceptionBuilder(key);
}
public ErrorKey key() {
return key;
}
public ErrorStatus status() {
return status;
}
public Map parameters() {
return parameters;
}
public static final class {{ baseName }}ExceptionBuilder {
private final ErrorKey key;
private final Map parameters = new HashMap<>();
private String message;
private Throwable cause;
private ErrorStatus status;
private {{ baseName }}ExceptionBuilder(ErrorKey key) {
this.key = key;
}
public {{ baseName }}ExceptionBuilder message(String message) {
this.message = message;
return this;
}
public {{ baseName }}ExceptionBuilder cause(Throwable cause) {
this.cause = cause;
return this;
}
public {{ baseName }}ExceptionBuilder addParameters(Map parameters) {
Assert.notNull("parameters", parameters);
parameters.forEach(this::addParameter);
return this;
}
public {{ baseName }}ExceptionBuilder addParameter(String key, String value) {
Assert.notBlank("key", key);
Assert.notNull("value", value);
parameters.put(key, value);
return this;
}
public {{ baseName }}ExceptionBuilder status(ErrorStatus status) {
this.status = status;
return this;
}
public {{ baseName }}Exception build() {
return new {{ baseName }}Exception(this);
}
}
}