Skip to content

Instantly share code, notes, and snippets.

@natevw
Last active December 31, 2015 02:09
Show Gist options
  • Save natevw/7919020 to your computer and use it in GitHub Desktop.
Save natevw/7919020 to your computer and use it in GitHub Desktop.
Cloudant-specific Fermata plugin to treat write quorum failures as an error. (BigCouch returns a 202 status code which would normally be considered "successful", but the update could still end up lost or in conflict. So you may prefer to handle more similarly to a 500. Note that in this case, the best strategy AFAICT is to *write* until a 201 is…
// WORKAROUND: custom statusCheck to treat 202 responses as an error
// if Cloudant sends this, we can't be sure the write won't conflict
// see https://github.com/cloudant/bigcouch/issues/55
// and https://cloudant.com/for-developers/faq/data/
// and http://bigcouch.cloudant.com/api
// it's futile to attempt a unanimous quorum read to be sure (r=N is only advisory)
// so…assume the worst (e.g. conflicting or lost update) and treat these like a 500
// NOTE: this is copy-pasta from Fermata's builtin plugins.
fermata.registerPlugin('statusCheck_bigcouch', function (transport) {
return function (request, callback) {
return transport(request, function (err, response) {
if (!err && (response.status.toFixed()[0] !== '2' || response.status === 202)) {
err = Error("Bad status code from server: " + response.status);
err.status = response.status;
}
callback(err, response);
});
};
});
fermata.registerPlugin('json_bigcouch', function (transport, baseURL) {
this.base = baseURL;
return transport.using('statusCheck_bigcouch').using('autoConvert', "application/json");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment