I am working on a library which will make HTTP call to my rest service basis on inputs passed to this library. And whatever response comes back from service whether it is successful response or failure coming from the service or something happened in this library like timeout or any other errors, I am returning a response object back to the customer which customer can look into and iterate the object accordingly.
My response object is like this as of now and this is what I am returning back to the customer and customer will use this object and iterate it to figure out whether it is successful response or some failure and use it accordingly.
public class DataResponse {
// response data
private final String response;
private final String date;
private final String confidence;
private final String mask;
// what are the errors?
private final ErrorCode error;
// whether it is successful response or not
private final StatusCode status;
public DataResponse(String response, String date, String confidence, String mask, ErrorCode error, StatusCode status) {
this.response = response;
this.date = date;
this.confidence = confidence;
this.mask = mask;
this.error = error;
this.status = status;
}
// getters here
}
And below is my ErrorCode
class which contains all the errors happened at client level or service level so that the customer can know what happened.
public enum ErrorCode {
OK(200, "NONE", "Response is success."),
NO_CONTENT(204, "No Content", "Response is success."),
CLIENT_TIMEOUT(1007, "Timeout", "Timeout has occured on the Client.");
// some more error messdates, keeping it short to show the idea
private final int code;
private final String status;
private final String description;
private ErrorCode(int code, String status, String description) {
this.code = code;
this.status = status;
this.description = description;
}
// getters here
@Override
public String toString() {
return code + " : " + status;
}
}
And below is StatusCode
class basis on which customer will decide whether it is successful response or not.
public enum StatusCode {
SUCCESS, ERROR;
}
Customers of this library - they will use this DataResponse
object in two ways:
- Firstly, they can serialize
DataResponse
object to JSON as per their needs if they want to do it but it's up to them. - Secondly they will use this
DataResponse
object as it is and just iterate it normally.
So wanted to make sure from both the perspectives if the current design is good? If not what can be the improvements.
Please review my code. If you have any questions, please ask. If you have any comments, please give them.