javascript - 如何传递错误的变量

标签 javascript node.js

嘿哟,

我有以下功能

async function fnIsOnScreenOnce(img, desc,iCounter,client,repeatDelay=0) {
  await timeout(repeatDelay);
  let screenshot= await client.screenshot()

  let buf = new Buffer(screenshot.value, 'base64');
  let img1 = cv.imdecode(buf)
  let result = img1.matchTemplate(img, 5).minMaxLoc(); 
  result.screenshot=img1;
  if (result.maxVal <= 0.65) {
      // Fail
      const msg = "Can't see object yet";
      throw new Error(result);
  }
        // All good
        console.log("result:"+result)
        logger.info("Found image on screen: "+desc);
        return result;
}

函数的调用

function fnIsOnScreen(img,client, repeats = 5, desc, wait = 2000,repeatDelay) {
    logger.info("Looking for image on screen:" +desc +" with " + repeats + " repeats ");
    let iCounter = 0;
    let init = ()=> timeout(wait).then((asd)=>{
      const attempt = () => fnIsOnScreenOnce(img, desc, iCounter,client,repeatDelay).then((data=>{
        let imagepath=fnMarkOnImage(data.screenshot,img,data,outputDir)
        let description={};
        description.action="Is image on screen ?";
        description.desc=desc;
        description.repeats=repeats;
        description.wait=wait;
        description.img=imagepath;
        description.message="is this correct element ? if is then it was found correctly";
        fnPushToOutputArray(description)
      return data;
      })).catch(err => {
              console.log(JSON.stringify(err));
              console.log(err);
              console.log(err.result);
              iCounter++;
              if (iCounter === repeats) {
                  // Failed, out of retries
                  logger.info("Object not found : " + desc);
                  return Promise.reject("Object not found : " + desc);
              }
              // Retry after waiting
              return attempt();
          });
          return attempt();      
    })
    return init();


}

结果对象包含一些日期。 错误结果包含 {} 对象,其中没有值。我需要获得所有的值。那么我如何通过 throw new error 传递结果对象以在 catch 中检索它?

最佳答案

返回带有错误的额外数据的一种方法是扩展 Error 类并自行添加它们

class MyError extends Error {

    constructor(message, errorExtraParams) {
        super(message);
        this._errorExtraParams = errorExtraParams;
    }

    get errorExtraParams() {

        return this._errorExtraParams;

    }

}

throw new MyError("Error!!!", {})
//or
let mError =  new MyError("Error!!!", {})
console.log(mError.errorExtraParams)

但我建议你不要使用 throw Error,因为我不喜欢因为无关紧要的原因而抛出 Error。我的意思是,在您的情况下,没有理由抛出错误,因为没有错误,也没有理由创建错误只是为了告诉您代码“嘿,我没有找到图像”,而只是返回 false。

async function fnIsOnScreenOnce(img, desc, iCounter, client, repeatDelay = 0) {
    await timeout(repeatDelay);
    let screenshot = await client.screenshot()

    let buf = new Buffer(screenshot.value, 'base64');
    let img1 = cv.imdecode(buf)
    let result = img1.matchTemplate(img, 5).minMaxLoc();
    result.screenshot = img1;
    if (result.maxVal <= 0.65) {
        const msg = "Can't see object yet";
        return false;
    }
    // All good
    console.log("result:" + result)
    logger.info("Found image on screen: " + desc);
    return result;
}

function fnIsOnScreen(img, client, repeats = 5, desc, wait = 2000, repeatDelay) {
    logger.info("Looking for image on screen:" + desc + " with " + repeats + " repeats ");
    let iCounter = 0;
    let init = () => timeout(wait).then((asd) => {

        let found = false;
        do {
            let found = await fnIsOnScreenOnce(img, desc, iCounter, client, repeatDelay)
        } while (found !== false && iCounter++ < 10)

        let imagepath = fnMarkOnImage(found.screenshot, img, found, outputDir)

        let description = {};
        description.action = "Is image on screen ?";
        description.desc = desc;
        description.repeats = repeats;
        description.wait = wait;
        description.img = imagepath;
        description.message = "is this correct element ? if is then it was found correctly";
        fnPushToOutputArray(description)

        return found;

    })
    return init();
}

关于javascript - 如何传递错误的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49236162/

相关文章:

node.js - 如何使用自定义消息设置 Joi 验证?

node.js - 在 MongoDB 和 Node.js 中获取字段的所有值的最有效方法

node.js - grunt uglify 在父目录中找不到我的文件

javascript - HTML5 视频 - 在其上方覆盖一个 div

html - 怎么把文件写成html漂亮?

javascript - 单击以打开 PDF - 您可能需要合适的加载程序来处理此文件类型

javascript - ng-if 不在 ng-repeat 中比较 $index

AngularJS $http.post() 返回 404

javascript - 带有 es2015 预设的 Babel 在 IE9 上中断

javascript - parseInt 可以处理百分比吗?