我需要测试使用 .yaml 文件中的 openapi 生成器创建的客户端。到目前为止,对于每个端点,我正在创建另一条使用失眠进行测试的路线。
3条路线:
digitalAccountRouter.post('/accounts/child', digitalAccountController.create);
digitalAccountRouter.get(
'/accounts/main',
digitalAccountController.mainAccountInformation,
);
digitalAccountRouter.patch(
'/accounts/:financial_operation_key/owner',
digitalAccountController.update,
);
以及一条路线的实现示例:
public create = async (request: Request, response: Response,next: NextFunction,): Promise<Response | void> => {
const token = request.headers.authorization;
const payload = request.body;
payload.idempotencyKey = v4();
const digitalAccountClient = new DigitalAccountApi(
new Configuration({
baseOptions: {
headers: {
Authorization: token,
},
},
}),
);
const apiResponse = await digitalAccountClient
.accountsChildPost(payload)
.catch(next);
if (!apiResponse) {
throw new Error('No reponse');
}
return response.status(200).json(apiResponse.data);
};
快速错误处理:
app.use((err: any, _: Request, response: Response, next: NextFunction) => {
if (err) {
console.log(err);
return response.status(err.status || 500).json({ err });
}
return response.status(err.status || 500).json({ error: 'Internal error' });
});
我正在使用digitalAccountClient
发出accountsChildPost
端点请求并且它正在工作。但是我遇到了错误问题。如果对端点的请求发送错误我不知道如何通过。我想在端点发送时发送错误的响应。问题是快递没有发送完整的错误。如果我console.log(err)
显示终端上的所有对象,但在失眠中只有一部分错误到达。
部分错误:
我在失眠中收到的:
{
"err": {
"message": "Request failed with status code 401",
"name": "Error",
"stack": "Error: Request failed with status code 401\n at createError (/home/ruan/Documents/incentiveme/hubfintech/node_modules/axios/lib/core/createError.js:16:15)\n at settle (/home/ruan/Documents/incentiveme/hubfintech/node_modules/axios/lib/core/settle.js:17:12)\n at IncomingMessage.handleStreamEnd (/home/ruan/Documents/incentiveme/hubfintech/node_modules/axios/lib/adapters/http.js:260:11)\n at IncomingMessage.emit (events.js:322:22)\n at endReadableNT (_stream_readable.js:1187:12)\n at processTicksAndRejections (internal/process/task_queues.js:84:21)",
"config": {
"url": "https://qacst-ppi.hubprepaid.com.br/partner-interface/accounts/207389168/owner",
"method": "patch",
"data": "{\"companyName\":\"PPI\",\"contact\":{\"email\":\"valeinterno@vale.com.br\",\"homePhone\":\"1131859402\",\"businessPhone\":\"1131859402\",\"mobilePhone\":\"1131859402\"},\"address\":{\"street\":\"Rua dos Testes\",\"number\":\"123\",\"district\":\"Tamboré\",\"zipcode\":\"06460080\",\"city\":\"Barueri\",\"state\":\"SP\"}}",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"Authorization": "Bearer Bearer",
"User-Agent": "axios/0.21.1",
"Content-Length": 274
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
}
}
}
我错过了什么或做错了什么?