[New] Added new UserMessageException to use in Services
This commit is contained in:
parent
dc7972927b
commit
7005452a8c
|
@ -25,6 +25,7 @@ import li.strolch.agent.api.StrolchAgent;
|
||||||
import li.strolch.agent.api.StrolchComponent;
|
import li.strolch.agent.api.StrolchComponent;
|
||||||
import li.strolch.agent.api.StrolchRealm;
|
import li.strolch.agent.api.StrolchRealm;
|
||||||
import li.strolch.exception.StrolchException;
|
import li.strolch.exception.StrolchException;
|
||||||
|
import li.strolch.model.UserMessageException;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.privilege.base.PrivilegeException;
|
import li.strolch.privilege.base.PrivilegeException;
|
||||||
import li.strolch.privilege.handler.SystemAction;
|
import li.strolch.privilege.handler.SystemAction;
|
||||||
|
@ -572,6 +573,10 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
||||||
result.setState(ServiceResultState.EXCEPTION);
|
result.setState(ServiceResultState.EXCEPTION);
|
||||||
result.setMessage(e.getMessage());
|
result.setMessage(e.getMessage());
|
||||||
result.setThrowable(e);
|
result.setThrowable(e);
|
||||||
|
|
||||||
|
if (e instanceof UserMessageException)
|
||||||
|
result.setI18nMessage(((UserMessageException) e).getI18nMsg());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import static li.strolch.utils.helper.ExceptionHelper.formatException;
|
||||||
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
|
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
|
||||||
import static li.strolch.utils.helper.StringHelper.isEmpty;
|
import static li.strolch.utils.helper.StringHelper.isEmpty;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
@ -56,6 +57,18 @@ public class ServiceResult {
|
||||||
this.throwable = throwable;
|
this.throwable = throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ServiceResult(ServiceResultState state, I18nMessage i18nMessage) {
|
||||||
|
this.state = state;
|
||||||
|
this.message = i18nMessage.getMessage(Locale.getDefault());
|
||||||
|
this.i18nMessage = i18nMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceResult(ServiceResultState state, String message, I18nMessage i18nMessage) {
|
||||||
|
this.state = state;
|
||||||
|
this.message = message;
|
||||||
|
this.i18nMessage = i18nMessage;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the state is {@link ServiceResultState#SUCCESS}
|
* @return true if the state is {@link ServiceResultState#SUCCESS}
|
||||||
*/
|
*/
|
||||||
|
@ -116,15 +129,15 @@ public class ServiceResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult success() {
|
public static ServiceResult success() {
|
||||||
return new ServiceResult(ServiceResultState.SUCCESS, null, null);
|
return new ServiceResult(ServiceResultState.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult success(String msg) {
|
public static ServiceResult success(String msg) {
|
||||||
return new ServiceResult(ServiceResultState.SUCCESS, msg, null);
|
return new ServiceResult(ServiceResultState.SUCCESS, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult warning(String warning) {
|
public static ServiceResult warning(String warning) {
|
||||||
return new ServiceResult(ServiceResultState.WARNING, warning, null);
|
return new ServiceResult(ServiceResultState.WARNING, warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult warning(String warning, Throwable t) {
|
public static ServiceResult warning(String warning, Throwable t) {
|
||||||
|
@ -132,7 +145,15 @@ public class ServiceResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult error(String error) {
|
public static ServiceResult error(String error) {
|
||||||
return new ServiceResult(ServiceResultState.FAILED, error, null);
|
return new ServiceResult(ServiceResultState.FAILED, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServiceResult error(I18nMessage i18nMessage) {
|
||||||
|
return new ServiceResult(ServiceResultState.FAILED, i18nMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServiceResult error(String message, I18nMessage i18nMessage) {
|
||||||
|
return new ServiceResult(ServiceResultState.FAILED, message, i18nMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceResult failed(String error, Throwable t) {
|
public static ServiceResult failed(String error, Throwable t) {
|
||||||
|
@ -192,6 +213,10 @@ public class ServiceResult {
|
||||||
return this.i18nMessage;
|
return this.i18nMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setI18nMessage(I18nMessage i18nMessage) {
|
||||||
|
this.i18nMessage = i18nMessage;
|
||||||
|
}
|
||||||
|
|
||||||
public JsonObject toJson() {
|
public JsonObject toJson() {
|
||||||
JsonObject json = new JsonObject();
|
JsonObject json = new JsonObject();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package li.strolch.model;
|
||||||
|
|
||||||
|
import li.strolch.utils.I18nMessage;
|
||||||
|
import li.strolch.utils.dbc.DBC;
|
||||||
|
|
||||||
|
public class UserMessageException extends Exception {
|
||||||
|
|
||||||
|
private final I18nMessage i18nMsg;
|
||||||
|
|
||||||
|
public UserMessageException(I18nMessage i18nMsg) {
|
||||||
|
DBC.PRE.assertNotNull("i18nMsg must not be null!", i18nMsg);
|
||||||
|
this.i18nMsg = i18nMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public I18nMessage getI18nMsg() {
|
||||||
|
return this.i18nMsg;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue