对salesforce进行POST api调用,无法向客户端返回正确的状态码,它返回正确的错误消息,但错误的状态码,应该是400,但返回200
这是调用handleCreateBusinessContactCase
函数的请求。
@Post('case/businessContact')
public async handleCreateBusinessContactCase(
@Body() requestBody: ContactRequest,
@Request() request: koa.Request,
@Header('X-Correlation-Id') correlationId?: string,
): Promise<any> {
const logCtx = LogContext.getLogContext(request.ctx, 'handleCreateBusinessContactCase');
const repsone = await createBusinessContactCase(logCtx, requestBody);
return Promise.resolve(repsone);
}
.....
return axios({
url,
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${tokens.authentication}`,
},
data: jsonData,
timeout: 10000
}).then((response: AxiosResponse<any>) => {
/*if (response.status === 401) {
log.debug(`SaleForce refused token, clear the token and try once more, status code: ${response.status}`);
}*/
return response.data
}).catch((error: AxiosError) => {
console.log(error.response?.data);
console.log(error.response?.status);
console.log(error.response?.headers);
return error.response?.data
});
从console.log
行输出是预期的,但在swagger UI我得到正确的消息,但状态码是200,应该是400!
日志输出
[
{
message: "Record Type ID: this ID value isn't valid for the user: 0120Y000000VLBKQA4",
errorCode: 'INVALID_CROSS_REFERENCE_KEY',
fields: [ 'RecordTypeId' ]
}
]
400
{
date: 'Sun, 10 Oct 2021 23:31:46 GMT',
'set-cookie': [
'CookieConsentPolicy=0:1; domain=icelandair--test.my.salesforce.com; path=/; expires=Mon, 10-Oct-2022 23:31:46 GMT; Max-Age=31536000',
'LSKey-c$CookieConsentPolicy=0:1; domain=my.salesforce.com; path=/; expires=Mon, 10-Oct-2022 23:31:46 GMT; Max-Age=31536000',
'BrowserId=PDDStioiEeyHw1UM8hQSng; domain=.salesforce.com; path=/; expires=Mon, 10-Oct-2022 23:31:46 GMT; Max-Age=31536000'
],
'strict-transport-security': 'max-age=31536000; includeSubDomains',
'x-content-type-options': 'nosniff',
'x-xss-protection': '1; mode=block',
'x-robots-tag': 'none',
'cache-control': 'no-cache,must-revalidate,max-age=0,no-store,private',
'sforce-limit-info': 'api-usage=17224/5000000',
'content-type': 'application/json;charset=UTF-8',
'transfer-encoding': 'chunked',
connection: 'close'
}
解决方案是首先修改控制器,使其不总是解析承诺
@Post('case/businessContact')
public async handleCreateBusinessContactCase(
@Body() requestBody: ContactRequest,
@Request() request: koa.Request,
@Header('X-Correlation-Id') correlationId?: string,
): Promise<any> {
const logCtx = LogContext.getLogContext(request.ctx, 'handleCreateBusinessContactCase');
return createBusinessContactCase(logCtx, requestBody);
}
然后我可以格式化返回的对象。像这样的处理程序
export const createBusinessContactCase() = async(): Promise<anty> {
return axios({
url,
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${tokens.authentication}`,
},
data: jsonData,
timeout: 10000
}).then((response: AxiosResponse<any>) => {
/*if (response.status === 401) {
log.debug(`SaleForce refused token, clear the token and try once more, status code: ${response.status}`);
}*/
return response.data
}).catch((error: AxiosError) => {
throw { message: error.response?.data, status: error.response?.status };
});
};
}