English 中文(简体)
NodeJS API正等待着 go(选择)的错误,没有把Error To Frontend
原标题:NodeJS API await got(options) error Handling Not Sending Error To Frontend

我有一个从控制人员开始、从服务到完成申请的通道、回复控制人员处理和返回前线的节点。 我对处理错误没有什么麻烦。 如果<代码>got(选项)方法失败,我就会发现500个内部错误。 如果我附上<代码>got(options)的功能,例如:

const response = await got(options).catch(err=>{
console.log( error   +err.message);
console.log( error   +JSON.stringify(err.body));
});

错误标识,但我不能将错误发到前端,而是将500个错误用在APIC电话上。 我尝试了多种方法,但又是新的错误处理方法,因此无法工作。

My controller code is:

  router.delete( /deletePayment/:id , (req, res, next) => {
  squarePaymentService.deletePayment(req.params.id)
    .then(paymentResponse => res.send(paymentResponse))
    .catch(err => next(err));
  });

《服务守则》是:

 async function deletePayment(paymentId) {

  const options = await ApiOptionsService.deleteItem(paymentId)

  const response = await got(options).catch(err=>{
    console.log( error   +err.message); //need to catch and send this error to frontend.
  });
  return response.body;
}

I want to catch the error in console.log( error +err.message); and send it to the frontend.

最佳回答

如果你想要传播与你一样的错误,那么你就必须在发现错误后再犯错误。

async function deletePayment(paymentId) {

  const options = await ApiOptionsService.deleteItem(paymentId)

  const response = await got(options).catch(err=>{
    console.log( error   +err.message); 
    // re-throw the error so the caller will get it
    throw err;
  });
  return response.body;
}

但是,该守则只是记录一些错误,正在将<条码>await<>/条码>与<条码>混合起来。 如果你想在这项职能中出现任何错误,我就这样:

async function deletePayment(paymentId) {
    try {
        const options = await ApiOptionsService.deleteItem(paymentId)
        const response = await got(options);
        return response.body;
    } catch(err) {
        // log error
        console.log( error   + err.message);
        // rethrow so caller gets rejection
        throw err;
    }
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签