Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created September 15, 2017 14:38
Show Gist options
  • Save swapnilshrikhande/adb2421d33aab92ff5343609f7a6f137 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/adb2421d33aab92ff5343609f7a6f137 to your computer and use it in GitHub Desktop.
Rest error handler
public class RESTErrorHandler {
public static final String RESPONSENULL = 'Response null';
public static final String UNKNOWN_ERROR = 'Unknown error';
public static final String SERVER_ERROR = 'Server side error';
public static final String CLIENT_ERROR = 'Client request error';
public static final String REDIRECTION = 'Redirection';
public static final String SUCCESS = 'Success';
public static final String INFO_RESPONSE = 'Informational response';
public class StatusDetails {
public Integer statusCode;
public String cause;
public String technicalReason;
public String actions;
//if this variable value is set to true, we can try to recover from the error by retrying the request again.
public Boolean isRecoverable;
public Boolean isSuccess;
public StatusDetails(Integer statusCodeValue){
this.statusCode = statusCodeValue;
}
public StatusDetails setCause(String value){
this.cause = value;
return this;
}
public StatusDetails setTechnicalReason(String value){
this.technicalReason = value;
return this;
}
public StatusDetails setActions(String value){
this.actions = value;
return this;
}
public StatusDetails setIsRecoverable(Boolean value){
this.isRecoverable = value;
return this;
}
public StatusDetails setIsSuccess(Boolean value){
this.isSuccess = value;
return this;
}
}
public static Map<Integer,StatusDetails> ERRORSTATUSDETAILS = new Map<Integer,StatusDetails>{
400 => new StatusDetails(400)
.setCause('Bad Request')
.setTechnicalReason(
'The specified URI is invalid.\n'
+'The request is not formatted correctly.\n'
+'The request is missing a required field.\n'
+'The number of requests received exceed the request limit.\n'
),
401 => new StatusDetails(401)
.setCause('Unauthorized')
.setTechnicalReason(
'The Authorization header is not included in the request.\n'
),
403 => new StatusDetails(403)
.setCause('Forbidden')
.setTechnicalReason(
'The Authorization header is included but it fails validation.\n'
+'The Oauth token has expired or been revoked. \n'
).setIsRecoverable(true),
404 => new StatusDetails(404)
.setCause('Not Found')
.setTechnicalReason(
'The Trip ID or Booking ID specified in the URI is invalid.\n'
),
409 => new StatusDetails(409)
.setCause('Conflict')
.setTechnicalReason(
'A job for the specified definition is already queued or running.\n'
),
500 => new StatusDetails(500)
.setCause('Internal Server Error')
.setTechnicalReason(
'The system is unable to serve the request currently.\n'
).setIsRecoverable(true),
503 => new StatusDetails(503)
.setCause('Service Unavailable')
.setTechnicalReason(
'This response is returned when the web service is down for maintenance.\n'
+'The partner application should sleep for 5 minutes then retry the request.\n'
+'If the request continues to fail after a few retries, the developer should contact [email protected].\n'
).setIsRecoverable(true),
302 => new StatusDetails(302)
.setCause('Redirect')
.setTechnicalReason(
' Server requesting redirect to another resource. \n'
)
};
public static String getStatusType(Integer statusValue){
Double statusTypeDouble = Math.floor(statusValue / 100);
Integer statusCodeType = statusTypeDouble !=null ? Integer.valueOf(statusTypeDouble) : 0;
if( statusCodeType == 1 ){
return 'Informational response';
} if( statusCodeType == 2 ){
return 'Success';
} else if( statusCodeType == 3 ) {
return 'Redirection';
} else if( statusCodeType == 4 ) {
return 'Client request error';
} else if( statusCodeType == 5 ) {
return 'Server side error';
} else {
return 'Unknown error';
}
}
public static StatusDetails getStatusDetails(HttpResponse httpResponse){
if( httpResponse == null){
return new StatusDetails(0)
.setCause('Response null')
.setIsSuccess(false);
}
return getStatusDetails(httpResponse.getStatusCode());
}
public static StatusDetails getStatusDetails(Integer statusValue){
StatusDetails StatusDetails = ERRORSTATUSDETAILS.get(statusValue);
if( StatusDetails == null ){
StatusDetails = new StatusDetails(statusValue)
.setCause(
getStatusType(statusValue)
);
if( 'Success'.equalsIgnoreCase( StatusDetails.cause ) ){
StatusDetails.isSuccess = true;
} else {
StatusDetails.isSuccess = false;
}
} else {
StatusDetails.isSuccess = false;
}
return StatusDetails;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment