Skip to content

Commit 969e609

Browse files
willhausmanpaveltiunov
authored andcommitted
feat: Error type for returning specific http status codes (#288). Thanks to @willhausman!
* type for returning specific http status codes * cube base error
1 parent c3eeb50 commit 969e609

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CubejsHandlerError extends Error {
2+
constructor(status, type, message) {
3+
super(message || type);
4+
this.status = status;
5+
this.type = type;
6+
}
7+
}
8+
9+
module.exports = CubejsHandlerError;
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
class UserError extends Error {}
1+
const CubejsHandlerError = require('./CubejsHandlerError');
2+
3+
class UserError extends CubejsHandlerError {
4+
constructor(message) {
5+
super(400, 'User Error', message);
6+
}
7+
}
28

39
module.exports = UserError;

packages/cubejs-api-gateway/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const moment = require('moment');
55
const dateParser = require('./dateParser');
66

77
const UserError = require('./UserError');
8+
const CubejsHandlerError = require('./CubejsHandlerError');
89
const SubscriptionServer = require('./SubscriptionServer');
910
const LocalSubscriptionStore = require('./LocalSubscriptionStore');
1011

@@ -429,14 +430,14 @@ class ApiGateway {
429430
handleError({
430431
e, context, query, res, requestStarted
431432
}) {
432-
if (e instanceof UserError) {
433+
if (e instanceof CubejsHandlerError) {
433434
this.log(context, {
434-
type: 'User Error',
435+
type: e.type,
435436
query,
436437
error: e.message,
437438
duration: this.duration(requestStarted)
438439
});
439-
res({ error: e.message }, { status: 400 });
440+
res({ error: e.message }, { status: e.status });
440441
} else if (e.error === 'Continue wait') {
441442
this.log(context, {
442443
type: 'Continue wait',

0 commit comments

Comments
 (0)