Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

catch backend errors in 'get' or 'getOrElse' and follow 'orElse' when they occur #3

Merged
merged 2 commits into from
Apr 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void function () {
}).nodeify(cb);
};
Cache.prototype.getOrElse = function (rawKey, val, opts, cb) {
var cache$, key, refreshValue, verifyFreshness;
var cache$, handleError, key, refreshValue, verifyFreshness;
key = this.applyPrefix(rawKey);
cache$ = optionalOpts(opts, cb);
cb = cache$.cb;
Expand Down Expand Up @@ -169,7 +169,10 @@ void function () {
return null != (null != wrappedValue ? wrappedValue.d : void 0) ? null != wrappedValue ? wrappedValue.d : void 0 : this$.staleOrPending[key];
};
}(this);
return this.getWrapped(key).then(verifyFreshness).nodeify(cb);
handleError = function (error) {
return null;
};
return this.getWrapped(key)['catch'](handleError).then(verifyFreshness).nodeify(cb);
};
return Cache;
}();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"memcached": "~0.2.0"
},
"devDependencies": {
"bondjs": "~1.0.0",
"coffee-script-redux": "2.0.0-beta7",
"expect.js": "0.2.0",
"mocha": "1.7.4",
Expand Down
7 changes: 6 additions & 1 deletion src/cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class Cache
# Return the value even if it's stale, we want the result ASAP
wrappedValue?.d ? @staleOrPending[key]

@getWrapped(key).then(verifyFreshness).nodeify cb
handleError = (error) ->
# we should let the refreshValue method work if getWrapped has problems
# tracking backend errors should be done with wrapping your backend clients
return null

@getWrapped(key).catch(handleError).then(verifyFreshness).nodeify cb

module.exports = Cache
15 changes: 14 additions & 1 deletion test/cache.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

expect = require 'expect.js'
bond = require 'bondjs'

Q = require 'q'
{defaults} = require 'underscore'

Cache = require '../lib/cache'
cached = require '../lib/cached'

Expand Down Expand Up @@ -138,3 +138,16 @@ describe 'Cache', ->
done()

cache.getOrElse 'bad_keys', errorGenerator, freshFor: 1, theCallback

it 'handles thrown get errors by falling back on the value refresher', (done) ->
valueGenerator = cached.deferred (cb) ->
cb null, 'fresh cats'

theCallback = (err, data) ->
expect(err).to.be null
expect(data).to.be 'fresh cats'
done()

bond(cache, 'getWrapped').return Q.reject new Error('backend troubles')

cache.getOrElse 'bad_get', valueGenerator, freshFor: 5, theCallback