Skip to content

Commit

Permalink
Handle Server Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mu7ammadAbed committed Mar 31, 2020
1 parent dbb6a93 commit 66c4241
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
27 changes: 17 additions & 10 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
const getCohortQuery = require('../../../../database/queries');

exports.getSpecificCohort = async (req, res) => {
exports.getSpecificCohort = async (req, res, next) => {
try {
const { cohortid } = req.params;
const { rows } = await getCohortQuery(cohortid);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
if (cohortid > 0) {
const { rows } = await getCohortQuery(cohortid);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
} else {
res.status(404).json({
statusCode: 404,
message: "Sorry There's no cohort for this id",
});
}
} else {
throw new Error();
res.status(404).json({
statusCode: 404,
message: 'You enterd wrong cohort ID',
});
}
} catch (err) {
res.status(404).json({
statusCode: 404,
message: "Sorry There's no cohort for this id",
});
next(err);
}
};
8 changes: 2 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ const dbBuild = require('../server/database/config/build');

const app = require('../server/app');

beforeAll(() => {
return dbBuild();
});
beforeAll(() => dbBuild());

afterAll(() => {
return connection.end();
});
afterAll(() => connection.end());

describe('Cohort', () => {
test('Route /cohorts/1 status 200, json header, data.name =G8 ', (done) => {
Expand Down

0 comments on commit 66c4241

Please sign in to comment.