[New] Using StrolchElementNotFoundException for missing elements, for 404 in REST

This commit is contained in:
Robert von Burg 2019-09-27 09:59:44 +02:00
parent ca1b93d4ea
commit 9c08d87be2
3 changed files with 22 additions and 4 deletions

View File

@ -24,6 +24,7 @@ import java.util.stream.Stream;
import li.strolch.agent.api.ElementMap;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.exception.StrolchElementNotFoundException;
import li.strolch.exception.StrolchException;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Version;
@ -88,7 +89,7 @@ public abstract class TransientElementMap<T extends StrolchRootElement> implemen
T t = getBy(tx, StrolchConstants.TEMPLATE, type);
if (assertExists && t == null) {
String msg = "The template with type \"{0}\" does not exist!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, type));
throw new StrolchElementNotFoundException(MessageFormat.format(msg, type));
}
if (t == null)
@ -117,7 +118,7 @@ public abstract class TransientElementMap<T extends StrolchRootElement> implemen
if (assertExists && t == null) {
String msg = "The element with type \"{0}\" and id \"{1}\" does not exist!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, type, id));
throw new StrolchElementNotFoundException(MessageFormat.format(msg, type, id));
}
if (t == null)
@ -136,7 +137,7 @@ public abstract class TransientElementMap<T extends StrolchRootElement> implemen
T t = getBy(tx, type, id, false);
if (assertExists && t == null) {
String msg = "The element with type \"{0}\" and id \"{1}\" does not exist for param \"{2}\""; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, type, id, refP.getLocator()));
throw new StrolchElementNotFoundException(MessageFormat.format(msg, type, id, refP.getLocator()));
}
return t;
}
@ -154,7 +155,7 @@ public abstract class TransientElementMap<T extends StrolchRootElement> implemen
T t = getBy(tx, type, id, false);
if (assertExists && t == null) {
String msg = "The element with type \"{0}\" and id \"{1}\" does not exist for param \"{2}\""; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, type, id, refP.getLocator()));
throw new StrolchElementNotFoundException(MessageFormat.format(msg, type, id, refP.getLocator()));
}
return t;
}) //

View File

@ -0,0 +1,12 @@
package li.strolch.exception;
public class StrolchElementNotFoundException extends StrolchModelException {
public StrolchElementNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public StrolchElementNotFoundException(String message) {
super(message);
}
}

View File

@ -13,6 +13,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import li.strolch.exception.StrolchElementNotFoundException;
import li.strolch.privilege.base.AccessDeniedException;
import li.strolch.privilege.base.PrivilegeException;
import li.strolch.privilege.base.PrivilegeModelException;
@ -140,6 +141,8 @@ public class ResponseUtil {
status = Status.INTERNAL_SERVER_ERROR;
} else if (t instanceof PrivilegeException) {
status = Status.UNAUTHORIZED;
} else if (t instanceof StrolchElementNotFoundException) {
status = Status.NOT_FOUND;
} else {
status = Status.INTERNAL_SERVER_ERROR;
}
@ -150,6 +153,8 @@ public class ResponseUtil {
public static Response toResponse(Throwable t) {
if (t instanceof AccessDeniedException) {
return ResponseUtil.toResponse(Status.FORBIDDEN, t);
} else if (t instanceof StrolchElementNotFoundException) {
return ResponseUtil.toResponse(Status.NOT_FOUND, t);
} else if (t instanceof PrivilegeModelException) {
return ResponseUtil.toResponse(Status.INTERNAL_SERVER_ERROR, t);
} else if (t instanceof PrivilegeException) {