Skip to content

Commit 9c8b008

Browse files
committed
Only react to a matching error from etcd, passing through other errors; fix renew()
1 parent f7b2ae7 commit 9c8b008

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/microlock.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ export default class Microlock extends EventEmitter {
133133
return resolve(val)
134134
});
135135
})
136-
.catch(() => this.__throwAlreadyLocked());
136+
.catch((e) => {
137+
// Key already exists
138+
if (e.errorCode === 105) {
139+
this.__throwAlreadyLocked();
140+
}
141+
throw e;
142+
});
137143
}
138144

139145
unlock () {
@@ -143,12 +149,18 @@ export default class Microlock extends EventEmitter {
143149
return resolve(res);
144150
});
145151
})
146-
.catch(() => this.__throwLockNotOwned());
152+
.catch((e) => {
153+
// KeyNotFound or CompareFailed
154+
if (e.errorCode === 100 || e.errorCode === 101) {
155+
this.__throwLockNotOwned();
156+
}
157+
throw e;
158+
});
147159
}
148160

149161
renew () {
150162
return new Promise((resolve, reject) => {
151-
return this.__etcd.set(this.__key, this.__node_id, {
163+
return this.__etcd.set(this.__key, null, {
152164
prevValue: this.__node_id,
153165
refresh: true,
154166
ttl: this.__ttl
@@ -157,6 +169,12 @@ export default class Microlock extends EventEmitter {
157169
return resolve(val);
158170
})
159171
})
160-
.catch(() => this.__throwLockNotOwned());
172+
.catch((e) => {
173+
// KeyNotFound or CompareFailed
174+
if (e.errorCode === 100 || e.errorCode === 101) {
175+
this.__throwLockNotOwned();
176+
}
177+
throw e;
178+
});
161179
}
162180
}

test/integration/microlock_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Microlock, {
1212
InvalidTtlError,
1313
AlreadyLockedError,
1414
LockNotOwnedError
15-
} from '../../lib/microlock';
15+
} from '../../src/microlock';
1616

1717
describe('microlock', () => {
1818
let etcd = null

0 commit comments

Comments
 (0)