Commits

Kenneth Jørgensen  committed 3f54420

Added ability for optional other argument to catch to be an error or other throwable material.

  • Participants
  • Parent commits ee02276

Comments (0)

Files changed (2)

File spec/core/EventMessageSpec.coffee

 				expect(reply.status).toBe "error"
 				expect(reply.error.split("\n")[0]).toBe "Error: Error :: Error: Error"
 
-		it "should accept an optional message an proxy error unless it has a reply handler", ->
+		it "should accept an optional message and proxy errors unless it has a reply handler", ->
 			# otherMsg has an error:
 			# msg.catch otherMsg, (reply) =>
 
 				expect(handler.callCount).toBe 0
 				reply = replier.getCall(0).args[0]
 				expect(reply).toBe errorMsg
+
+		it "should accept an optional error and proxy it unless it has a reply handler", ->
+			# otherMsg *is* an error:
+			# msg.catch otherMsg, (reply) =>
+			error = new Error "Error"
+			# No reply should throw the exception directly.
+			testNoReply = -> msgNoReply.catch error, handler
+			expect(testNoReply).toThrow "Error"
+			expect(handler.callCount).toBe 0
+			# With reply handler should pass the error along.
+			msg.catch error, handler
+			waitsFor (-> replier.called), "Replier never called", 100
+			runs ->
+				expect(replier.callCount).toBe 1
+				expect(handler.callCount).toBe 0
+				reply = replier.getCall(0).args[0]
+				expect(reply.status).toBe "error"
+				expect(reply.error.split("\n")[0]).toBe "Error: Error :: Error: Error"
+
+		it "should pass the call onyo handler if nothing is wrong", ->
+			catchFunc = msg.catch null, handler
+			catchFunc msg
+			waitsFor (-> handler.called), "Handler never called", 100
+			runs ->
+				expect(handler.callCount).toBe 1
+				receivedMsg = handler.getCall(0).args[0]
+				expect(receivedMsg).toBe msg

File src/core/EventMessage.coffee

 	# removing the need to constantly check incoming messages for errors.
 	# Example usage:
 	#
-	#     @send "address", msg.catch otherMsg, (reply) ->
+	#     @send "address", msg.catch other, (reply) ->
 	#
-	# In the above example, proxyErrors will check msg and otherMsg for errors, and pass them to the reply handler
+	# In the above example, proxyErrors will check msg and other for errors, and pass them to the reply handler
 	# for msg. If no errors are detected, it will execute the supplied handler inside a try/catch block, and pass
 	# any errors back through msg.
 	# If no reply handler exists on msg, errors will be rethrown, or no try/catch block will be used.
-	# otherMsg is optional.
-	catch: (otherMsg, handler) ->
-		unless otherMsg instanceof EventMessage and _.isFunction handler
-			if _.isFunction otherMsg
-				handler = otherMsg
-				otherMsg = undefined
-			else
-				throw new Error "Optional otherMsg must be an EventMessage and handler must be a function, msg:#{typeof otherMsg}, handler:#{typeof handler}"
+	# other is optional and can be an EventMessage or something throwable, or undefined.
+	catch: (other, handler) ->
+		unless handler?
+			unless _.isFunction other
+				throw new Error "Supplied handler is not a function, #{typeof other} supplied"
+			handler = other
+			other = undefined
 		# If we don't have a reply handler, just return a passthrough function.
 		unless _.isFunction @_replyHandler
 			# Throw error if we have one.
 			if @isError()
 				throw @error
-			if otherMsg? and otherMsg.isError()
-				throw otherMsg.error
+			if other?
+				if other instanceof EventMessage and other.isError()
+					throw other.error
+				else
+					throw other
 			# Create and return handler.
 			return (msg) ->
 				# Pass message errors.
 			if @isError()
 				@reply @
 				return
-			if otherMsg? and otherMsg.isError()
-				@reply otherMsg
+			if other?
+				if other instanceof EventMessage and other.isError()
+					@reply other
+				else
+					@replyError other
 			# Create and return handler.
 			return (msg) =>
 				# Pass message errors.