Skip to content

Commit

Permalink
#788 refactored TkFallback
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk committed Dec 22, 2018
1 parent 7a4efc7 commit f691e0b
Showing 1 changed file with 34 additions and 49 deletions.
83 changes: 34 additions & 49 deletions src/main/java/org/takes/facets/fallback/TkFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,23 @@ private static Response route(final Take take, final Fallback fbk,
);
} catch (final HttpException ex) {
final Opt<Response> fbres = fbk.route(
new RqFallback.Fake(
req, ex.code(),
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
)
TkFallback.fallback(req, start, ex, ex.code())
);
if (!fbres.has()) {
throw new IOException(
String.format(
"There is no fallback available in %s",
fbk.getClass().getCanonicalName()
),
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
TkFallback.error(ex, req, start)
);
}
res = TkFallback.wrap(fbres.get(), fbk, req);
} catch (final Throwable ex) {
final Opt<Response> fbres = fbk.route(
new RqFallback.Fake(
req, HttpURLConnection.HTTP_INTERNAL_ERROR,
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
TkFallback.fallback(
req, start, ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
);
if (!fbres.has()) {
Expand All @@ -123,10 +111,7 @@ private static Response route(final Take take, final Fallback fbk,
ex.getClass().getCanonicalName(),
fbk.getClass().getCanonicalName()
),
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
TkFallback.error(ex, req, start)
);
}
res = TkFallback.wrap(
Expand All @@ -137,6 +122,23 @@ private static Response route(final Take take, final Fallback fbk,
return res;
}

/**
* Fallback request.
* @param req Request
* @param start Start time of request processing
* @param throwable Exception thrown
* @param code Error code
* @return Fallback request
* @throws IOException In case of error
* @checkstyle ParameterNumber (3 lines)
*/
private static RqFallback.Fake fallback(final Request req, final long start,
final Throwable throwable, final int code) throws IOException {
return new RqFallback.Fake(
req, code, TkFallback.error(throwable, req, start)
);
}

/**
* Wrap response.
* @param res Response to wrap
Expand All @@ -156,22 +158,13 @@ public Iterable<String> head() throws IOException {
head = res.head();
} catch (final HttpException ex) {
head = fbk.route(
new RqFallback.Fake(
req, ex.code(),
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
)
TkFallback.fallback(req, start, ex, ex.code())
).get().head();
} catch (final Throwable ex) {
head = fbk.route(
new RqFallback.Fake(
req, HttpURLConnection.HTTP_INTERNAL_ERROR,
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
TkFallback.fallback(
req, start, ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).get().head();
}
Expand All @@ -186,22 +179,13 @@ public InputStream body() throws IOException {
body = res.body();
} catch (final HttpException ex) {
body = fbk.route(
new RqFallback.Fake(
req, ex.code(),
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
)
TkFallback.fallback(req, start, ex, ex.code())
).get().body();
} catch (final Throwable ex) {
body = fbk.route(
new RqFallback.Fake(
req, HttpURLConnection.HTTP_INTERNAL_ERROR,
TkFallback.error(
ex, req,
System.currentTimeMillis() - start
)
TkFallback.fallback(
req, start, ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).get().body();
}
Expand All @@ -214,13 +198,14 @@ public InputStream body() throws IOException {
* Create an error.
* @param exp Exception original
* @param req Request we're processing
* @param msec Milliseconds it took
* @param start When started
* @return Error
* @throws IOException If fails
*/
private static Throwable error(final Throwable exp, final Request req,
final long msec) throws IOException {
final long start) throws IOException {
final String time;
final long msec = System.currentTimeMillis() - start;
if (msec < TimeUnit.SECONDS.toMillis(1L)) {
time = String.format("%dms", msec);
} else {
Expand Down

0 comments on commit f691e0b

Please sign in to comment.