Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass variable in error

Heyo,

I have a following function

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;
}

Call of the function

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();


}

result object contains some date. On error result contains {} object with no values in it. I would need to get all the values. So how can i pass result object through throw new error to retrieve it in catch ?

like image 864
trixo Avatar asked May 12 '26 19:05

trixo


1 Answers

One way to return extra data with error is to extend Error class and add them your self

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)

But I suggest you don't use throw Error, because I don't like to throw Errors for insignificant reasons. What I mean is that in your case there is no reason to throw error cause there is no error and no reason to create an error just to tell you code "Hey I didnt find the image" instead just return 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();
}
like image 153
Stamos Avatar answered May 15 '26 07:05

Stamos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!