Skip to content

Commit

Permalink
Add custom status when failing a node12/14 request
Browse files Browse the repository at this point in the history
Tested with a 401 status before failing the message and saw
the code as expected.
Also tested with just calling fail, and saw the 500 code.

Closes #258 and Fixes: #257

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed May 12, 2021
1 parent eb16a41 commit 6b8c608
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions template/node12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ class FunctionEvent {

class FunctionContext {
constructor(cb) {
this.value = 200;
this.statusCode = 200;
this.cb = cb;
this.headerValues = {};
this.cbCalled = 0;
}

status(value) {
if(!value) {
return this.value;
status(statusCode) {
if(!statusCode) {
return this.statusCode;
}

this.value = value;
this.statusCode = statusCode;
return this;
}

Expand All @@ -86,6 +86,10 @@ class FunctionContext {

fail(value) {
let message;
if(this.status() == "200") {
this.status(500)
}

this.cbCalled++;
this.cb(value, message);
}
Expand All @@ -96,7 +100,7 @@ const middleware = async (req, res) => {
if (err) {
console.error(err);

return res.status(500)
return res.status(fnContext.status())
.send(err.toString ? err.toString() : err);
}

Expand Down
16 changes: 10 additions & 6 deletions template/node14/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ class FunctionEvent {

class FunctionContext {
constructor(cb) {
this.value = 200;
this.statusCode = 200;
this.cb = cb;
this.headerValues = {};
this.cbCalled = 0;
}

status(value) {
if(!value) {
return this.value;
status(statusCode) {
if(!statusCode) {
return this.statusCode;
}

this.value = value;
this.statusCode = statusCode;
return this;
}

Expand All @@ -86,6 +86,10 @@ class FunctionContext {

fail(value) {
let message;
if(this.status() == "200") {
this.status(500)
}

this.cbCalled++;
this.cb(value, message);
}
Expand All @@ -96,7 +100,7 @@ const middleware = async (req, res) => {
if (err) {
console.error(err);

return res.status(500)
return res.status(fnContext.status())
.send(err.toString ? err.toString() : err);
}

Expand Down

0 comments on commit 6b8c608

Please sign in to comment.