Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #2337 update req/res transformer interceptor to handle the erro… #2338

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.nio.charset.StandardCharsets;
import java.util.*;

import static com.networknt.utility.Constants.ERROR_MESSAGE;

/**
* Transforms the request body of an active request being processed.
* This is executed by RequestInterceptorExecutionHandler. Also, this class will be responsible for
Expand All @@ -36,6 +38,7 @@
public class RequestTransformerInterceptor implements RequestInterceptor {
static final Logger logger = LoggerFactory.getLogger(RequestTransformerInterceptor.class);
static final String REQUEST_TRANSFORM = "request-transform";
static final String GENERIC_EXCEPTION = "ERR10014";

private final RequestTransformerConfig config;
private volatile HttpHandler next;
Expand Down Expand Up @@ -253,7 +256,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
case "validationError":
// If the rule engine returns any validationError entry, stop the chain and send the res.
// this can be either XML or JSON or TEXT. Just make sure it matches the content type
String errorMessage = (String)result.get("errorMessage");
String errorMessage = (String)result.get(ERROR_MESSAGE);
String contentType = (String)result.get("contentType");
int statusCode = (Integer)result.get("statusCode");
if(logger.isTraceEnabled()) logger.trace("Entry key validationError with errorMessage {} contentType {} statusCode {}");
Expand All @@ -263,6 +266,12 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
break;
}
}
} else {
// The plugin returns false, and it indicates an error has happened. We need to send this error
// message to the caller and stop the chain immediately by setting the exchange status.
String errorMessage = (String)result.get(ERROR_MESSAGE);
if(logger.isTraceEnabled()) logger.trace("Error message {} returns from the plugin", errorMessage);
setExchangeStatus(exchange, GENERIC_EXCEPTION, errorMessage);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.Map;

import static com.networknt.utility.Constants.ERROR_MESSAGE;

/**
* This is a generic middleware handler to manipulate response based on rule-engine rules so that it can be much more
* flexible than any other handlers like the header handler to manipulate the headers. The rules will be loaded from
Expand Down Expand Up @@ -54,6 +56,7 @@ public class ResponseTransformerInterceptor implements ResponseInterceptor {

private static final String STARTUP_HOOK_NOT_LOADED = "ERR11019";
private static final String RESPONSE_TRANSFORM = "response-transform";
static final String GENERIC_EXCEPTION = "ERR10014";

private final ResponseTransformerConfig config;
private volatile HttpHandler next;
Expand Down Expand Up @@ -191,6 +194,11 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
break;
}
}
} else {
// The finalResult is false to indicate there is an error in the plugin action. Set the exchange to stop the chain.
String errorMessage = (String)result.get(ERROR_MESSAGE);
if(logger.isTraceEnabled()) logger.trace("Error message {} returns from the plugin", errorMessage);
setExchangeStatus(exchange, GENERIC_EXCEPTION, errorMessage);
}
}
if (logger.isDebugEnabled()) logger.trace("ResponseTransformerInterceptor.handleRequest ends.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ResponseTransformerConfig {
private static final String APPLIED_PATH_PREFIXES = "appliedPathPrefixes";

private Map<String, Object> mappedConfig;
private Config config;
private final Config config;
private boolean enabled;
private boolean requiredContent;
private String defaultBodyEncoding;
Expand Down
3 changes: 3 additions & 0 deletions utility/src/main/java/com/networknt/utility/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,7 @@ public class Constants {
// framework
public static final String LIGHT_4J = "Light4j";
public static final String SPRING_BOOT = "SpringBoot";

// plugin error message
public static final String ERROR_MESSAGE = "errorMessage";
}