From f2ab14656be02cdb81c0e95c8d092acf808f4505 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 10 Dec 2014 18:08:43 -0800 Subject: [PATCH] BufferedProcess::onDidThrowError -> ::onDidThrowError --- spec/buffered-process-spec.coffee | 5 +++-- src/buffered-process.coffee | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index a9b7d5629..93b1357c1 100644 --- a/spec/buffered-process-spec.coffee +++ b/spec/buffered-process-spec.coffee @@ -17,14 +17,15 @@ describe "BufferedProcess", -> args: ['nothing'] options: {} - process.onDidThrowError errorSpy = jasmine.createSpy() + errorSpy = jasmine.createSpy().andCallFake (error) -> error.handle() + process.onWillThrowError(errorSpy) waitsFor -> errorSpy.callCount > 0 runs -> expect(window.onerror).not.toHaveBeenCalled() expect(errorSpy).toHaveBeenCalled() - expect(errorSpy.mostRecentCall.args[0].message).toContain 'spawn bad-command-nope ENOENT' + expect(errorSpy.mostRecentCall.args[0].error.message).toContain 'spawn bad-command-nope ENOENT' describe "when there is not an error handler specified", -> it "calls the error handler and does not throw an exception", -> diff --git a/src/buffered-process.coffee b/src/buffered-process.coffee index 5a28db9ed..874f42d2f 100644 --- a/src/buffered-process.coffee +++ b/src/buffered-process.coffee @@ -101,24 +101,35 @@ class BufferedProcess triggerExitCallback() @process.on 'error', (error) => - handlers = @emitter.handlersByEventName['did-throw-error'] - if handlers? and handlers.length - @emitter.emit 'did-throw-error', error - else - e = new Error("Failed to spawn command `#{command}`. Make sure `#{command}` is installed and on your PATH", error.path) - e.name = 'BufferedProcessError' - throw e + handled = false + handle = -> handled = true + + @emitter.emit 'will-throw-error', {error, handle} + + if error.code is 'ENOENT' and error.syscall.indexOf('spawn') is 0 + error = new Error("Failed to spawn command `#{command}`. Make sure `#{command}` is installed and on your PATH", error.path) + error.name = 'BufferedProcessError' + + throw error unless handled ### Section: Event Subscription ### - # Public: Will call your callback when an error is raised by the process. + # Public: Will call your callback when an error will be raised by the process. # Usually this is due to the command not being available or not on the PATH. + # You can call `handle()` on the object passed to your callback to indicate + # that you have handled this error. + # + # * `callback` {Function} callback + # * `errorObject` {Object} + # * `error` {Object} the error object + # * `handle` {Function} call this to indicate you have handled the error. + # The error will not be thrown if this function is called. # # Returns a {Disposable} - onDidThrowError: (callback) -> - @emitter.on 'did-throw-error', callback + onWillThrowError: (callback) -> + @emitter.on 'will-throw-error', callback ### Section: Helper Methods