Skip to content

Commit 94aef2a

Browse files
Merge pull request #6 from djMax/master
Error handling and a fix for renew
2 parents f7b2ae7 + db547e4 commit 94aef2a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
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
}

0 commit comments

Comments
 (0)