Skip to content

Commit ee1ad4e

Browse files
author
Alex Turpin
committed
Log error when opening of browser fails
1 parent 80120dc commit ee1ad4e

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

lib/internal-events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function (bs) {
4242
var open = bs.options.get("open");
4343

4444
if (mode === "proxy" || mode === "server" || open === "ui" || open === "ui-external") {
45-
utils.openBrowser(data.url, bs.options);
45+
utils.openBrowser(data.url, bs.options, bs);
4646
}
4747

4848
// log about any file watching

lib/logger.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ module.exports.callbacks = {
6767
logger.info("{cyan:Reloading Browsers...");
6868
}
6969
},
70+
/**
71+
*
72+
*/
73+
"browser:error": function () {
74+
logger.error("Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the {cyan:open} option to {cyan:false})");
75+
},
7076
/**
7177
* @param {BrowserSync} bs
7278
* @param data

lib/utils.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ var utils = {
140140
* @param {String} url
141141
* @param {Object} options
142142
*/
143-
openBrowser: function (url, options) {
143+
openBrowser: function (url, options, bs) {
144144

145145
var open = options.get("open");
146146
var browser = options.get("browser");
@@ -155,13 +155,13 @@ var utils = {
155155
if (browser !== "default") {
156156
if (utils.isList(browser)) {
157157
browser.forEach(function (browser) {
158-
utils.open(url, browser);
158+
utils.open(url, browser, bs);
159159
});
160160
} else {
161-
utils.open(url, browser); // single
161+
utils.open(url, browser, bs); // single
162162
}
163163
} else {
164-
utils.open(url);
164+
utils.open(url, null, bs);
165165
}
166166
}
167167
},
@@ -170,7 +170,7 @@ var utils = {
170170
* @param url
171171
* @param name
172172
*/
173-
open: function (url, name) {
173+
open: function (url, name, bs) {
174174
var options = (function () {
175175
if (_.isString(name)) {
176176
return {app: name};
@@ -180,7 +180,10 @@ var utils = {
180180
}
181181
return {};
182182
})();
183-
require("opn")(url, options);
183+
var opn = require("opn");
184+
opn(url, options).catch(function() {
185+
bs.events.emit("browser:error");
186+
});
184187
},
185188
/**
186189
* @param {Boolean} kill

0 commit comments

Comments
 (0)