Browse Source

Common mechanism for handling errors #474 (#475)

* handling middleware

* rm try/catch in routers
pull/1/head
Alexey Velikiy 6 years ago committed by GitHub
parent
commit
29b1977a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      server/src/index.ts
  2. 86
      server/src/routes/analytic_units_router.ts
  3. 30
      server/src/routes/panel_router.ts
  4. 22
      server/src/routes/segments_router.ts
  5. 20
      server/src/routes/threshold_router.ts

12
server/src/index.ts

@ -33,6 +33,18 @@ app.use(async function(ctx, next) {
ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
await next();
});
app.use(async function(ctx, next) {
try {
await next();
} catch (e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `${ctx.method} ${ctx.url} error: ${e.message}`
};
}
});
var rootRouter = new Router();

86
server/src/routes/analytic_units_router.ts

@ -7,7 +7,6 @@ import * as Router from 'koa-router';
async function getStatus(ctx: Router.IRouterContext) {
try {
let analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot get status of undefined id');
@ -25,18 +24,9 @@ async function getStatus(ctx: Router.IRouterContext) {
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.FAILED) {
ctx.response.body.errorMessage = analyticUnit.error;
}
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits/status error: ${e.message}`
};
}
}
async function getUnit(ctx: Router.IRouterContext) {
try {
let analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('No id param in query');
@ -52,19 +42,9 @@ async function getUnit(ctx: Router.IRouterContext) {
metric: analyticUnit.metric,
status: analyticUnit.status
};
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits error: ${e.message}`
};
}
}
async function getUnits(ctx: Router.IRouterContext) {
try {
const panelUrl = ctx.request.query.panelUrl;
if(panelUrl === undefined) {
throw new Error('Cannot get alerts of undefined panelUrl');
@ -75,17 +55,7 @@ async function getUnits(ctx: Router.IRouterContext) {
analyticUnits = [];
}
ctx.response.body = {
analyticUnits
};
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits/units error: ${e.message}`
};
}
ctx.response.body = { analyticUnits };
}
function getTypes(ctx: Router.IRouterContext) {
@ -93,20 +63,11 @@ function getTypes(ctx: Router.IRouterContext) {
}
async function createUnit(ctx: Router.IRouterContext) {
try {
let id = await createAnalyticUnitFromObject(ctx.request.body);
ctx.response.body = { id };
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `POST /analyticUnits error: ${e.message}`
};
}
}
async function updateUnit(ctx: Router.IRouterContext) {
try {
const unit = ctx.request.body as AnalyticUnit.AnalyticUnit;
if(unit.id === undefined) {
throw new Error('Cannot update undefined id');
@ -114,22 +75,13 @@ async function updateUnit(ctx: Router.IRouterContext) {
// TODO: we can't allow to update everything
AnalyticUnit.update(unit.id, unit);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch (e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `PATCH /analyticUnits error: ${e.message}`
};
}
}
async function updateMetric(ctx: Router.IRouterContext) {
try {
const { analyticUnitId, metric, datasource } = ctx.request.body as {
analyticUnitId: AnalyticUnit.AnalyticUnitId, metric: any, datasource: any
};
@ -149,17 +101,9 @@ async function updateMetric(ctx: Router.IRouterContext) {
code: 200,
message: 'Success'
};
} catch (e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `PATCH /analyticUnits/metric error: ${e.message}`
};
}
}
async function updateAlert(ctx: Router.IRouterContext) {
try {
const { analyticUnitId, alert } = ctx.request.body as {
analyticUnitId: AnalyticUnit.AnalyticUnitId, alert: boolean
};
@ -176,17 +120,10 @@ async function updateAlert(ctx: Router.IRouterContext) {
code: 200,
message: 'Success'
};
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `PATCH /analyticUnits/alert error: ${e.message}`
};
}
}
async function deleteUnit(ctx: Router.IRouterContext) {
try {
const analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot delete undefined id');
@ -196,35 +133,16 @@ async function deleteUnit(ctx: Router.IRouterContext) {
code: 200,
message: 'Success'
};
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `DELETE /analyticUnits error: ${e.message}`
};
}
}
async function runDetect(ctx: Router.IRouterContext) {
try {
const { id: analyticUnitId } = ctx.request.body as { id: AnalyticUnit.AnalyticUnitId };
AnalyticsController.runFirstLearning(analyticUnitId);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch (e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `POST /analyticUnits/detect error: ${e.message}`
};
}
}
export var router = new Router();

30
server/src/routes/panel_router.ts

@ -5,65 +5,35 @@ import * as Router from 'koa-router';
async function getAnalyticUnits(ctx: Router.IRouterContext) {
try {
let panelUrl: string = ctx.request.query.panelUrl;
if(panelUrl === undefined || panelUrl === '') {
throw new Error('panelUrl is missing');
}
const analyticUnits = await Panel.findOne({ panelUrl });
ctx.response.body = { analyticUnits };
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `GET /panel error: ${e.message}`
};
}
}
async function addAnalyticUnit(ctx: Router.IRouterContext) {
try {
let { panelUrl, analyticUnitId } = ctx.request.body as {
panelUrl: string, analyticUnitId: AnalyticUnitId
};
await Panel.insertAnalyticUnit(panelUrl, analyticUnitId);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `POST /panel error: ${e.message}`
};
}
}
async function deleteAnalyticUnit(ctx: Router.IRouterContext) {
try {
let { panelUrl, analyticUnitId } = ctx.request.body as {
panelUrl: string, analyticUnitId: AnalyticUnitId
};
// TODO: stop task when analytic unit is removed
await Panel.removeAnalyticUnit(panelUrl, analyticUnitId);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `DELETE /panel error: ${e.message}`
};
}
}
export const router = new Router();

22
server/src/routes/segments_router.ts

@ -7,7 +7,6 @@ import * as Router from 'koa-router';
async function getSegments(ctx: Router.IRouterContext) {
try {
let id: AnalyticUnitId = ctx.request.query.id;
if(id === undefined || id === '') {
throw new Error('analyticUnitId (id) is missing');
@ -23,23 +22,11 @@ async function getSegments(ctx: Router.IRouterContext) {
if(!isNaN(+ctx.request.query.to)) {
query.timeToLTE = +ctx.request.query.to;
}
let segments = await Segment.findMany(id, query);
ctx.response.body = { segments };
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `GET /segments error: ${e.message}`
};
}
}
async function updateSegments(ctx: Router.IRouterContext) {
try {
const {
addedSegments, id, removedSegments: removedIds
} = ctx.request.body as {
@ -55,15 +42,6 @@ async function updateSegments(ctx: Router.IRouterContext) {
);
ctx.response.body = { addedIds };
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `PATCH /segments (learning) error: ${e.message}`
};
}
}
export const router = new Router();

20
server/src/routes/threshold_router.ts

@ -8,7 +8,7 @@ import * as _ from 'lodash';
async function getThresholds(ctx: Router.IRouterContext) {
try {
const ids: AnalyticUnitId[] = ctx.request.query.ids.split(',');
if(ids === undefined) {
@ -20,18 +20,10 @@ async function getThresholds(ctx: Router.IRouterContext) {
);
ctx.response.body = { thresholds };
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `GET /threshold error: ${e.message}`
};
}
}
async function updateThreshold(ctx: Router.IRouterContext) {
try {
const {
id, value, condition
} = ctx.request.body as {
@ -44,14 +36,6 @@ async function updateThreshold(ctx: Router.IRouterContext) {
code: 200,
message: 'Success'
};
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `PATCH /threshold error: ${e.message}`
};
}
}
export const router = new Router();

Loading…
Cancel
Save