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

15
server/src/routes/segments_router.ts

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

Loading…
Cancel
Save