Skip to content

Commit b1dc91b

Browse files
mfikesswannodette
authored andcommitted
CLJS-2654: Gracefully handle browser launch failure
1 parent e7dd8f0 commit b1dc91b

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/main/clojure/cljs/repl/browser.clj

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@
290290

291291
(def lock (Object.))
292292

293+
(defn- waiting-to-connect-message [url]
294+
(print-str "Waiting for browser to connect to" url "..."))
295+
296+
(defn- maybe-browse-url [base-url]
297+
(try
298+
(browse/browse-url (str base-url "?rel=" (System/currentTimeMillis)))
299+
(catch Throwable t
300+
(if-some [error-message (not-empty (.getMessage t))]
301+
(println "Failed to launch a browser:\n" error-message "\n")
302+
(println "Could not launch a browser.\n"))
303+
(println "You can instead launch a non-browser REPL (Node or Nashorn).\n")
304+
(println "You can disable automatic browser launch with this REPL option")
305+
(println " :launch-browser false")
306+
(println "and you can specify the listen IP address with this REPL option")
307+
(println " :host \"127.0.0.1\"\n")
308+
(println (waiting-to-connect-message base-url)))))
309+
293310
(defn setup [{:keys [working-dir launch-browser server-state] :as repl-env} {:keys [output-dir] :as opts}]
294311
(locking lock
295312
(when-not (:socket @server-state)
@@ -318,8 +335,8 @@
318335
(server/start repl-env)
319336
(let [base-url (str "http://" (:host repl-env) ":" (:port repl-env))]
320337
(if launch-browser
321-
(browse/browse-url (str base-url "?rel=" (System/currentTimeMillis)))
322-
(println "Waiting for browser to connect to" base-url "..."))))))
338+
(maybe-browse-url base-url)
339+
(println (waiting-to-connect-message base-url)))))))
323340
(swap! server-state update :listeners inc))
324341

325342
(defrecord BrowserEnv []

0 commit comments

Comments
 (0)