Skip to content

Commit 7d71e16

Browse files
committed
Merge pull request #35 from Simperium/add/bucket-errors
Update Channel error handling
2 parents b6c98d7 + 8fa9c6e commit 7d71e16

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/simperium/channel.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ internal.applyChange = function( change, ghost ) {
152152
// clientid: 'node-b9776e96-c068-42ae-893a-03f50833bddb',
153153
// error: 400 }
154154
if ( change.error ) {
155-
error = new Error( 'Could not apply change to ' + ghost.key );
155+
error = new Error( `${change.error} - Could not apply change to: ${ghost.key}` );
156156
error.code = change.error;
157157
error.change = change;
158158
error.ghost = ghost;
@@ -232,6 +232,9 @@ export default function Channel( appid, access_token, bucket, store ) {
232232
bucket.emit( 'index' );
233233
} );
234234

235+
// forward errors to bucket instance
236+
this.on( 'error', bucket.emit.bind( bucket, 'error' ) )
237+
235238
bucket.update = function( id, object, options, callback ) {
236239
if ( typeof options === 'function' ) {
237240
callback = options;

src/simperium/ghost/store.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Store.prototype.get = function( id ) {
3535
var ghost = this.index[id];
3636
if ( !ghost ) {
3737
ghost = {data: {}};
38+
ghost.key = id;
3839
this.index[id] = JSON.stringify( ghost );
3940
} else {
4041
ghost = JSON.parse( ghost );

test/simperium/channel_test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Channel from '../../src/simperium/channel'
33
import util from 'util'
44
import { parseMessage } from '../../src/simperium/util'
5-
import assert from 'assert'
5+
import assert, { equal } from 'assert'
66
import * as fn from './fn'
77
import jsondiff from '../../src/simperium/jsondiff'
88
import defaultGhostStoreProvider from '../../src/simperium/ghost/default'
@@ -309,6 +309,18 @@ describe( 'Channel', function() {
309309
bucket.update( key, {title: 'Goodbye world'} );
310310
} );
311311

312+
it( 'should emit errors on the bucket instance', function( done ) {
313+
const error = {error: 404, id: 'thing', ccids: ['abc']}
314+
bucket.on( 'error', ( e ) => {
315+
process.nextTick( () => {
316+
equal( 404, e.code )
317+
equal( `${ e.code } - Could not apply change to: ${error.id}`, e.message )
318+
done()
319+
} )
320+
} )
321+
channel.handleMessage( 'c:' + JSON.stringify( [ error ] ) );
322+
} );
323+
312324
it( 'should ignore 412 change errors', function( done ) {
313325
// if a change is sent and acknowledged with a 412, change should be dequeued and
314326
// no error should be emitted

0 commit comments

Comments
 (0)