Spring boot concern about custom exception message

Viewed 35

I have the following class:

@SoapFault(faultCode = FaultCode.CLIENT)
public class PasswordDigitsException extends RuntimeException {

    private static final String errorMessage;

    static {
        if (PasswordConstraints.MIN_DIGITS == 1) {
            errorMessage = String.format("Password must contain at least %d digit",
                    PasswordConstraints.MIN_DIGITS);
        } else {
            errorMessage = String.format("Password must contain at least %d digits",
                    PasswordConstraints.MIN_DIGITS);
        }
    }


    public PasswordDigitsException() {
        super(errorMessage);
    }
}

And the folowing interface:

public interface PasswordConstraints {

    int MINIM_LENGTH = 6;
    int MAXIM_LENGTH = 20;

    int MIN_UPPER_CASE_LETTERS = 1;
    int MIN_LOWER_CASE_LETTERS = 1;

    int MIN_DIGITS = 1;

    int MIN_SPECIAL_CHARACTERS = 1;
}

From what I read, https://www.geeksforgeeks.org/static-blocks-in-java/, the code inside the static block is executed only once: the first time the class is loaded into memory. So the concern is: If I'll modify the interface fields, I would have to recompile this exception class as well, which I don't want to do.

The question is: is there any method I could keep the custom message without recompiling exception class? (and without going in service to send the message manual)

0 Answers
Related