1

I am running a test using WebdriverIO and precisely on this line:

await browser.waitForVisible('#tx-sent li', 15000)

Every now and then, I get a Promise rejection error:

Error: Promise was rejected with the following reason: java.net.SocketException: Connection reset by peer (connect failed)

Is there a way to catch this promise rejection so that it doesn't cause the entire test to fail? In other words I want to catch this Promise rejection and resolve it.

ARR
  • 77
  • 1
  • 9
  • `try { ... } catch (ex) { ... }` – Andreas Feb 25 '19 at 09:21
  • [Correct Try…Catch Syntax Using Async/Await](https://stackoverflow.com/questions/44663864/correct-try-catch-syntax-using-async-await) – Andreas Feb 25 '19 at 09:24
  • @Andreas and if I want to include multiple 'await' lines of code in the try block, that's fine, right? – ARR Feb 25 '19 at 10:04
  • You can, but I wouldn't do it... One `try...catch...` per `await`, or the `await ...then(value => { ... }, error => { ... })` approach from the [link](https://stackoverflow.com/questions/44663864/correct-try-catch-syntax-using-async-await). – Andreas Feb 25 '19 at 10:10
  • Why wouldn't you do it? The thing is, this error randomly occurs in one of the await lines, and I think it makes more sense to group this all into a try catch block rather than using one for each await line. – ARR Feb 25 '19 at 10:21
  • Grouping all promises in one block will stop the execution of other promises once error occurs in any one of them – Ganesh Karewad Feb 25 '19 at 11:17

2 Answers2

1

You can use try/catch

try {
        await browser.waitForVisible('#tx-sent li', 15000);
} catch(e) {
        console.log(e);
}
mrblue
  • 237
  • 1
  • 12
0

You can use try and catch to handle errors in promises. do something like this

try {
   await browser.waitForVisible('#tx-sent li', 15000)
   } catch(error) {
  // thro or log erro as per you need
  //throw error;
   console.log(error);
 }
Ganesh Karewad
  • 1,158
  • 12
  • 21