2019-06-23 03:35:23 -07:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import { parse, stringify } from 'flatted';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecutionDb,
|
|
|
|
IExecutionFlatted,
|
|
|
|
IExecutionFlattedDb,
|
|
|
|
IExecutionResponse,
|
|
|
|
IWorkflowDb,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special Error which allows to return also an error code and http status code
|
|
|
|
*
|
|
|
|
* @export
|
2019-08-28 08:16:09 -07:00
|
|
|
* @class ResponseError
|
2019-06-23 03:35:23 -07:00
|
|
|
* @extends {Error}
|
|
|
|
*/
|
2019-08-28 08:16:09 -07:00
|
|
|
export class ResponseError extends Error {
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
// The HTTP status code of response
|
|
|
|
httpStatusCode?: number;
|
|
|
|
|
|
|
|
// The error code in the resonse
|
|
|
|
errorCode?: number;
|
|
|
|
|
|
|
|
/**
|
2019-08-28 08:16:09 -07:00
|
|
|
* Creates an instance of ResponseError.
|
2019-06-23 03:35:23 -07:00
|
|
|
* @param {string} message The error message
|
|
|
|
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
|
|
|
|
* @param {number} [httpStatusCode] The HTTP status code the response should have
|
2019-08-28 08:16:09 -07:00
|
|
|
* @memberof ResponseError
|
2019-06-23 03:35:23 -07:00
|
|
|
*/
|
|
|
|
constructor(message: string, errorCode?: number, httpStatusCode?: number) {
|
|
|
|
super(message);
|
2019-08-28 08:16:09 -07:00
|
|
|
this.name = 'ResponseError';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
if (errorCode) {
|
|
|
|
this.errorCode = errorCode;
|
|
|
|
}
|
|
|
|
if (httpStatusCode) {
|
|
|
|
this.httpStatusCode = httpStatusCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-04 05:24:48 -07:00
|
|
|
|
|
|
|
export function basicAuthAuthorizationError(resp: Response, realm: string, message?: string) {
|
|
|
|
resp.statusCode = 401;
|
|
|
|
resp.setHeader('WWW-Authenticate', `Basic realm="${realm}"`);
|
2020-03-31 09:41:21 -07:00
|
|
|
resp.json({code: resp.statusCode, message});
|
2019-08-04 05:24:48 -07:00
|
|
|
}
|
|
|
|
|
2019-10-14 13:37:51 -07:00
|
|
|
export function jwtAuthAuthorizationError(resp: Response, message?: string) {
|
|
|
|
resp.statusCode = 403;
|
2020-03-31 09:41:21 -07:00
|
|
|
resp.json({code: resp.statusCode, message});
|
2019-10-14 13:37:51 -07:00
|
|
|
}
|
|
|
|
|
2019-08-04 05:24:48 -07:00
|
|
|
|
2019-08-28 08:03:35 -07:00
|
|
|
export function sendSuccessResponse(res: Response, data: any, raw?: boolean, responseCode?: number) { // tslint:disable-line:no-any
|
|
|
|
if (responseCode !== undefined) {
|
|
|
|
res.status(responseCode);
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
if (raw === true) {
|
2020-03-31 09:41:21 -07:00
|
|
|
res.json(data);
|
2019-06-23 03:35:23 -07:00
|
|
|
} else {
|
2020-03-31 09:41:21 -07:00
|
|
|
res.json({
|
2019-06-23 03:35:23 -07:00
|
|
|
data
|
2020-03-31 09:41:21 -07:00
|
|
|
});
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-28 08:16:09 -07:00
|
|
|
export function sendErrorResponse(res: Response, error: ResponseError) {
|
2019-06-23 03:35:23 -07:00
|
|
|
let httpStatusCode = 500;
|
|
|
|
if (error.httpStatusCode) {
|
|
|
|
httpStatusCode = error.httpStatusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
console.error('ERROR RESPONSE');
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
code: 0,
|
|
|
|
message: 'Unknown error',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (error.errorCode) {
|
|
|
|
response.code = error.errorCode;
|
|
|
|
}
|
|
|
|
if (error.message) {
|
|
|
|
response.message = error.message;
|
|
|
|
}
|
|
|
|
if (error.stack && process.env.NODE_ENV !== 'production') {
|
|
|
|
// @ts-ignore
|
|
|
|
response.stack = error.stack;
|
|
|
|
}
|
|
|
|
|
2020-03-31 09:41:21 -07:00
|
|
|
res.status(httpStatusCode).json(response);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function which does not just allow to return Promises it also makes sure that
|
|
|
|
* all the responses have the same format
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {(req: Request, res: Response) => Promise<any>} processFunction The actual function to process the request
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function send(processFunction: (req: Request, res: Response) => Promise<any>) { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
return async (req: Request, res: Response) => {
|
|
|
|
try {
|
|
|
|
const data = await processFunction(req, res);
|
|
|
|
|
|
|
|
// Success response
|
|
|
|
sendSuccessResponse(res, data);
|
|
|
|
} catch (error) {
|
|
|
|
// Error response
|
|
|
|
sendErrorResponse(res, error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flattens the Execution data.
|
|
|
|
* As it contains a lot of references which normally would be saved as duplicate data
|
|
|
|
* with regular JSON.stringify it gets flattened which keeps the references in place.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {IExecutionDb} fullExecutionData The data to flatten
|
|
|
|
* @returns {IExecutionFlatted}
|
|
|
|
*/
|
|
|
|
export function flattenExecutionData(fullExecutionData: IExecutionDb): IExecutionFlatted {
|
|
|
|
// Flatten the data
|
|
|
|
const returnData: IExecutionFlatted = Object.assign({}, {
|
|
|
|
data: stringify(fullExecutionData.data),
|
|
|
|
mode: fullExecutionData.mode,
|
|
|
|
startedAt: fullExecutionData.startedAt,
|
|
|
|
stoppedAt: fullExecutionData.stoppedAt,
|
|
|
|
finished: fullExecutionData.finished ? fullExecutionData.finished : false,
|
|
|
|
workflowId: fullExecutionData.workflowId,
|
|
|
|
workflowData: fullExecutionData.workflowData!,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fullExecutionData.id !== undefined) {
|
|
|
|
returnData.id = fullExecutionData.id!.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullExecutionData.retryOf !== undefined) {
|
|
|
|
returnData.retryOf = fullExecutionData.retryOf!.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullExecutionData.retrySuccessId !== undefined) {
|
|
|
|
returnData.retrySuccessId = fullExecutionData.retrySuccessId!.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unflattens the Execution data.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {IExecutionFlattedDb} fullExecutionData The data to unflatten
|
|
|
|
* @returns {IExecutionResponse}
|
|
|
|
*/
|
|
|
|
export function unflattenExecutionData(fullExecutionData: IExecutionFlattedDb): IExecutionResponse {
|
|
|
|
|
|
|
|
const returnData: IExecutionResponse = Object.assign({}, {
|
|
|
|
id: fullExecutionData.id.toString(),
|
|
|
|
workflowData: fullExecutionData.workflowData as IWorkflowDb,
|
|
|
|
data: parse(fullExecutionData.data),
|
|
|
|
mode: fullExecutionData.mode,
|
|
|
|
startedAt: fullExecutionData.startedAt,
|
|
|
|
stoppedAt: fullExecutionData.stoppedAt,
|
|
|
|
finished: fullExecutionData.finished ? fullExecutionData.finished : false
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|