Browse Source

Specific error messages #201 (#222)

* Improve error messages in /analyticUnits

* Improve /segments error messages
pull/1/head
rozetko 6 years ago committed by GitHub
parent
commit
dfd2dd3673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      server/src/routes/analytic_units_router.ts
  2. 47
      server/src/routes/segments_router.ts

31
server/src/routes/analytic_units_router.ts

@ -13,6 +13,9 @@ async function getStatus(ctx: Router.IRouterContext) {
}
let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit === null) {
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`);
}
ctx.response.body = {
status: analyticUnit.status
@ -24,19 +27,24 @@ async function getStatus(ctx: Router.IRouterContext) {
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = 'Can`t find anything';
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');
}
let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit === null) {
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`);
}
ctx.response.body = {
name: analyticUnit.name,
@ -46,9 +54,11 @@ async function getUnit(ctx: Router.IRouterContext) {
} catch(e) {
console.error(e);
// TODO: better send 404 when we know than isn`t found
ctx.response.status = 500;
ctx.response.body = 'Can`t find anything';
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits error: ${e.message}`
};
}
}
@ -60,7 +70,7 @@ async function createUnit(ctx: Router.IRouterContext) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `Creation error: ${e.message}`
message: `POST /analyticUnits error: ${e.message}`
};
}
@ -68,16 +78,21 @@ async function createUnit(ctx: Router.IRouterContext) {
async function deleteUnit(ctx: Router.IRouterContext) {
try {
await AnalyticUnit.remove(ctx.request.query.id);
const analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot delete undefined id');
}
await AnalyticUnit.remove(analyticUnitId);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `Deletion error: ${e.message}`
message: `DELETE /analyticUnits error: ${e.message}`
};
}
}

47
server/src/routes/segments_router.ts

@ -1,32 +1,40 @@
import * as AnalyticsController from '../controllers/analytics_controller';
import { AnalyticUnitId, AnalyticUnitStatus } from '../models/analytic_unit_model';
import { AnalyticUnitId } from '../models/analytic_unit_model';
import * as Segment from '../models/segment_model';
import * as Router from 'koa-router';
async function getSegments(ctx: Router.IRouterContext) {
let id: AnalyticUnitId = ctx.request.query.id;
if(id === undefined || id === '') {
throw new Error('analyticUnitId (id) is missing');
}
let query: Segment.FindManyQuery = {};
if(!isNaN(+ctx.request.query.lastSegmentId)) {
query.intexGT = +ctx.request.query.lastSegmentId;
}
if(!isNaN(+ctx.request.query.from)) {
query.timeFromGTE = +ctx.request.query.from;
}
if(!isNaN(+ctx.request.query.to)) {
query.timeToLTE = +ctx.request.query.to;
}
try {
let id: AnalyticUnitId = ctx.request.query.id;
if(id === undefined || id === '') {
throw new Error('analyticUnitId (id) is missing');
}
let query: Segment.FindManyQuery = {};
let segments = await Segment.findMany(id, query);
if(!isNaN(+ctx.request.query.lastSegmentId)) {
query.intexGT = +ctx.request.query.lastSegmentId;
}
if(!isNaN(+ctx.request.query.from)) {
query.timeFromGTE = +ctx.request.query.from;
}
if(!isNaN(+ctx.request.query.to)) {
query.timeToLTE = +ctx.request.query.to;
}
ctx.response.body = { segments };
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) {
@ -49,10 +57,11 @@ async function updateSegments(ctx: Router.IRouterContext) {
ctx.response.body = { addedIds, removed };
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `Learning error: ${e.message}`
message: `PATCH /segments (learning) error: ${e.message}`
};
}
}

Loading…
Cancel
Save