-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from aboutbits/add-exceptions
add exceptions and builder
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...n/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |