mirror of
https://github.com/caprover/caprover
synced 2026-09-26 16:45:39 +00:00
Added a generic error handler with support for captian errors in app.ts
This commit is contained in:
@@ -17,5 +17,20 @@ class ApiStatusCodes {
|
||||
};
|
||||
}
|
||||
}
|
||||
ApiStatusCodes.STATUS_OK_DEPLOY_STARTED = 101;
|
||||
ApiStatusCodes.STATUS_ERROR_GENERIC = 1000;
|
||||
ApiStatusCodes.STATUS_OK = 100;
|
||||
ApiStatusCodes.STATUS_ERROR_CAPTAIN_NOT_INITIALIZED = 1001;
|
||||
ApiStatusCodes.STATUS_ERROR_USER_NOT_INITIALIZED = 1101;
|
||||
ApiStatusCodes.STATUS_ERROR_NOT_AUTHORIZED = 1102;
|
||||
ApiStatusCodes.STATUS_ERROR_ALREADY_EXIST = 1103;
|
||||
ApiStatusCodes.STATUS_ERROR_BAD_NAME = 1104;
|
||||
ApiStatusCodes.STATUS_WRONG_PASSWORD = 1105;
|
||||
ApiStatusCodes.STATUS_AUTH_TOKEN_INVALID = 1106;
|
||||
ApiStatusCodes.VERIFICATION_FAILED = 1107;
|
||||
ApiStatusCodes.ILLEGAL_OPERATION = 1108;
|
||||
ApiStatusCodes.BUILD_ERROR = 1109;
|
||||
ApiStatusCodes.ILLEGAL_PARAMETER = 1110;
|
||||
ApiStatusCodes.NOT_FOUND = 1111;
|
||||
module.exports = ApiStatusCodes;
|
||||
//# sourceMappingURL=ApiStatusCodes.js.map
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"ApiStatusCodes.js","sourceRoot":"","sources":["../../src/api/ApiStatusCodes.ts"],"names":[],"mappings":";AAAA,iDAA6C;AAE7C,qCAAqC;AACrC,0CAA0C;AAE1C,MAAM,cAAc;IAChB,MAAM,CAAC,WAAW,CAAC,IAAY,EAAE,OAAe;QAC5C,OAAO,IAAI,2BAAY,CAAC,IAAI,EAAE,OAAO,IAAI,MAAM,CAAC,CAAA;IACpD,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,GAAa;QAC9B,OAAO,UAA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ACjC,oCAAqB,GAAG,IAAI,CAAA;AAC5B,oCAAqB,GAAG,IAAI,CAAA;AAC5B,wCAAyB,GAAG,IAAI,CAAA;AAChC,kCAAmB,GAAG,IAAI,CAAA;AAC1B,gCAAiB,GAAG,IAAI,CAAA;AACxB,0BAAW,GAAG,IAAI,CAAA;AAClB,gCAAiB,GAAG,IAAI,CAAA;AACxB,wBAAS,GAAG,IAAI,CAAA;AAGpC,iBAAS,cAAc,CAAA"}
|
||||
@@ -4,6 +4,7 @@ class CaptainError extends Error {
|
||||
constructor(code, msg) {
|
||||
super(msg);
|
||||
this.captainErrorType = code;
|
||||
this.apiMessage = msg;
|
||||
}
|
||||
}
|
||||
exports.CaptainError = CaptainError;
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"CaptainError.js","sourceRoot":"","sources":["../../src/api/CaptainError.ts"],"names":[],"mappings":";;AAAA,MAAa,YAAa,SAAQ,KAAK;IAInC,YAAY,IAAY,EAAE,GAAW;QACjC,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAA;IAChC,CAAC;CACJ;AARD,oCAQC"}
|
||||
{"version":3,"file":"CaptainError.js","sourceRoot":"","sources":["../../src/api/CaptainError.ts"],"names":[],"mappings":";;AAAA,MAAa,YAAa,SAAQ,KAAK;IAInC,YAAY,IAAY,EAAE,GAAW;QACjC,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAA;QAC5B,IAAI,CAAC,UAAU,GAAG,GAAG,CAAA;IACzB,CAAC;CACJ;AATD,oCASC"}
|
||||
@@ -116,11 +116,6 @@ app.use(API_PREFIX + ':apiVersionFromRequest/', function (req, res, next) {
|
||||
res.send(response);
|
||||
return;
|
||||
}
|
||||
if (!res.locals.namespace &&
|
||||
!req.originalUrl.startsWith(API_PREFIX + CaptainConstants.apiVersion + '/user/webhooks/')) {
|
||||
res.send(new BaseApi(ApiStatusCodes.STATUS_ERROR_GENERIC, 'no namespace'));
|
||||
return;
|
||||
}
|
||||
next();
|
||||
});
|
||||
// unsecured end points:
|
||||
@@ -136,13 +131,7 @@ app.use(function (req, res, next) {
|
||||
});
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||
Logger.e(err);
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
Promise.reject(err).catch(ApiStatusCodes.createCatcher(res));
|
||||
});
|
||||
// Initializing with delay helps with debugging. Usually, docker didn't see the CAPTAIN service
|
||||
// if this was done without a delay
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -145,7 +145,7 @@ class Authenticator {
|
||||
}
|
||||
static get(namespace) {
|
||||
if (!namespace) {
|
||||
throw new Error('namespace is needed');
|
||||
throw ApiStatusCodes.createError(ApiStatusCodes.STATUS_ERROR_NOT_AUTHORIZED, 'Empty namespace');
|
||||
}
|
||||
if (!authenticatorCache[namespace]) {
|
||||
const captainSalt = CaptainManager.get().getCaptainSalt();
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -21,21 +21,21 @@ class ApiStatusCodes {
|
||||
}
|
||||
}
|
||||
|
||||
static STATUS_OK_DEPLOY_STARTED: 101
|
||||
static STATUS_ERROR_GENERIC: 1000
|
||||
static STATUS_OK: 100
|
||||
static STATUS_ERROR_CAPTAIN_NOT_INITIALIZED: 1001
|
||||
static STATUS_ERROR_USER_NOT_INITIALIZED: 1101
|
||||
static STATUS_ERROR_NOT_AUTHORIZED: 1102
|
||||
static STATUS_ERROR_ALREADY_EXIST: 1103
|
||||
static STATUS_ERROR_BAD_NAME: 1104
|
||||
static STATUS_WRONG_PASSWORD: 1105
|
||||
static STATUS_AUTH_TOKEN_INVALID: 1106
|
||||
static VERIFICATION_FAILED: 1107
|
||||
static ILLEGAL_OPERATION: 1108
|
||||
static BUILD_ERROR: 1109
|
||||
static ILLEGAL_PARAMETER: 1110
|
||||
static NOT_FOUND: 1111
|
||||
static readonly STATUS_OK_DEPLOY_STARTED = 101
|
||||
static readonly STATUS_ERROR_GENERIC = 1000
|
||||
static readonly STATUS_OK = 100
|
||||
static readonly STATUS_ERROR_CAPTAIN_NOT_INITIALIZED = 1001
|
||||
static readonly STATUS_ERROR_USER_NOT_INITIALIZED = 1101
|
||||
static readonly STATUS_ERROR_NOT_AUTHORIZED = 1102
|
||||
static readonly STATUS_ERROR_ALREADY_EXIST = 1103
|
||||
static readonly STATUS_ERROR_BAD_NAME = 1104
|
||||
static readonly STATUS_WRONG_PASSWORD = 1105
|
||||
static readonly STATUS_AUTH_TOKEN_INVALID = 1106
|
||||
static readonly VERIFICATION_FAILED = 1107
|
||||
static readonly ILLEGAL_OPERATION = 1108
|
||||
static readonly BUILD_ERROR = 1109
|
||||
static readonly ILLEGAL_PARAMETER = 1110
|
||||
static readonly NOT_FOUND = 1111
|
||||
}
|
||||
|
||||
export = ApiStatusCodes
|
||||
|
||||
@@ -5,5 +5,6 @@ export class CaptainError extends Error {
|
||||
constructor(code: number, msg: string) {
|
||||
super(msg)
|
||||
this.captainErrorType = code
|
||||
this.apiMessage = msg
|
||||
}
|
||||
}
|
||||
|
||||
+1
-21
@@ -166,18 +166,6 @@ app.use(API_PREFIX + ':apiVersionFromRequest/', function(req, res, next) {
|
||||
return
|
||||
}
|
||||
|
||||
if (
|
||||
!res.locals.namespace &&
|
||||
!req.originalUrl.startsWith(
|
||||
API_PREFIX + CaptainConstants.apiVersion + '/user/webhooks/'
|
||||
)
|
||||
) {
|
||||
res.send(
|
||||
new BaseApi(ApiStatusCodes.STATUS_ERROR_GENERIC, 'no namespace')
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
@@ -198,15 +186,7 @@ app.use(function(req, res, next) {
|
||||
|
||||
// error handler
|
||||
app.use(function(err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
||||
|
||||
Logger.e(err)
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500)
|
||||
res.render('error')
|
||||
Promise.reject(err).catch(ApiStatusCodes.createCatcher(res))
|
||||
} as express.ErrorRequestHandler)
|
||||
|
||||
// Initializing with delay helps with debugging. Usually, docker didn't see the CAPTAIN service
|
||||
|
||||
@@ -254,7 +254,10 @@ class Authenticator {
|
||||
|
||||
static get(namespace: string): Authenticator {
|
||||
if (!namespace) {
|
||||
throw new Error('namespace is needed')
|
||||
throw ApiStatusCodes.createError(
|
||||
ApiStatusCodes.STATUS_ERROR_NOT_AUTHORIZED,
|
||||
'Empty namespace'
|
||||
)
|
||||
}
|
||||
|
||||
if (!authenticatorCache[namespace]) {
|
||||
|
||||
Reference in New Issue
Block a user