Skip to content

Commit

Permalink
Merge pull request #6 from djMax/master
Browse files Browse the repository at this point in the history
Error handling and a fix for renew
  • Loading branch information
thebigredgeek authored Jun 27, 2017
2 parents f7b2ae7 + db547e4 commit 94aef2a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/microlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ export default class Microlock extends EventEmitter {
return resolve(val)
});
})
.catch(() => this.__throwAlreadyLocked());
.catch((e) => {
// Key already exists
if (e.errorCode === 105) {
this.__throwAlreadyLocked();
}
throw e;
});
}

unlock () {
Expand All @@ -143,12 +149,18 @@ export default class Microlock extends EventEmitter {
return resolve(res);
});
})
.catch(() => this.__throwLockNotOwned());
.catch((e) => {
// KeyNotFound or CompareFailed
if (e.errorCode === 100 || e.errorCode === 101) {
this.__throwLockNotOwned();
}
throw e;
});
}

renew () {
return new Promise((resolve, reject) => {
return this.__etcd.set(this.__key, this.__node_id, {
return this.__etcd.set(this.__key, null, {
prevValue: this.__node_id,
refresh: true,
ttl: this.__ttl
Expand All @@ -157,6 +169,12 @@ export default class Microlock extends EventEmitter {
return resolve(val);
})
})
.catch(() => this.__throwLockNotOwned());
.catch((e) => {
// KeyNotFound or CompareFailed
if (e.errorCode === 100 || e.errorCode === 101) {
this.__throwLockNotOwned();
}
throw e;
});
}
}

0 comments on commit 94aef2a

Please sign in to comment.