Can I extend App Engine Standard error JSON in Java?

57 views Asked by At

App Engine manages what's returned as error JSON on Cloud Endpoints. When I throw ServiceException which Google supplies, I pass it statusCode and statusMessage:

public ServiceException(int statusCode, String statusMessage) {
    super(statusMessage);

    this.statusCode = statusCode;
    this.reason = null;
    this.domain = null;
}

App Engine then returns this JSON to the caller in the error response:

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "required",
                "message": "The statusMessage I gave it."
            }
        ],
        "code": 401,
        "message": "The statusMessage I gave it."
    }
}

I would like to extend the response with an extra field, e.g. for an application error code which tells the client what went wrong (message should be human readable).

I'm not so deep into Java servlets and internal workings of this, but I have tried to catch the response and modify it, which worked for a 200 response, but not for an error response. Any ideas?

1

There are 1 answers

3
Rodrigo C. On

Based on what you are requesting, I would suggest you take a look at how the ServiceException class works on the Java endpoints-framework.

Here you can check how the exceptions are being handled and, based on this, extend the error JSON that is returned to suit your needs, that is, that anyone can understand why his run failed.