Skip to content

Commit 208f9db

Browse files
authored
Merge pull request #12 from aboutbits/add-exceptions
add exceptions and builder
2 parents 94724a1 + 7e6cfbb commit 208f9db

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package it.aboutbits.springboot.toolbox.exception;
2+
3+
public class ClientRuntimeException extends RuntimeException {
4+
public ClientRuntimeException() {
5+
}
6+
7+
public ClientRuntimeException(String message) {
8+
super(message);
9+
}
10+
11+
public ClientRuntimeException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ClientRuntimeException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public ClientRuntimeException(
20+
String message,
21+
Throwable cause,
22+
boolean enableSuppression,
23+
boolean writableStackTrace
24+
) {
25+
super(message, cause, enableSuppression, writableStackTrace);
26+
}
27+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package it.aboutbits.springboot.toolbox.exception;
2+
3+
import jakarta.validation.ConstraintViolation;
4+
import jakarta.validation.ConstraintViolationException;
5+
import jakarta.validation.Path;
6+
import jakarta.validation.metadata.ConstraintDescriptor;
7+
import lombok.NonNull;
8+
import org.hibernate.validator.internal.engine.path.PathImpl;
9+
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
public final class ConstraintViolationExceptionBuilder {
14+
private final Set<ConstraintViolation<?>> constraintViolations = new HashSet<>();
15+
16+
private ConstraintViolationExceptionBuilder() {
17+
18+
}
19+
20+
public static ConstraintViolationExceptionBuilder constraintViolation() {
21+
return new ConstraintViolationExceptionBuilder();
22+
}
23+
24+
public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, String message) {
25+
var constraintViolation = new CustomConstraintViolation(message, PathImpl.createPathFromString(propertyPath));
26+
constraintViolations.add(constraintViolation);
27+
return this;
28+
}
29+
30+
public ConstraintViolationExceptionBuilder causedBy(@NonNull ConstraintViolationException e) {
31+
constraintViolations.addAll(e.getConstraintViolations());
32+
return this;
33+
}
34+
35+
public ConstraintViolationException create() {
36+
return new ConstraintViolationException(constraintViolations);
37+
}
38+
39+
public boolean hasCauses() {
40+
return !constraintViolations.isEmpty();
41+
}
42+
43+
private record CustomConstraintViolation(String message, Path propertyPath) implements ConstraintViolation<Object> {
44+
@Override
45+
public String getMessage() {
46+
return message;
47+
}
48+
49+
@Override
50+
public String getMessageTemplate() {
51+
return null;
52+
}
53+
54+
@Override
55+
public Object getRootBean() {
56+
return null;
57+
}
58+
59+
@Override
60+
public Class<Object> getRootBeanClass() {
61+
return null;
62+
}
63+
64+
@Override
65+
public Object getLeafBean() {
66+
return null;
67+
}
68+
69+
@Override
70+
public Object[] getExecutableParameters() {
71+
return new Object[0];
72+
}
73+
74+
@Override
75+
public Object getExecutableReturnValue() {
76+
return null;
77+
}
78+
79+
@Override
80+
public Path getPropertyPath() {
81+
return propertyPath;
82+
}
83+
84+
@Override
85+
public Object getInvalidValue() {
86+
return null;
87+
}
88+
89+
@Override
90+
public ConstraintDescriptor<?> getConstraintDescriptor() {
91+
return null;
92+
}
93+
94+
@Override
95+
public <U> U unwrap(Class<U> type) {
96+
return null;
97+
}
98+
}
99+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package it.aboutbits.springboot.toolbox.exception;
2+
3+
public class ServerRuntimeException extends RuntimeException {
4+
public ServerRuntimeException() {
5+
}
6+
7+
public ServerRuntimeException(String message) {
8+
super(message);
9+
}
10+
11+
public ServerRuntimeException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ServerRuntimeException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public ServerRuntimeException(
20+
String message,
21+
Throwable cause,
22+
boolean enableSuppression,
23+
boolean writableStackTrace
24+
) {
25+
super(message, cause, enableSuppression, writableStackTrace);
26+
}
27+
}

0 commit comments

Comments
 (0)