Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exceptions and builder #12

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}