Skip to content

Commit 36014c5

Browse files
authored
fixes #2337 update req/res transformer interceptor to handle the error result from the plugins (#2338)
1 parent cd4b403 commit 36014c5

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

request-transformer/src/main/java/com/networknt/reqtrans/RequestTransformerInterceptor.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.nio.charset.StandardCharsets;
2626
import java.util.*;
2727

28+
import static com.networknt.utility.Constants.ERROR_MESSAGE;
29+
2830
/**
2931
* Transforms the request body of an active request being processed.
3032
* This is executed by RequestInterceptorExecutionHandler. Also, this class will be responsible for
@@ -36,6 +38,7 @@
3638
public class RequestTransformerInterceptor implements RequestInterceptor {
3739
static final Logger logger = LoggerFactory.getLogger(RequestTransformerInterceptor.class);
3840
static final String REQUEST_TRANSFORM = "request-transform";
41+
static final String GENERIC_EXCEPTION = "ERR10014";
3942

4043
private final RequestTransformerConfig config;
4144
private volatile HttpHandler next;
@@ -253,7 +256,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
253256
case "validationError":
254257
// If the rule engine returns any validationError entry, stop the chain and send the res.
255258
// this can be either XML or JSON or TEXT. Just make sure it matches the content type
256-
String errorMessage = (String)result.get("errorMessage");
259+
String errorMessage = (String)result.get(ERROR_MESSAGE);
257260
String contentType = (String)result.get("contentType");
258261
int statusCode = (Integer)result.get("statusCode");
259262
if(logger.isTraceEnabled()) logger.trace("Entry key validationError with errorMessage {} contentType {} statusCode {}");
@@ -263,6 +266,12 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
263266
break;
264267
}
265268
}
269+
} else {
270+
// The plugin returns false, and it indicates an error has happened. We need to send this error
271+
// message to the caller and stop the chain immediately by setting the exchange status.
272+
String errorMessage = (String)result.get(ERROR_MESSAGE);
273+
if(logger.isTraceEnabled()) logger.trace("Error message {} returns from the plugin", errorMessage);
274+
setExchangeStatus(exchange, GENERIC_EXCEPTION, errorMessage);
266275
}
267276
}
268277
}

response-transformer/src/main/java/com/networknt/restrans/ResponseTransformerInterceptor.java

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28+
import static com.networknt.utility.Constants.ERROR_MESSAGE;
29+
2830
/**
2931
* This is a generic middleware handler to manipulate response based on rule-engine rules so that it can be much more
3032
* flexible than any other handlers like the header handler to manipulate the headers. The rules will be loaded from
@@ -54,6 +56,7 @@ public class ResponseTransformerInterceptor implements ResponseInterceptor {
5456

5557
private static final String STARTUP_HOOK_NOT_LOADED = "ERR11019";
5658
private static final String RESPONSE_TRANSFORM = "response-transform";
59+
static final String GENERIC_EXCEPTION = "ERR10014";
5760

5861
private final ResponseTransformerConfig config;
5962
private volatile HttpHandler next;
@@ -191,6 +194,11 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
191194
break;
192195
}
193196
}
197+
} else {
198+
// The finalResult is false to indicate there is an error in the plugin action. Set the exchange to stop the chain.
199+
String errorMessage = (String)result.get(ERROR_MESSAGE);
200+
if(logger.isTraceEnabled()) logger.trace("Error message {} returns from the plugin", errorMessage);
201+
setExchangeStatus(exchange, GENERIC_EXCEPTION, errorMessage);
194202
}
195203
}
196204
if (logger.isDebugEnabled()) logger.trace("ResponseTransformerInterceptor.handleRequest ends.");

restrans-config/src/main/java/com/networknt/restrans/ResponseTransformerConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ResponseTransformerConfig {
2121
private static final String APPLIED_PATH_PREFIXES = "appliedPathPrefixes";
2222

2323
private Map<String, Object> mappedConfig;
24-
private Config config;
24+
private final Config config;
2525
private boolean enabled;
2626
private boolean requiredContent;
2727
private String defaultBodyEncoding;

utility/src/main/java/com/networknt/utility/Constants.java

+3
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,7 @@ public class Constants {
131131
// framework
132132
public static final String LIGHT_4J = "Light4j";
133133
public static final String SPRING_BOOT = "SpringBoot";
134+
135+
// plugin error message
136+
public static final String ERROR_MESSAGE = "errorMessage";
134137
}

0 commit comments

Comments
 (0)