Skip to content

Commit

Permalink
add exceptions and builder
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCotare committed Sep 30, 2024
1 parent 94724a1 commit 7e6cfbb
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ConstraintViolation<?>> 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<Object> {
@Override
public String getMessage() {
return message;
}

@Override
public String getMessageTemplate() {
return null;
}

@Override
public Object getRootBean() {
return null;
}

@Override
public Class<Object> 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> U unwrap(Class<U> type) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 7e6cfbb

Please sign in to comment.