Framework thrown out or some third-party components thrown exception. We simply do not know its corresponding error code information, so we have no way to return a specific error code and error messages to the user.
We first define Map There are some unpredictable anomaly, defined error codes and error messages. If we find the error code in the map, we can return to the user, if we did not find. Unified abnormal returns 99999
This error code abnormal our custom. For example returns the operation fails, contact the administrator of this.
For we are so unpredictable exceptions to deal with.
First simulate what unpredictable exception. Method to add, we json body which does not pass data back-end method is called
at this time to return the 400 error message, this is not our unified response format information.
This is our unified response information
or the type of abnormal returns 99999. Either put these anomalies to define a specific code
first project in which we want to capture this kind of anomalies. Based on error handling inside, to capture Exception.class here @ExceptionHandler this type of exception.
But here we get less than an abnormal code
here to add a breakpoint, type what we look unusual
break in. Copy the exception information out of this capture.
Abnormal body means that the requested data does not
fight for such abnormal return our unity 99999. This enumeration is CommCode inside Server_error
example, here we return 99999
Such eventual reunification of the returned data as follows
Defined Map
Some unpredictable anomaly is defined error code
we can define the error code HttpMessageNotReadableException this type.
Illegal parameters defined in this enumeration CommonCode
ImmutableMap is type in Google toolkit, the map data once into them can not be changed. That is, read-only
previous representation type of exception, the exception will always inherit Throwable. Exception code followed by
re-defined objects builder, builder object is used to build the ImmutableMap. Note modifier is protected
then how to build it? Placed inside a static block. Now the data is still there this builder, once the builder to construct the data Map inside. The map data will not be changed.
Use builder.build method for constructing Map
test
Adding a breakpoint here
null pointer exception
herein should build EXCEPTIONS constructed to receive map object.
Tested again
to find the error code and return.
This request into a request to go get data. Returned error code is 99999 because of this error is not added to our error Map collection.
Over the unpredictable exception handling it done.