Skip to content

Instantly share code, notes, and snippets.

@rinogo
Created April 13, 2017 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinogo/5958c4a60905a963673317656e055c6c to your computer and use it in GitHub Desktop.
Save rinogo/5958c4a60905a963673317656e055c6c to your computer and use it in GitHub Desktop.
An example of how to use the `.catch()` mechanism in NightmareJS
var Nightmare = require("nightmare");
var nightmare = new Nightmare();
nightmare
.goto("https://non-existent-website-nifgeoawniogea.com")
.catch(handleError);
//Note: You can manually raise an exception from within a `.then()` command with a normal `throw new Error()`:
//throw new Error("Description of the error.");
function handleError(error) {
nightmare.end().then();
var message;
if(typeof error.details != "undefined" && error.details != "") {
message = error.details;
} else if(typeof error == "string") {
message = error;
if(error == "Cannot read property 'focus' of null") {
message += " (Likely because a non-existent selector was used)";
}
} else {
message = error.message;
}
console.error({"status": "error", "message": message});
}
@rinogo
Copy link
Author

rinogo commented Apr 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment