Skip to content

Commit

Permalink
removed try catch code, should be elsewhere as it should be outside o…
Browse files Browse the repository at this point in the history
…f our scope (moved to taskgroup tasks)
  • Loading branch information
balupton committed Aug 17, 2013
1 parent 0311498 commit 8eafa4f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 50 deletions.
16 changes: 4 additions & 12 deletions src/lib/ambi.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,14 @@ ambi = (method, args...) ->
# Asynchronous method
# Only call the completion callback if an error occurs as the fire method should fire the completion callback itself if all goes well
if isAsynchronousMethod
# Fire the method and catch errors
try
fireMethod.apply(null, args)
catch err
# an error was caught so fire the completion callback with the error
err = result
completionCallback(err)
# Fire the method
fireMethod.apply(null, args)

# Synchronous method
# Always call the completion callback ourselves as the fire method does not make use of it
else
# Fire the method and catch errors
try
result = fireMethod.apply(null, args)
catch err
result = err
# Fire the method and check for returned errors
result = fireMethod.apply(null, args)

# Check the result for a returned error
if typeChecker.isError(result)
Expand Down
108 changes: 70 additions & 38 deletions src/test/ambi-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ joe.describe 'ambi', (describe,it) ->
totalChecks = 2

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on a synchronous function
# by return
returnErrorSync = (x,y) ->
++executedChecks
return err
return new Error(errMessage)

# Test unsuccessful call
ambi returnErrorSync, 2, 5, (err,result) ->
expect(err, 'error to be set').to.eql(err)
expect(err.message, 'error to be set').to.eql(errMessage)
expect(result, 'result to be undefined').to.not.exist
++executedChecks

Expand All @@ -89,19 +89,19 @@ joe.describe 'ambi', (describe,it) ->
totalChecks = 2

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on an asynchronous function
# by callback
callbackErrorAsync = (x,y,next) ->
wait delay, ->
next(err)
next(new Error(errMessage))
++executedChecks
return 'async'

# Test unsuccessful call
ambi callbackErrorAsync, 2, 5, (err,result) ->
expect(err, 'error to be set').to.eql(err)
expect(err.message, 'error to be set').to.eql(errMessage)
expect(result, 'result to be undefined').to.not.exist
++executedChecks

Expand All @@ -116,7 +116,7 @@ joe.describe 'ambi', (describe,it) ->
totalChecks = 2

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on an asynchronous function
# by return
Expand All @@ -125,7 +125,7 @@ joe.describe 'ambi', (describe,it) ->
wait delay, ->
next(null, x*y)
++executedChecks
return err
return new Error(errMessage)

# Test successfull call
ambi returnErrorThenCompleteAsync, 2, 5, (err,result) ->
Expand All @@ -144,21 +144,21 @@ joe.describe 'ambi', (describe,it) ->
totalChecks = 2

# Define the error to be used
err = new Error('my first error')
err2 = new Error('my second error')
errMessage = 'my first error'
errMessage2 = 'my second error'

# Perform an error on an asynchronous function
# by return
# and never calling the callback
returnErrorThenCallbackErrorAsync = (x,y,next) ->
wait delay, ->
next(err2)
next(new Error(errMessage2))
++executedChecks
return err
return new Error(errMessage)

# Test unsuccessful error call
ambi returnErrorThenCallbackErrorAsync, 2, 5, (err,result) ->
expect(err, 'error to be set').to.eql(err2)
expect(err.message, 'error to be set').to.eql(errMessage2)
expect(result, 'result to be undefined').to.not.exist
++executedChecks

Expand All @@ -167,55 +167,81 @@ joe.describe 'ambi', (describe,it) ->
expect(executedChecks, 'special checks were as expected').to.eql(totalChecks)
done()

it 'should handle thrown errors on unsuccessful synchronous functions', (done) ->
it 'should NOT handle thrown errors on unsuccessful synchronous functions', (done) ->
# Define the amount of special checks
executedChecks = 0
totalChecks = 2
neverReached = false

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on a synchronous function
# by throw
throwErrorSync = (x,y) ->
throwErrorSyncUncaught = (x,y) ->
++executedChecks
throw err
throw new Error(errMessage)

# Test unsuccessful call
ambi throwErrorSync, 2, 5, (err,result) ->
expect(err, 'error to be set').to.eql(err)
expect(result, 'result to be undefined').to.not.exist
# Error callback
catchUncaughtException = (err) ->
expect(err.message, 'error to be set').to.eql(errMessage)
++executedChecks

# Test unsuccessful call
d = require('domain').create()
d.on('error', catchUncaughtException)
d.run ->
try
ambi throwErrorSyncUncaught, 2, 5, (err,result) ->
# should never reach here
++executedChecks
neverReached = true
catch err
catchUncaughtException(err)

# Check all the special checks passed
wait delay*2, ->
expect(executedChecks, 'special checks were as expected').to.eql(totalChecks)
expect(neverReached, 'never reached section should have never been reached').to.eql(false)
done()

it 'should handle thrown errors on unsuccessful asynchronous functions', (done) ->
it 'should NOT handle thrown errors on unsuccessful asynchronous functions', (done) ->
# Define the amount of special checks
executedChecks = 0
totalChecks = 2
neverReached = false

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on a synchronous function
# by throw
# and never calling the callback
throwErrorAsync = (x,y,next) ->
throwErrorAsyncUncaught = (x,y,next) ->
++executedChecks
throw err
throw new Error(errMessage)

# Test unsuccessful call
ambi throwErrorAsync, 2, 5, (err,result) ->
expect(err, 'error to be set').to.eql(err)
expect(result, 'result to be undefined').to.not.exist
# Error callback
catchUncaughtException = (err) ->
expect(err.message, 'error to be set').to.eql(errMessage)
++executedChecks

# Test unsuccessful call
d = require('domain').create()
d.on('error', catchUncaughtException)
d.run ->
try
ambi throwErrorAsyncUncaught, 2, 5, (err,result) ->
# should never reach here
++executedChecks
neverReached = true
catch err
catchUncaughtException(err)

# Check all the special checks passed
wait delay*2, ->
expect(executedChecks, 'special checks were as expected').to.eql(totalChecks)
expect(neverReached, 'never reached section should have never been reached').to.eql(false)
done()

it 'should NOT handle asynchronous thrown errors on unsuccessful asynchronous functions', (done) ->
Expand All @@ -225,27 +251,33 @@ joe.describe 'ambi', (describe,it) ->
neverReached = false

# Define the error to be used
err = new Error('my first error')
errMessage = 'my first error'

# Perform an error on an asynchronous function
# by throw inside asynchronous function
# and still calling the callback with the error
throwErrorAsyncUncaught = (x,y,next) ->
wait delay, ->
++executedChecks
throw err
throw new Error(errMessage)
return 'async'

# Error callback
catchUncaughtException = (err) ->
expect(err.message, 'error to be set').to.eql(errMessage)
++executedChecks

# Test unsuccessful call
d = require('domain').create()
d.on 'error', (err) ->
expect(err, 'error to be set').to.eql(err)
++executedChecks
d.on('error', catchUncaughtException)
d.run ->
ambi throwErrorAsyncUncaught, 2, 5, (err,result) ->
# should never reach here
++executedChecks
neverReached = true
try
ambi throwErrorAsyncUncaught, 2, 5, (err,result) ->
# should never reach here
++executedChecks
neverReached = true
catch err
catchUncaughtException(err)

# Check all the special checks passed
wait delay*2, ->
Expand Down

0 comments on commit 8eafa4f

Please sign in to comment.