Skip to content

Commit 2f85f93

Browse files
authored
Merge pull request #4 from antstackio/fix-early-response
fix multiple response being send
2 parents 1a9498c + 1146d96 commit 2f85f93

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ function createApp(reqParams: ReqParams, handler: HandlerFunc) {
3131

3232
if (!proceed) {
3333
res.statusCode = 401;
34-
res.json({
34+
return res.json({
3535
message: "unauthenticated request",
3636
});
3737
}
38-
next();
38+
39+
// if user has manually responded then skip going further
40+
if (!res.headersSent) next();
3941
} catch (e) {
40-
console.log("error is", e);
4142
res.statusCode = 400;
4243
res.json({
4344
message: "something went wrong",
@@ -55,7 +56,11 @@ function createApp(reqParams: ReqParams, handler: HandlerFunc) {
5556

5657
res.send({ data });
5758
} catch (e) {
58-
console.log("error is", e);
59+
res.statusCode = 400;
60+
res.json({
61+
message: "something went wrong",
62+
error: e.message,
63+
});
5964
}
6065
});
6166

0 commit comments

Comments
 (0)