Skip to content

Commit

Permalink
Simplified error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 11, 2016
1 parent fd0fcd3 commit b496bba
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.rapidoid.data.BufRange;
import org.rapidoid.data.BufRanges;
import org.rapidoid.data.KeyValueRanges;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.handler.HttpHandler;
import org.rapidoid.http.impl.*;
import org.rapidoid.http.processor.AbstractHttpProcessor;
Expand Down Expand Up @@ -52,17 +51,15 @@ public class FastHttp extends AbstractHttpProcessor {
private static final HttpParser HTTP_PARSER = new HttpParser();

private final HttpRoutesImpl[] routes;
private final Customization customization;

private final Map<String, Object> attributes = Coll.synchronizedMap();
private final Map<String, Map<String, Serializable>> sessions = Coll.mapOfMaps();

public FastHttp(HttpRoutesImpl... routes) {
super(null);
U.must(routes.length > 0, "Routes are missing!");

U.must(routes.length > 0, "Routes are missing!");
this.routes = routes;
this.customization = routes[0].custom();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -170,7 +167,7 @@ public void onRequest(Channel channel, boolean isGet, boolean isKeepAlive, BufRa
cookies = Collections.synchronizedMap(cookies);

req = new ReqImpl(this, channel, isKeepAlive, verb, uri, path, query, body, params, headers, cookies,
posted, files, contentType, segment, route);
posted, files, contentType, segment, route);

if (!attributes.isEmpty()) {
req.attrs().putAll(attributes);
Expand Down Expand Up @@ -204,7 +201,7 @@ public void onRequest(Channel channel, boolean isGet, boolean isKeepAlive, BufRa
private boolean handleError(Channel channel, boolean isKeepAlive, ReqImpl req, MediaType contentType, Throwable e) {
if (req != null) {
if (!req.isStopped()) {
HttpIO.errorAndDone(req, e, customization.errorHandler());
HttpIO.errorAndDone(req, e);
}
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req, Object extra
txMode = before(req, username, roles);

} catch (Throwable e) {
HttpIO.errorAndDone(req, e, Customization.of(req).errorHandler());
HttpIO.errorAndDone(req, e);
return HttpStatus.DONE;
}

Expand All @@ -88,7 +88,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req, Object extra

} catch (Throwable e) {
// if there was an error in the job scheduling:
HttpIO.errorAndDone(req, e, Customization.of(req).errorHandler());
HttpIO.errorAndDone(req, e);
return HttpStatus.DONE;
}

Expand Down Expand Up @@ -201,7 +201,7 @@ public void run() {
try {
JPA.transaction(handleRequest, txMode == TransactionMode.READ_ONLY);
} catch (Exception e) {
HttpIO.errorAndDone(req, e, Customization.of(req).errorHandler());
HttpIO.errorAndDone(req, e);
}
}
};
Expand Down Expand Up @@ -260,7 +260,7 @@ public void complete(Channel ctx, boolean isKeepAlive, Req req, Object result) {
}

if (result instanceof Throwable) {
HttpIO.errorAndDone(req, (Throwable) result, Customization.of(req).errorHandler());
HttpIO.errorAndDone(req, (Throwable) result);
return;

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req, Object extra
return HttpStatus.NOT_FOUND;

} catch (Exception e) {
HttpIO.errorAndDone(req, e, customization.errorHandler());
HttpIO.errorAndDone(req, e);
return HttpStatus.DONE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.rapidoid.http.HttpUtils;
import org.rapidoid.http.Req;
import org.rapidoid.http.Resp;
import org.rapidoid.http.customize.ErrorHandler;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.log.Log;
import org.rapidoid.net.abstracts.Channel;
import org.rapidoid.render.Template;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class HttpIO extends RapidoidThing {
public static final byte[] HTTP_200_OK = "HTTP/1.1 200 OK\r\n".getBytes();

public static final byte[] HTTP_400_BAD_REQUEST = "HTTP/1.1 400 Bad Request\r\nContent-Length: 12\r\n\r\nBad Request!"
.getBytes();
.getBytes();

private static final byte[] HEADER_SEP = ": ".getBytes();

Expand Down Expand Up @@ -113,22 +113,22 @@ public static void write200(Channel ctx, boolean isKeepAlive, MediaType contentT
writeContentLengthAndBody(ctx, content);
}

public static void error(Req req, Throwable error, ErrorHandler errorHandler) {
public static void error(final Req req, final Throwable error) {
Log.debug("HTTP handler error!", "error", error);

try {
Resp resp = req.response().code(500);
Object result = errorHandler.handleError(req, resp, error);
Object result = Customization.of(req).errorHandler().handleError(req, resp, error);
HttpUtils.resultToResponse(req, result);

} catch (Exception e) {
Log.error("The error handler had error!", e);
Log.error("An error occurred inside the error handler!", e);
HttpUtils.resultToResponse(req, HttpUtils.getErrorInfo(req.response(), e));
}
}

public static void errorAndDone(Req req, Throwable error, ErrorHandler errorHandler) {
error(req, error, errorHandler);
public static void errorAndDone(Req req, Throwable error) {
error(req, error);
// the Req object will do the rendering
req.done();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private byte[] responseToBytes() {
return response.renderToBytes();

} catch (Throwable e) {
HttpIO.error(this, e, Customization.of(this).errorHandler());
HttpIO.error(this, e);

try {
return response.renderToBytes();
Expand All @@ -533,7 +533,7 @@ private String validateResponse() {
}

if (response.result() == null && response.body() == null && response.redirect() == null
&& response.file() == null && response.raw() == null && !response().mvc()) {
&& response.file() == null && response.raw() == null && !response().mvc()) {
return "Response content wasn't provided!";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.http.*;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.impl.HttpIO;

import java.util.Map;
Expand Down Expand Up @@ -69,7 +68,7 @@ public void onDone(HttpResp result, Throwable error) {
resp.body(body);
resp.done();
} else {
HttpIO.errorAndDone(req, error, Customization.of(req).errorHandler());
HttpIO.errorAndDone(req, error);
}
}

Expand Down

0 comments on commit b496bba

Please sign in to comment.