public interface OpenErrorCode {
String name();
int getStatus();
default OpenRuntimeException runtimeException(Throwable cause, Object... params) {
return new OpenRuntimeException(this, cause, params);
}
default OpenRuntimeException runtimeException(Object... params) {
return new OpenRuntimeException(this, params);
}
default OpenRuntimeException runtimeException(Throwable cause) {
return new OpenRuntimeException(this, cause);
}
default String getMessage(Locale locale, Object... params) {
String message = "NO MESSAGE!!!";
if (locale == null) {
locale = Locale.getDefault();
}
try {
String formatter = Objects.equals("OpenExceptionEnum", this.getClass().getSimpleName()) ?
"META-INF.{0}" : "i18n.exception.{0}";
ResourceBundle bundle = ResourceBundle
.getBundle(MessageFormat.format(formatter,
this.getClass().getSimpleName()), locale);
message = bundle.getString(this.name());
return new MessageFormat(message).format(params);
} catch (Throwable e) {
if (!(e instanceof MissingResourceException)) {
e.printStackTrace();
}
message = Objects.isNull(params) ? message : new MessageFormat("{0}").format(params);
return message;
}
}
default String getMessage(Object... params) {
return getMessage(Locale.getDefault(), params);
}
}