fix(gateway-engine) Clearer handling of unexpected exceptions in ErrorWriter implementations.
MODIFY
gateway/engine/beans/src/main/java/io/apiman/gateway/engine/beans/EngineErrorResponse.java
gateway/engine/beans/src/main/java/io/apiman/gateway/engine/beans/EngineErrorResponse.java
@@ -21,12 +21,16 @@ import java.io.StringWriter;
import javax.xml.bind.annotation.XmlRootElement;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+
/**
* Models an error from the engine.
*
* @author Marc Savy <msavy@redhat.com>
*/
@XmlRootElement
+@JsonInclude(Include.NON_NULL)
public class EngineErrorResponse implements Serializable {
private static final long serialVersionUID = 8881390951647532958L;

MODIFY
gateway/engine/core/src/main/java/io/apiman/gateway/engine/impl/DefaultPolicyErrorWriter.java
gateway/engine/core/src/main/java/io/apiman/gateway/engine/impl/DefaultPolicyErrorWriter.java
@@ -62,7 +62,8 @@ public class DefaultPolicyErrorWriter implements IPolicyErrorWriter {
if (request != null && request.getApi() != null && "xml".equals(request.getApi().getEndpointContentType())) {
isXml = true;
}
- String message = (error.getMessage() == null) ? "" : error.getMessage(); // TODO get and/or print ultimate cause?
+ String message = createErrorMessage(request, error);
+ // TODO get and/or print ultimate cause?
response.setHeader("X-Gateway-Error", message);
int statusCode = 500;
@@ -71,8 +72,8 @@ public class DefaultPolicyErrorWriter implements IPolicyErrorWriter {
}
response.setStatusCode(statusCode);
-
- EngineErrorResponse eer = createErrorResponse(error, statusCode);
+ // #createErrorResponse can be overriden by subclasses (e.g. trace). So need to be careful.
+ EngineErrorResponse eer = createErrorResponse(error, message, statusCode);
if (isXml) {
response.setHeader("Content-Type", "application/xml");
try {
@@ -95,13 +96,20 @@ public class DefaultPolicyErrorWriter implements IPolicyErrorWriter {
}
}
+ protected String createErrorMessage(ApiRequest request, Throwable error) {
+ if (error.getMessage() == null) {
+ return error.getClass().getCanonicalName() + " error occurred with no message. Refer to logs.";
+ }
+ return error.getMessage();
+ }
+
/**
* @param error
*/
- protected EngineErrorResponse createErrorResponse(Throwable error, int statusCode) {
+ protected EngineErrorResponse createErrorResponse(Throwable error, String message, int statusCode) {
EngineErrorResponse eer = new EngineErrorResponse();
eer.setResponseCode(statusCode);
- eer.setMessage(error.getMessage());
+ eer.setMessage(message);
return eer;
}

MODIFY
gateway/engine/core/src/main/java/io/apiman/gateway/engine/impl/TracePolicyErrorWriter.java
gateway/engine/core/src/main/java/io/apiman/gateway/engine/impl/TracePolicyErrorWriter.java
package io.apiman.gateway.engine.impl;
+import io.apiman.gateway.engine.beans.ApiRequest;
import io.apiman.gateway.engine.beans.EngineErrorResponse;
/**
@@ -27,11 +28,18 @@ public class TracePolicyErrorWriter extends DefaultPolicyErrorWriter {
* @see io.apiman.gateway.engine.impl.DefaultPolicyErrorWriter#createErrorResponse(java.lang.Throwable)
*/
@Override
- protected EngineErrorResponse createErrorResponse(Throwable error, int statusCode) {
- EngineErrorResponse response = super.createErrorResponse(error, statusCode);
+ protected EngineErrorResponse createErrorResponse(Throwable error, String message, int statusCode) {
+ EngineErrorResponse response = super.createErrorResponse(error, message, statusCode);
response.setTrace(error);
- response.setResponseCode(statusCode);
return response;
}
+ @Override
+ protected String createErrorMessage(ApiRequest request, Throwable error) {
+ if (error.getMessage() == null) {
+ return error.getClass().getCanonicalName();
+ }
+ return error.getMessage();
+ }
+
}

fix(gateway-engine) Clearer handling of unexpected exceptions in ErrorWriter implementations.