Skip to content

Commit

Permalink
Merge pull request peerlibrary#41 from zodern/master
Browse files Browse the repository at this point in the history
Handle errors inside autoruns
  • Loading branch information
mitar authored Sep 20, 2019
2 parents ad3ec84 + faee4ae commit 3799cdf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ extendPublish (name, publishFunction, options) ->
# and that you can return cursors from the function which would be automatically published.
publish.autorun = (runFunc) ->
handle = Tracker.autorun (computation) ->
result = runFunc.call publish, computation
try
result = runFunc.call publish, computation
catch e
if computation.firstRun
throw e
else
publish.error(e)

collectionNames = getCollectionNames result
allCollectionNames[computation._id] = collectionNames
Expand Down
37 changes: 37 additions & 0 deletions tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,40 @@ for idGeneration in ['STRING', 'MONGO']

# Register the test case.
ClassyTestCase.addTest new ReactivePublishTestCase()

if Meteor.isServer
Meteor.publish "initial error", () ->
@autorun () =>
throw new Meteor.Error('triggered error')

Meteor.publish "rereun error", () ->
reactiveError = new ReactiveVar false

@autorun () =>
if reactiveError.get()
throw new Meteor.Error('triggered error')

setTimeout (-> reactiveError.set(true)), 1000
@ready()

class ReactivePublishErrorTests extends ClassyTestCase
@testName: "reactivepublish - Errors"

testClientInitialError: () ->
sub = Meteor.subscribe("initial error", {
onStop: (error) =>
@assertEqual(error.message, '[triggered error]')
})

testClientRerunError: () ->
isReady = false
Meteor.subscribe("rereun error", {
onStop: @expect (error) =>
@assertEqual(isReady, true, 'received error on rerun')
@assertEqual(error.message, '[triggered error]')
onReady: () =>
isReady = true
})

ClassyTestCase.addTest new ReactivePublishErrorTests

0 comments on commit 3799cdf

Please sign in to comment.