Skip to content

Commit

Permalink
Refactored and implemented the handling of special HTTP exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jan 10, 2015
1 parent b540130 commit b41b1bb
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
10 changes: 9 additions & 1 deletion rapidoid-app/src/main/java/org/rapidoid/app/AppPageGeneric.java
Expand Up @@ -32,6 +32,7 @@
import org.rapidoid.html.tag.ATag;
import org.rapidoid.html.tag.FormTag;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.HttpExchangeException;
import org.rapidoid.http.HttpExchangeHolder;
import org.rapidoid.oauth.OAuth;
import org.rapidoid.oauth.OAuthProvider;
Expand Down Expand Up @@ -339,7 +340,14 @@ protected boolean isScreenAllowed(Class<?> screenCls) {
}

public void on(String cmd, Object[] args) {
Pages.callCmdHandler(x, screen, new Cmd(cmd, args));
try {
Pages.callCmdHandler(x, screen, new Cmd(cmd, args));
} catch (Exception e) {
if (U.rootCause(e) instanceof HttpExchangeException) {
Pages.store(x, screen);
throw U.rte(e);
}
}
}

}
Expand Up @@ -220,8 +220,8 @@ public Data subpath_() {
return x.subpath_();
}

public HttpExchangeException asError() {
return x.asError();
public HttpExchangeException error() {
return x.error();
}

public String query() {
Expand Down
2 changes: 1 addition & 1 deletion rapidoid-app/src/main/java/org/rapidoid/app/Screen.java
Expand Up @@ -78,7 +78,7 @@ protected <T> T entity() {
Object entity = DB.getIfExists(id);

if (entity == null) {
throw ctx().notFound().asError();
throw ctx().notFound().error();
}

return (T) entity;
Expand Down
Expand Up @@ -20,18 +20,16 @@
* #L%
*/

@SuppressWarnings("serial")
public class HttpExchangeException extends RuntimeException {

private static final long serialVersionUID = 548339907490281755L;
private static final HttpExchangeException INSTANCE = new HttpExchangeException();

private final HttpExchange x;

public HttpExchangeException(HttpExchange x) {
this.x = x;
private HttpExchangeException() {
}

public HttpExchange getHttpExchange() {
return x;
public static HttpExchangeException get() {
return INSTANCE;
}

}
Expand Up @@ -36,7 +36,7 @@ public interface HttpExchangeHeaders extends HttpExchangeBody {

HttpExchangeHeaders notFound();

HttpExchangeException asError();
HttpExchangeException error();

HttpExchangeHeaders plain();

Expand Down
Expand Up @@ -884,7 +884,7 @@ public synchronized boolean hasError() {
}

@Override
public synchronized Throwable error() {
public synchronized Throwable getError() {
return error;
}

Expand Down Expand Up @@ -957,7 +957,8 @@ public synchronized HttpExchangeBody goBack(int steps) {
}
}

return redirect(dest);
redirect(dest);
throw error();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -987,8 +988,8 @@ public synchronized void init(HttpResponses responses, HttpSession session, Rout
}

@Override
public HttpExchangeException asError() {
return new HttpExchangeException(this);
public HttpExchangeException error() {
return HttpExchangeException.get();
}

}
Expand Up @@ -26,6 +26,6 @@ public interface HttpInterception extends Runnable {

boolean hasError();

Throwable error();
Throwable getError();

}
14 changes: 10 additions & 4 deletions rapidoid-http/src/main/java/org/rapidoid/http/HttpProtocol.java
Expand Up @@ -83,7 +83,7 @@ private void processRequest(HttpExchangeImpl x) {
}

if (x.hasError()) {
handleError(x, x.error());
handleError(x, x.getError());
} else if (!x.isAsync()) {
x.completeResponse();
}
Expand All @@ -94,9 +94,15 @@ private void processRequest(HttpExchangeImpl x) {
}

private void handleError(HttpExchangeImpl x, Throwable e) {
U.error("Internal server error!", "request", x, "error", e);
x.errorResponse(e);
x.completeResponse();
Throwable cause = U.rootCause(e);
if (cause instanceof HttpExchangeException) {
// redirect, notFound etc.
x.completeResponse();
} else {
U.error("Internal server error!", "request", x, "error", cause);
x.errorResponse(e);
x.completeResponse();
}
}

public static void processResponse(HttpExchange xch, Object res) {
Expand Down
45 changes: 35 additions & 10 deletions rapidoid-pages/src/main/java/org/rapidoid/pages/Pages.java
Expand Up @@ -30,6 +30,7 @@
import org.rapidoid.html.Tags;
import org.rapidoid.http.HTTPServer;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.HttpExchangeException;
import org.rapidoid.inject.IoC;
import org.rapidoid.json.JSON;
import org.rapidoid.pages.impl.BuiltInCmdHandler;
Expand Down Expand Up @@ -247,21 +248,45 @@ public static Object emit(HttpExchange x, Object view) {

Pages.load(x, view);

boolean processView = true;

if (cmd != null) {
callCmdHandler(x, view, cmd);
try {
callCmdHandler(x, view, cmd);
} catch (Exception e) {
Throwable cause = U.rootCause(e);
if (cause instanceof HttpExchangeException) {
processView = false;
} else {
x.json();
return U.map("!error", "Error occured while processing command: " + cmd.name);
}
}
}

ctx = Tags.context();
x.sessionSet(Pages.SESSION_CTX, ctx);

Object content = Pages.contentOf(x, view);

if (content == null || content instanceof HttpExchange) {
return content;
String html;
if (processView) {
ctx = Tags.context();
x.sessionSet(Pages.SESSION_CTX, ctx);
Object content;
try {
content = Pages.contentOf(x, view);
if (content == null || content instanceof HttpExchange) {
return content;
}
} catch (Exception e) {
Throwable cause = U.rootCause(e);
if (cause instanceof HttpExchangeException) {
return null;
} else {
throw U.rte(e);
}
}
html = PageRenderer.get().toHTML(ctx, content, x);
} else {
html = "Error!";
}

String html = PageRenderer.get().toHTML(ctx, content, x);

Pages.store(x, view);

if (x.redirectUrl() != null) {
Expand Down

0 comments on commit b41b1bb

Please sign in to comment.