我在ibm-app-conect中开发,遇到了一个问题。
我使用后端soap服务并将其转换为JSON。
当我测试我的服务(OK)时,我没有问题,但是当我用错误案例测试我的服务时,我得到一个通用的错误消息。
在该图像中,我收到了通用错误消息。
如何控制此错误,以便得到图像中显示的以下消息?
下面是我在这个gatewayscript或程序中使用的代码。
var apim=require('apim');
// IMPORT
const transform = require('transform');
const converter = require('json-xml-converter');
// CONSTANTES
const HTTP_CODE_FUNCTIONAL = 209;
const HTTP_CODE_OK = 200;
const HTTP_CODE_500 = 500;
const HTTP_MESSAGE_500 = 'Internal Server Error';
const codeErrors = [500, 403, 503, 404];
// VARIABLES
let bodyProvider = apim.getvariable('message.body');
let httpCode = apim.getvariable('message.status.code');
let backSideTransport = apim.getvariable('message.headers.X-Backside-Transport');
console.log("probando:" + backSideTransport);
// SERVICE VARIABLES
let tagBackend = '//xmlns:GetClienteObservadosOFACR1SelResponse';
let tagNamespace = 'http://tempuri.org/';
let dataType = 'xml';
let nsVar = 'xmlns';
// INIT VALIDATE PROCCESS
manageResponse(httpCode,bodyProvider,dataType,tagBackend,nsVar,tagNamespace);
/*---------------------------- Operation section ----------------------------*/
// Especific function: Validate items ok
function validateEspecific(sJson) {
let okSrvCods = ['OK'];
let flagMapInGateway = true;
let texto = '';
if (typeof sJson.GetClienteObservadosOFACR1SelResponse.GetClienteObservadosOFACR1SelResult.NewDataSet.Datos == undefined
|| sJson.GetClienteObservadosOFACR1SelResponse.GetClienteObservadosOFACR1SelResult.NewDataSet.Datos == null
|| sJson.GetClienteObservadosOFACR1SelResponse.GetClienteObservadosOFACR1SelResult.NewDataSet.Datos == '') {
texto += '{ "blackListFlag": "false"}';
}else{
texto += '{ "blackListFlag": "true"}';
}
generateResponseMessage(200,'OK','0','EJECUCION CON EXITO','0000', 'OK',texto);
}
// Especific function: Create response 200
function mapJsonOK(codError, msgError, respuesta) {
let rsMessage = '{ "sequenceId": "' + respuesta.trim() + '"}';
generateResponseMessage(200,'OK','0','EJECUCION CON EXITO',codError, msgError,rsMessage);
}
/*---------------------------- Operation section ----------------------------*/
/*------------------------------ Free section -------------------------------*/
function defaultVal(field, def) {
field = typeof field !== 'undefined' && field != null && field != '' ? field : def;
return field;
}
/*------------------------------ Free section -------------------------------*/
/*-------------------------- Framework IFX section --------------------------*/
//Generic function: Manage response
function manageResponse(httpCode,bodyProvider,dataType,tagBackend,nsVar,tagNamespace){
if (codeErrors.includes(httpCode)){
// Separar segun codigo
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}else if(200 == httpCode){
if(bodyProvider){
validateAndTransformBody(dataType, bodyProvider, tagBackend, nsVar, tagNamespace);
}else{
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}
}else{
// Codigo HTTP no conocido, genera respuesta generica
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}
}
// Generic function: Validate dataType and transform body
function validateAndTransformBody(dataType,bodyProvider, tagBackend, nsVar, tagNamespace) {
if(dataType == 'xml' ){
let options = { 'expression': '//tns1:GetClienteObservadosOFACR1SelResponse', 'xmldom': bodyProvider, 'namespace': { 'tns1' : 'http://tempuri.org/'}};
let option = { omitXmlDeclaration: true };
transform.xpath(options, function(err, xmlNodeList) {
if (err) { session.output.write(err);
}else{
let output = XML.stringify(option, xmlNodeList);
let xml = XML.parse(output);
let sJson = converter.toJSON('badgerfish',xml);
validateEspecific(sJson);
}
});
}else{ validateEspecific(bodyProvider); }
}
// Generic function: Propagate to Map Policy
function skipToMap(flag) {
apim.setvariable('message.headers.skip', true);
}
// Generic function: Responde cabecera de error IFX
function generateResponseMessage(codeHttp,reasonHttp,busResponseCode,busResponseMessage,srvResponseCode,srvResponseMessage,responseMessage){
let globalTransaccionId = apim.getvariable('message.headers.X-Global-Transaction-ID');
apim.setvariable('message.headers','','clear');
apim.setvariable('message.body','','clear');
apim.setvariable('message.headers.busResponseCode', busResponseCode);
apim.setvariable('message.headers.busResponseMessage', busResponseMessage);
apim.setvariable('message.headers.srvResponseCode', srvResponseCode);
apim.setvariable('message.headers.srvResponseMessage', srvResponseMessage);
apim.setvariable('message.headers.X-Global-Transaction-ID', globalTransaccionId);
if(responseMessage != null){
apim.setvariable('message.body', responseMessage);
}
apim.setvariable('message.status.code', codeHttp);
apim.setvariable('message.status.reason', reasonHttp);
apim.output('application/json');
}
/*-------------------------- Framework IFX section --------------------------*/
function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; }
我尝试使用头X-Backside-Transport
,但我无法修复它。
if (codeErrors.includes(httpCode)){
let backSideTransport = apim.getvariable('message.headers.X-Backside-Transport');
// Separar segun codigo
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}else if(200 == httpCode && backSideTransport == 'FAIL FAIL'){
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '502','An error occurred trying to invoke external services.');
apim.setvariable('message.headers.srvResponseCode', HTTP_CODE_500);
apim.setvariable('message.headers.srvResponseMessage', HTTP_MESSAGE_HTTP_500);
}else{
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}
}else{
// Codigo HTTP no conocido, genera respuesta generica
generateResponseMessage(HTTP_CODE_500, HTTP_MESSAGE_500, '302','An error occurred trying to invoke external services.');
}
}
1条答案
按热度按时间iezvtpos1#
如果HTTP代码为200,要控制JavaScript中的SOAP错误消息,可以使用XMLHttpRequest(XHR)对象发出HTTP请求并处理响应。下面是一个示例代码片段:
在上面的代码中,我们创建了XHR对象的一个新示例,将其onreadystatechange属性设置为一个函数,该函数将在请求完成且响应就绪时处理响应。在onreadystatechange函数中,我们检查XHR对象的readyState属性是否等于4(意味着请求已完成,响应已就绪),以及status属性是否等于200(意味着HTTP请求已成功)。
如果HTTP请求成功,我们可以使用XHR对象的responseXML属性处理SOAP响应。如果HTTP请求不成功,我们可以通过使用控制台将错误消息记录到控制台来处理错误。error().