Skip to content

Commit 06a8b30

Browse files
committed
feat (invoke-service) add support for returning specific http status
1 parent 2b61c64 commit 06a8b30

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/server-code/api/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ class ServerCodeError extends Error {
4848
/**
4949
* @param {Number} code
5050
* @param {String} message
51+
* @param {Number=} httpStatusCode
5152
*/
52-
constructor(code, message) {
53+
constructor(code, message, httpStatusCode) {
5354
super(message);
5455

5556
this.code = code;
5657
this.message = message;
58+
this.httpStatusCode = httpStatusCode;
5759
}
5860
}
5961

lib/server-code/runners/tasks/util/result-wrapper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/**
44
* @typedef {Object} ExceptionWrapper
55
* @property {String} ___jsonclass
6+
* @property {Number} code
7+
* @property {Number} httpStatusCode
68
* @property {String} exceptionClass
79
* @property {String} exceptionMessage
810
*/
@@ -26,10 +28,12 @@ exports.invocationResult = function(err, args) {
2628
*/
2729
exports.exception = function(err) {
2830
const errCode = typeof err.code === 'number' ? err.code : 0;
29-
31+
const httpStatusCode = typeof err.httpStatusCode === 'number' ? err.httpStatusCode : -1;
32+
3033
return {
3134
___jsonclass : err.___jsonclass || 'com.backendless.commons.exception.ExceptionWrapper',
3235
code : errCode,
36+
httpStatusCode : httpStatusCode,
3337
exceptionClass : err.exceptionClass || 'java.lang.Exception',
3438
exceptionMessage: err.exceptionMessage || err.message || err
3539
};

test/invoke-service.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,36 @@ describe('[invoke-service] task executor', function() {
113113
});
114114
});
115115

116+
it('should handle custom error', function() {
117+
class Foo {
118+
bar() {
119+
throw new Backendless.ServerCode.Error(126, 'erred');
120+
}
121+
}
122+
123+
return invoke(createTask(Foo.name, 'bar'), new TestModel().addService(Foo))
124+
.then(res => {
125+
assert.equal(res.exceptionMessage, 'erred');
126+
assert.equal(res.code, 126);
127+
assert.equal(res.httpStatusCode, -1);
128+
});
129+
});
130+
131+
it('should handle custom error with specific http status code', function() {
132+
class Foo {
133+
bar() {
134+
throw new Backendless.ServerCode.Error(126, 'erred', 403);
135+
}
136+
}
137+
138+
return invoke(createTask(Foo.name, 'bar'), new TestModel().addService(Foo))
139+
.then(res => {
140+
assert.equal(res.exceptionMessage, 'erred');
141+
assert.equal(res.code, 126);
142+
assert.equal(res.httpStatusCode, 403);
143+
});
144+
});
145+
116146
it('should transform arguments if custom types are defined', function() {
117147
class Foo {
118148
}

0 commit comments

Comments
 (0)