rinogo
4/13/2017 - 9:42 PM

An example of how to use the `.catch()` mechanism in NightmareJS

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