Skip to content

Commit 4a77a24

Browse files
authored
Merge pull request #39 from aboutbits/exception-message-interface
add interface to use enums as constant message references providing a…
2 parents b860e35 + 05aadca commit 4a77a24

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/main/java/it/aboutbits/springboot/toolbox/exception/ClientRuntimeException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ public class ClientRuntimeException extends RuntimeException {
44
public ClientRuntimeException() {
55
}
66

7+
public ClientRuntimeException(ExceptionMessageDefinition message) {
8+
super(message.code());
9+
}
10+
711
public ClientRuntimeException(String message) {
812
super(message);
913
}
1014

15+
public ClientRuntimeException(ExceptionMessageDefinition message, Throwable cause) {
16+
super(message.code(), cause);
17+
}
18+
1119
public ClientRuntimeException(String message, Throwable cause) {
1220
super(message, cause);
1321
}
@@ -16,6 +24,15 @@ public ClientRuntimeException(Throwable cause) {
1624
super(cause);
1725
}
1826

27+
public ClientRuntimeException(
28+
ExceptionMessageDefinition message,
29+
Throwable cause,
30+
boolean enableSuppression,
31+
boolean writableStackTrace
32+
) {
33+
super(message.code(), cause, enableSuppression, writableStackTrace);
34+
}
35+
1936
public ClientRuntimeException(
2037
String message,
2138
Throwable cause,

src/main/java/it/aboutbits/springboot/toolbox/exception/ConstraintViolationExceptionBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jakarta.validation.metadata.ConstraintDescriptor;
77
import lombok.NonNull;
88
import org.hibernate.validator.internal.engine.path.PathImpl;
9+
import org.springframework.lang.Nullable;
910

1011
import java.util.HashSet;
1112
import java.util.Set;
@@ -21,7 +22,14 @@ public static ConstraintViolationExceptionBuilder constraintViolation() {
2122
return new ConstraintViolationExceptionBuilder();
2223
}
2324

24-
public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, String message) {
25+
public ConstraintViolationExceptionBuilder causedBy(
26+
@NonNull String propertyPath,
27+
@NonNull ExceptionMessageDefinition message
28+
) {
29+
return causedBy(propertyPath, message.code());
30+
}
31+
32+
public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, @Nullable String message) {
2533
var constraintViolation = new CustomConstraintViolation(message, PathImpl.createPathFromString(propertyPath));
2634
constraintViolations.add(constraintViolation);
2735
return this;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package it.aboutbits.springboot.toolbox.exception;
2+
3+
/**
4+
* Use this to create a class or enum that defines the exception messages.
5+
* This way, the messages can be referenced by a constant rather than using plain strings.
6+
* <p>
7+
* Example:
8+
* {@snippet :
9+
* @Getter
10+
* @Accessors(fluent = true)
11+
* @RequiredArgsConstructor
12+
* enum MyExceptionMessages implements ExceptionMessageDefinition {
13+
* SHARED_ERROR_GENERAL("shared.error.general");
14+
*
15+
* private final String code;
16+
* }
17+
* }
18+
*/
19+
public interface ExceptionMessageDefinition {
20+
String code();
21+
}

src/main/java/it/aboutbits/springboot/toolbox/exception/ServerRuntimeException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ public class ServerRuntimeException extends RuntimeException {
44
public ServerRuntimeException() {
55
}
66

7+
public ServerRuntimeException(ExceptionMessageDefinition message) {
8+
super(message.code());
9+
}
10+
711
public ServerRuntimeException(String message) {
812
super(message);
913
}
1014

15+
public ServerRuntimeException(ExceptionMessageDefinition message, Throwable cause) {
16+
super(message.code(), cause);
17+
}
18+
1119
public ServerRuntimeException(String message, Throwable cause) {
1220
super(message, cause);
1321
}
@@ -16,6 +24,15 @@ public ServerRuntimeException(Throwable cause) {
1624
super(cause);
1725
}
1826

27+
public ServerRuntimeException(
28+
ExceptionMessageDefinition message,
29+
Throwable cause,
30+
boolean enableSuppression,
31+
boolean writableStackTrace
32+
) {
33+
super(message.code(), cause, enableSuppression, writableStackTrace);
34+
}
35+
1936
public ServerRuntimeException(
2037
String message,
2138
Throwable cause,

0 commit comments

Comments
 (0)