Skip to content

Commit

Permalink
Async error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 11, 2016
1 parent b496bba commit bcbc6cc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
5 changes: 5 additions & 0 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/Req.java
Expand Up @@ -340,4 +340,9 @@ public interface Req {
*/
Customization custom();

/**
* Reverts the previous processing of the request, usually with intention to process the same request again.
*/
void revert();

}
Expand Up @@ -76,8 +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);
return HttpStatus.DONE;
return HttpIO.errorAndDone(req, e);
}

U.notNull(txMode, "transactionMode");
Expand All @@ -88,8 +87,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);
return HttpStatus.DONE;
return HttpIO.errorAndDone(req, e);
}

return HttpStatus.ASYNC;
Expand Down Expand Up @@ -212,7 +210,7 @@ public void run() {
}

private Object wrap(final Channel channel, final boolean isKeepAlive, final Req req, final int index, final Object extra)
throws Exception {
throws Exception {
HttpWrapper wrapper = wrappers[index];

HandlerInvocation invocation = new HandlerInvocation() {
Expand Down
Expand Up @@ -62,8 +62,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req, Object extra
return HttpStatus.NOT_FOUND;

} catch (Exception e) {
HttpIO.errorAndDone(req, e);
return HttpStatus.DONE;
return HttpIO.errorAndDone(req, e);
}
}

Expand Down
Expand Up @@ -8,11 +8,9 @@
import org.rapidoid.commons.MediaType;
import org.rapidoid.data.BufRange;
import org.rapidoid.data.JSON;
import org.rapidoid.http.HttpResponseCodes;
import org.rapidoid.http.HttpUtils;
import org.rapidoid.http.Req;
import org.rapidoid.http.Resp;
import org.rapidoid.http.*;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.job.Jobs;
import org.rapidoid.log.Log;
import org.rapidoid.net.abstracts.Channel;
import org.rapidoid.render.Template;
Expand Down Expand Up @@ -127,10 +125,20 @@ public static void error(final Req req, final Throwable error) {
}
}

public static void errorAndDone(Req req, Throwable error) {
error(req, error);
// the Req object will do the rendering
req.done();
public static HttpStatus errorAndDone(final Req req, final Throwable error) {
req.revert();
req.async();

Jobs.execute(new Runnable() {
@Override
public void run() {
error(req, error);
// the Req object will do the rendering
req.done();
}
});

return HttpStatus.ASYNC;
}

public static void writeContentLengthAndBody(Channel ctx, byte[] content) {
Expand Down
Expand Up @@ -582,6 +582,7 @@ public Customization custom() {
@Override
public Req async() {
this.async = true;
channel.async();
return this;
}

Expand Down Expand Up @@ -730,4 +731,15 @@ public boolean isStopped() {
return stopped;
}

@Override
public void revert() {
rendering = false;
posConLen = 0;
posBefore = 0;
async = false;
done = false;
completed = false;
response = null;
}

}

0 comments on commit bcbc6cc

Please sign in to comment.