I want to model some error codes. The classic enum approach
public enum FileError implement FormattedError {
_10 ("some error with parameters [{0}] and [{1}]"),
_20 ("some other error");
private final String description;
private Error(String description) {
this.description = description;
}
public String getDescription(Object... parameters) {
return // logic to format message
}
...
}
it is not good for me because I have many modules, each with it's error codes and I don't want to copy and paste the boilerplate (constructors, getters, logic..) in all these enums.
So I went for a "manual" enum implemented like this
public class FileError extends BaseError {
public final static FileError _10 = new FileError (10, "some message with parameters [{0}] and [{1}]");
public final static FileError _20 = new FileError (20, "some other message");
}
where I can define my logic in BaseError and reuse it.
but it is still bad because there is no way to link the variable name to the number (_10 to 10) and people copy pasting might reuse the same number without noticing. I could add a test to check that via reflection but then how do I enforce people to use that test for their implementations.
so do you guys have a better idea about how I could achieve this ?
[edit] please keep in mind that I don't want to put error codes in properties files because I want the ide to link error codes in the code with their message.