Skip to content
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
Expand Up @@ -4,10 +4,18 @@ public class ClientRuntimeException extends RuntimeException {
public ClientRuntimeException() {
}

public ClientRuntimeException(ExceptionMessageDefinition message) {
super(message.code());
}

public ClientRuntimeException(String message) {
super(message);
}

public ClientRuntimeException(ExceptionMessageDefinition message, Throwable cause) {
super(message.code(), cause);
}

public ClientRuntimeException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -16,6 +24,15 @@ public ClientRuntimeException(Throwable cause) {
super(cause);
}

public ClientRuntimeException(
ExceptionMessageDefinition message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace
) {
super(message.code(), cause, enableSuppression, writableStackTrace);
}

public ClientRuntimeException(
String message,
Throwable cause,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.validation.metadata.ConstraintDescriptor;
import lombok.NonNull;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.lang.Nullable;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -21,7 +22,14 @@ public static ConstraintViolationExceptionBuilder constraintViolation() {
return new ConstraintViolationExceptionBuilder();
}

public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, String message) {
public ConstraintViolationExceptionBuilder causedBy(
@NonNull String propertyPath,
@NonNull ExceptionMessageDefinition message
) {
return causedBy(propertyPath, message.code());
}

public ConstraintViolationExceptionBuilder causedBy(@NonNull String propertyPath, @Nullable String message) {
var constraintViolation = new CustomConstraintViolation(message, PathImpl.createPathFromString(propertyPath));
constraintViolations.add(constraintViolation);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.aboutbits.springboot.toolbox.exception;

/**
* Use this to create a class or enum that defines the exception messages.
* This way, the messages can be referenced by a constant rather than using plain strings.
* <p>
* Example:
* {@snippet :
* @Getter
* @Accessors(fluent = true)
* @RequiredArgsConstructor
* enum MyExceptionMessages implements ExceptionMessageDefinition {
* SHARED_ERROR_GENERAL("shared.error.general");
*
* private final String code;
* }
* }
*/
Comment thread
SirCotare marked this conversation as resolved.
public interface ExceptionMessageDefinition {
String code();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ public class ServerRuntimeException extends RuntimeException {
public ServerRuntimeException() {
}

public ServerRuntimeException(ExceptionMessageDefinition message) {
super(message.code());
}

public ServerRuntimeException(String message) {
super(message);
}

public ServerRuntimeException(ExceptionMessageDefinition message, Throwable cause) {
super(message.code(), cause);
}

public ServerRuntimeException(String message, Throwable cause) {
super(message, cause);
}
Expand All @@ -16,6 +24,15 @@ public ServerRuntimeException(Throwable cause) {
super(cause);
}

public ServerRuntimeException(
ExceptionMessageDefinition message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace
) {
super(message.code(), cause, enableSuppression, writableStackTrace);
}

public ServerRuntimeException(
String message,
Throwable cause,
Expand Down