Skip to content

Commit

Permalink
Replace ISE with VertxException when failing context with information…
Browse files Browse the repository at this point in the history
…al message

Related to vert-x3#2486
Follows-up on vert-x3#1857

ISE creates a stack trace which isn't really useful. In these cases, we only care about the message.

This change makes the application log a single line (or more if the message is long). Besides, it saves the cost of creating the ISE stack trace.

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Oct 25, 2023
1 parent b0fb5b2 commit 73d6dae
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.netty.handler.codec.http.HttpHeaderValues;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.HttpHeaders;
Expand Down Expand Up @@ -132,7 +133,7 @@ public void handle(RoutingContext context) {
.resume();
} else {
String failure = "BodyHandler invoked after the request has ended. It should be the first handler invoked. Otherwise, you must pause the request after it's received.";
context.fail(new IllegalStateException(failure));
context.fail(new VertxException(failure, true));
}
} else {
// on reroute we need to re-merge the form params if that was desired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.CookieSameSite;
import io.vertx.core.http.HttpMethod;
Expand Down Expand Up @@ -196,7 +197,7 @@ private boolean isValidRequest(RoutingContext ctx) {
if (ctx.body().available()) {
header = ctx.request().getFormAttribute(headerName);
} else {
ctx.fail(new IllegalStateException("BodyHandler is required to process POST requests"));
ctx.fail(new VertxException("BodyHandler is required to process POST requests", true));
return false;
}
}
Expand Down Expand Up @@ -304,7 +305,7 @@ public void handle(RoutingContext ctx) {
// if we're being strict with the origin
// ensure that they are always valid
if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.vertx.ext.web.handler.impl;

import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand Down Expand Up @@ -250,7 +251,7 @@ public void handle(RoutingContext context) {
.response()
.setStatusMessage("CORS Rejected - Invalid origin");
context
.fail(403, new IllegalStateException("CORS Rejected - Invalid origin"));
.fail(403, new VertxException("CORS Rejected - Invalid origin", true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.vertx.ext.web.handler.impl;

import io.vertx.core.Future;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void mountRegister() {
.handler(ctx -> {
final User user = ctx.user().get();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand All @@ -170,7 +171,7 @@ private void mountVerify() {
.handler(ctx -> {
final User user = ctx.user().get();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.vertx.ext.web.handler.impl;

import io.vertx.core.Future;
import io.vertx.core.VertxException;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.audit.Marker;
Expand Down Expand Up @@ -119,7 +120,7 @@ public void postAuthentication(RoutingContext ctx) {
final User user = ctx.user().get();
if (user == null) {
// bad state
ctx.fail(403, new IllegalStateException("no user in the context"));
ctx.fail(403, new VertxException("no user in the context", true));
return;
}
// the user is authenticated, however the user may not have all the required scopes
Expand All @@ -128,12 +129,12 @@ public void postAuthentication(RoutingContext ctx) {
if (scopes.size() > 0) {
final JsonObject jwt = user.get("accessToken");
if (jwt == null) {
ctx.fail(403, new IllegalStateException("Invalid JWT: null"));
ctx.fail(403, new VertxException("Invalid JWT: null", true));
return;
}

if (jwt.getValue("scope") == null) {
ctx.fail(403, new IllegalStateException("Invalid JWT: scope claim is required"));
ctx.fail(403, new VertxException("Invalid JWT: scope claim is required", true));
return;
}

Expand All @@ -150,7 +151,7 @@ public void postAuthentication(RoutingContext ctx) {
if (target != null) {
for (String scope : scopes) {
if (!target.contains(scope)) {
ctx.fail(403, new IllegalStateException("JWT scopes != handler scopes"));
ctx.fail(403, new VertxException("JWT scopes != handler scopes", true));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.impl.logging.Logger;
Expand Down Expand Up @@ -342,7 +343,7 @@ public void postAuthentication(RoutingContext ctx) {
final User user = ctx.user().get();
if (user == null) {
// bad state
ctx.fail(403, new IllegalStateException("no user in the context"));
ctx.fail(403, new VertxException("no user in the context", true));
return;
}

Expand All @@ -368,12 +369,12 @@ public void postAuthentication(RoutingContext ctx) {
(idx != 0 && userScopes.charAt(idx -1) != ' ') ||
(idx + scope.length() != userScopes.length() && userScopes.charAt(idx + scope.length()) != ' ')) {
// invalid scope assignment
ctx.fail(403, new IllegalStateException("principal scope != handler scopes"));
ctx.fail(403, new VertxException("principal scope != handler scopes", true));
return;
}
} else {
// invalid scope assignment
ctx.fail(403, new IllegalStateException("principal scope != handler scopes"));
ctx.fail(403, new VertxException("principal scope != handler scopes", true));
return;
}
}
Expand Down Expand Up @@ -440,9 +441,9 @@ private void mountCallback() {

String errorDescription = ctx.request().getParam("error_description");
if (errorDescription != null) {
ctx.fail(errorCode, new IllegalStateException(error + ": " + errorDescription));
ctx.fail(errorCode, new VertxException(error + ": " + errorDescription, true));
} else {
ctx.fail(errorCode, new IllegalStateException(error));
ctx.fail(errorCode, new VertxException(error, true));
}
return;
}
Expand All @@ -452,7 +453,7 @@ private void mountCallback() {

// code is a require value
if (code == null) {
ctx.fail(400, new IllegalStateException("Missing code parameter"));
ctx.fail(400, new VertxException("Missing code parameter", true));
return;
}

Expand All @@ -468,7 +469,7 @@ private void mountCallback() {

// state is a required field
if (state == null) {
ctx.fail(400, new IllegalStateException("Missing IdP state parameter to the callback endpoint"));
ctx.fail(400, new VertxException("Missing IdP state parameter to the callback endpoint", true));
return;
}

Expand All @@ -482,7 +483,7 @@ private void mountCallback() {
// if there's a state in the context they must match
if (!state.equals(ctxState)) {
// forbidden, the state is not valid (this is a replay attack)
ctx.fail(401, new IllegalStateException("Invalid oauth2 state"));
ctx.fail(401, new VertxException("Invalid oauth2 state", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.vertx.ext.web.handler.impl;

import io.vertx.core.Future;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -144,7 +145,7 @@ private void mountRegister() {
.handler(ctx -> {
final User user = ctx.user().get();
if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}
final OtpKey key = otpKeyGen.generate();
Expand All @@ -169,7 +170,7 @@ private void mountVerify() {
final User user = ctx.user().get();

if (user == null || user.get("username") == null) {
ctx.fail(new IllegalStateException("User object misses 'username' attribute"));
ctx.fail(new VertxException("User object misses 'username' attribute", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.vertx.ext.web.handler.impl;

import io.vertx.core.Future;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand All @@ -31,8 +32,8 @@
import io.vertx.ext.web.handler.WebAuthnHandler;
import io.vertx.ext.web.impl.OrderListener;
import io.vertx.ext.web.impl.Origin;
import io.vertx.ext.web.impl.UserContextInternal;
import io.vertx.ext.web.impl.RoutingContextInternal;
import io.vertx.ext.web.impl.UserContextInternal;

public class WebAuthnHandlerImpl extends AuthenticationHandlerImpl<WebAuthn> implements WebAuthnHandler, OrderListener {

Expand Down Expand Up @@ -204,7 +205,7 @@ private void mountRegister() {
// input basic validation is OK

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down Expand Up @@ -243,7 +244,7 @@ private void mountLogin() {
// input basic validation is OK

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down Expand Up @@ -293,7 +294,7 @@ private void mountResponse() {
final Session session = ctx.session();

if (session == null) {
ctx.fail(500, new IllegalStateException("No session or session handler is missing."));
ctx.fail(500, new VertxException("No session or session handler is missing.", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import io.vertx.ext.web.handler.sockjs.SockJSSocket;
import io.vertx.ext.web.impl.Origin;

import static io.vertx.core.http.HttpHeaders.*;
import static io.vertx.core.http.HttpHeaders.ALLOW;
import static io.vertx.ext.web.impl.Utils.canUpgradeToWebsocket;

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ private void handleGet(RoutingContext ctx) {
}

if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

package io.vertx.ext.web.handler.sockjs.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.impl.logging.Logger;
Expand All @@ -50,7 +50,7 @@
import io.vertx.ext.web.handler.sockjs.SockJSSocket;
import io.vertx.ext.web.impl.Origin;

import static io.vertx.core.http.HttpHeaders.*;
import static io.vertx.core.http.HttpHeaders.ALLOW;
import static io.vertx.ext.web.impl.Utils.canUpgradeToWebsocket;

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ private void handleGet(RoutingContext ctx) {
}

if (!Origin.check(origin, ctx)) {
ctx.fail(403, new IllegalStateException("Invalid Origin"));
ctx.fail(403, new VertxException("Invalid Origin", true));
return;
}

Expand Down

0 comments on commit 73d6dae

Please sign in to comment.