From 7e6cfbbecb9f1111c6c47930b2ee776382a2a436 Mon Sep 17 00:00:00 2001 From: Andreas Hufler Date: Mon, 30 Sep 2024 11:11:16 +0200 Subject: [PATCH] add exceptions and builder --- .../exception/ClientRuntimeException.java | 27 +++++ .../ConstraintViolationExceptionBuilder.java | 99 +++++++++++++++++++ .../exception/ServerRuntimeException.java | 27 +++++ 3 files changed, 153 insertions(+) create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java create mode 100644 src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java diff --git a/src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java b/src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java new file mode 100644 index 0000000..c0de138 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java @@ -0,0 +1,27 @@ +package it.aboutbits.springboot.toolbox.exception; + +public class ClientRuntimeException extends RuntimeException { + public ClientRuntimeException() { + } + + public ClientRuntimeException(String message) { + super(message); + } + + public ClientRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public ClientRuntimeException(Throwable cause) { + super(cause); + } + + public ClientRuntimeException( + String message, + Throwable cause, + boolean enableSuppression, + boolean writableStackTrace + ) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java b/src/main/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java new file mode 100644 index 0000000..14d0c38 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java @@ -0,0 +1,99 @@ +package it.aboutbits.springboot.toolbox.exception; + +import jakarta.validation.ConstraintViolation; +import jakarta.validation.ConstraintViolationException; +import jakarta.validation.Path; +import jakarta.validation.metadata.ConstraintDescriptor; +import lombok.NonNull; +import org.hibernate.validator.internal.engine.path.PathImpl; + +import java.util.HashSet; +import java.util.Set; + +public final class ConstraintViolationExceptionBuilder { + private final Set> constraintViolations = new HashSet<>(); + + private ConstraintViolationExceptionBuilder() { + + } + + public static ConstraintViolationExceptionBuilder constraintViolation() { + return new ConstraintViolationExceptionBuilder(); + } + + public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, String message) { + var constraintViolation = new CustomConstraintViolation(message, PathImpl.createPathFromString(propertyPath)); + constraintViolations.add(constraintViolation); + return this; + } + + public ConstraintViolationExceptionBuilder causedBy(@NonNull ConstraintViolationException e) { + constraintViolations.addAll(e.getConstraintViolations()); + return this; + } + + public ConstraintViolationException create() { + return new ConstraintViolationException(constraintViolations); + } + + public boolean hasCauses() { + return !constraintViolations.isEmpty(); + } + + private record CustomConstraintViolation(String message, Path propertyPath) implements ConstraintViolation { + @Override + public String getMessage() { + return message; + } + + @Override + public String getMessageTemplate() { + return null; + } + + @Override + public Object getRootBean() { + return null; + } + + @Override + public Class getRootBeanClass() { + return null; + } + + @Override + public Object getLeafBean() { + return null; + } + + @Override + public Object[] getExecutableParameters() { + return new Object[0]; + } + + @Override + public Object getExecutableReturnValue() { + return null; + } + + @Override + public Path getPropertyPath() { + return propertyPath; + } + + @Override + public Object getInvalidValue() { + return null; + } + + @Override + public ConstraintDescriptor getConstraintDescriptor() { + return null; + } + + @Override + public U unwrap(Class type) { + return null; + } + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java b/src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java new file mode 100644 index 0000000..40aea6a --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java @@ -0,0 +1,27 @@ +package it.aboutbits.springboot.toolbox.exception; + +public class ServerRuntimeException extends RuntimeException { + public ServerRuntimeException() { + } + + public ServerRuntimeException(String message) { + super(message); + } + + public ServerRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public ServerRuntimeException(Throwable cause) { + super(cause); + } + + public ServerRuntimeException( + String message, + Throwable cause, + boolean enableSuppression, + boolean writableStackTrace + ) { + super(message, cause, enableSuppression, writableStackTrace); + } +}