Skip to content

Commit 5301858

Browse files
Merge jonasmalacofilho#3: fix throw within callback and overlapping fetches/clones
from m0ppers/fix-uncaught-exception: Fix uncaught exception (exception was thrown within callback)
2 parents 1ed3c78 + bd2cbbd commit 5301858

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/Main.hx

+46-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import js.node.*;
22
import js.node.http.*;
3+
import js.Promise;
34

45
class Main {
56
static function parseAuth(s:String)
@@ -45,20 +46,43 @@ class Main {
4546

4647
static function update(remote, local, callback)
4748
{
48-
trace("updating: fetching");
49-
fetch(local, function (ferr, stdout, stderr) {
50-
if (ferr != null) {
51-
trace("updating: fetch failed, cloning");
52-
clone(remote, local, function (cerr, stdout, stderr) {
53-
if (cerr != null)
54-
throw 'git clone exited with non-zero status: ${cerr.code}';
55-
trace("updating: success");
56-
callback();
49+
if (!updatePromises.exists(local)) {
50+
updatePromises[local] = new Promise(function(resolve, reject) {
51+
trace("updating: fetching");
52+
fetch(local, function (ferr, stdout, stderr) {
53+
if (ferr != null) {
54+
trace("updating: fetch failed, cloning");
55+
clone(remote, local, function (cerr, stdout, stderr) {
56+
if (cerr != null) {
57+
resolve('git clone exited with non-zero status: ${cerr.code}');
58+
} else {
59+
trace("updating: success");
60+
resolve(null);
61+
}
62+
});
63+
} else {
64+
trace("updating: success");
65+
resolve(null);
66+
}
5767
});
58-
} else {
59-
trace("updating: success");
60-
callback();
61-
}
68+
})
69+
.then(function(success) {
70+
updatePromises.remove(local);
71+
return Promise.resolve(success);
72+
})
73+
.catchError(function(err) {
74+
updatePromises.remove(local);
75+
return Promise.reject(err);
76+
});
77+
} else {
78+
trace("reusing existing promise");
79+
}
80+
return updatePromises[local]
81+
.then(function(nothing:Dynamic) {
82+
trace("promise fulfilled");
83+
callback(null);
84+
}, function(err:Dynamic) {
85+
callback(err);
6286
});
6387
}
6488

@@ -92,7 +116,14 @@ class Main {
92116
}
93117

94118
if (params.isInfoRequest) {
95-
update(remote, local, function () {
119+
update(remote, local, function (err) {
120+
if (err != null) {
121+
trace('ERROR: $err');
122+
trace(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
123+
res.statusCode = 500;
124+
res.end();
125+
return;
126+
}
96127
res.statusCode = 200;
97128
res.setHeader("Content-Type", 'application/x-${params.service}-advertisement');
98129
res.setHeader("Cache-Control", "no-cache");
@@ -133,6 +164,7 @@ class Main {
133164
}
134165
}
135166

167+
static var updatePromises = new Map<String, Promise<Dynamic>>();
136168
static var cacheDir = "/tmp/var/cache/git/";
137169
static var listenPort = 8080;
138170
static var usage = "

0 commit comments

Comments
 (0)