-
Notifications
You must be signed in to change notification settings - Fork 636
Status
Steve Hu edited this page Sep 24, 2016
·
5 revisions
In the scenario that error happens on the server, a Status object is designed to encapsulate standard http response 4xx and 5xx as well as application specific error code ERRXXXXX (prefixed with ERR with another 5 digits) and error message. Additionally, an description of the error will be available for more info about the error.
Here are the four fields in the Status object.
int statusCode;
String code;
String message;
String description;
status.json is a configuration file that contains all the status error defined for the application and it has the structure like this.
{
"ERR10000": {
"statusCode": 401,
"code": "ERR10000",
"message": "INVALID_AUTH_TOKEN",
"description": "Incorrect signature or malformed token in authorization header"
},
"ERR10001": {
"statusCode": 401,
"code": "ERR10001",
"message": "AUTH_TOKEN_EXPIRED",
"description": "Jwt token in authorization header expired"
},
.
.
.
}
To construct the object from this config
static final String STATUS_METHOD_NOT_ALLOWED = "ERR10008";
.
.
.
Status status = new Status(STATUS_METHOD_NOT_ALLOWED);
There are several way to serialize the object to JSON in response. And string concat is almost 10 times faster than Jackson ObjectMapper. For one million objects:
Jackson Perf 503
ToString Perf 65
Status status = new Status(STATUS_METHOD_NOT_ALLOWED);
exchange.setStatusCode(status.getStatusCode());
exchange.getResponseSender().send(status.toString());