How to process xml response when integration rest api in IBM BPM Automation Workflow
How to process xml response when integration rest api in IBM BPM Automation Workflow
1. Common functions for process xml response
function getErrorMessageInSystem(o) {
if (o && isNOTNullEmpty()) {
var value = o.error.xpath(o.path);
if (isNOTNullEmpty(value)
&& isNOTNullEmpty(value.item(0))) {
return value.item(0).getText();
}
}
return null;
};
function getRESTInfo(o) {
var info = {};
if (o && isNOTNullEmpty(o.error)) {
var pathRest = isNOTNullEmpty(o.path) ? o.path : '/error';
try {
var restResponse = o.error.xpath(pathRest + "/HTTPStatusCode");
if (isNOTNullEmptyCustom(restResponse)) {
info.errorCode = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/HTTPStatusCode"
});
info.errorMessage = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/HTTPStatusMessage"
});
info.errorContent = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/content"
});
} else {
info.errorCode = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/HTTPStatus"
});
info.errorContent = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/message"
});
}
} catch (ex) {
info.errorContent = getErrorMessageInSystem({
error : o.error,
path : pathRest + "/message"
});
info.errorMessage = 'Catching: UNKNOWN_ERROR';
}
}
return info;
};
function isNOTNullEmpty(value) {
if (UtilsClass.isArray(value)) {
return value != undefined && value != null && value.length > 0;
} else if (UtilsClass.isString(value)) {
return value != undefined && value != null && value.trim().length > 0;
}
return value != undefined && value != null;
};
2. How to use it
We have a service flow that integration with rest api
We will write code in "Catch Error" activity script as below:
var restInfo = getRESTInfo({
error: tw.system.error,
path: "/error/RESTResponseException"
});
if(isNOTNullEmpty(restInfo)){
if(!restInfo.errorCode){
restInfo = getRESTInfo({
error: tw.system.error
});
}
restInfo = restInfo ? restInfo : {};
}
object restInfo consists of values: errorCode, errorMessage and errorContent.
Nhận xét
Đăng nhận xét