Skip to content

Commit

Permalink
✨ api error handling
Browse files Browse the repository at this point in the history
- 에러 로그 생성
- http status code, message 전달
- 404 페이지 응답
  • Loading branch information
padosum committed Jan 7, 2023
1 parent 187a691 commit 8f70c02
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 58 deletions.
20 changes: 20 additions & 0 deletions src/server/app.js
Expand Up @@ -9,6 +9,22 @@ const webpackHotMiddleware = require('webpack-hot-middleware');

const app = express();

const errorLogger = (error, request, response, next) => {
console.error(`❗ error ${error.message}`);
next(error);
};

const errorResponder = (error, request, response, next) => {
response.header('Content-Type', 'application/json');
const status = error.status || 400;
response.status(status).send(error.message);
};

const invalidPathHandler = (request, response, next) => {
response.status(404);
response.send('invalid path');
};

app.use(helmet());

app.use((req, res, next) => {
Expand All @@ -23,6 +39,10 @@ app.use(express.urlencoded({ extended: false }));
const apiRouter = require('./routes/api.js');
app.use('/api', apiRouter);

app.use(errorLogger);
app.use(errorResponder);
app.use(invalidPathHandler);

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

if (process.env.NODE_ENV !== 'production') {
Expand Down
58 changes: 43 additions & 15 deletions src/server/controller/achievementsController.js
@@ -1,28 +1,56 @@
const achievements = require('../model/achievements');

module.exports = {
async all(req, res) {
const [allAchievements] = await achievements.get(req.pool);
res.send(allAchievements);
async all(req, res, next) {
try {
const [allAchievements] = await achievements.get(req.pool);
const mainAchievements = allAchievements.filter(({ achievementsId }) => !achievementsId);
const response = mainAchievements.map(achievement => {
const subAchievements = allAchievements.filter(({ achievementsId }) => achievementsId === achievement.id);
return {
...achievement,
subAchievements,
};
});
res.status(200).json(response);
} catch (err) {
next(err);
}
},

async get(req, res) {
const [achievement] = await achievements.getById(req.pool, req.params.id);
res.send(achievement);
async get(req, res, next) {
try {
const [achievement] = await achievements.getById(req.pool, req.params.id);
res.status(200).json(achievement);
} catch (err) {
next(err);
}
},

async store(req, res) {
const [result] = await achievements.create(req.pool, req.body);
res.send(result);
async store(req, res, next) {
try {
const [result] = await achievements.create(req.pool, req.body);
res.status(200).json(result);
} catch (err) {
next(err);
}
},

async update(req, res) {
const [result] = await achievements.update(req.pool, req.params.id, req.body);
res.send(result);
async update(req, res, next) {
try {
const [result] = await achievements.update(req.pool, req.params.id, req.body);
res.status(200).json(result);
} catch (err) {
next(err);
}
},

async delete(req, res) {
const [result] = await achievements.delete(req.pool, req.params.id);
res.send(result);
async delete(req, res, next) {
try {
const [result] = await achievements.delete(req.pool, req.params.id);
res.status(200).json(result);
} catch (err) {
next(err);
}
},
};
36 changes: 19 additions & 17 deletions src/server/controller/categoriesController.js
@@ -1,23 +1,25 @@
const categories = require('../model/categories');

module.exports = {
async all(req, res) {
const [allCategories] = await categories.get(req.pool);
const mainCategories = allCategories.filter(({is_sub}) => !is_sub);
async all(req, res, next) {
try {
const [allCategories] = await categories.get(req.pool);
const mainCategories = allCategories.filter(({ sub }) => !sub);

const response = mainCategories.map(({id, name}) => {
const subCategories = allCategories
.filter(({category_id, is_sub}) => is_sub && id === category_id)
.map(({id, name}) => {
return {id, name};
});
return {
id,
name,
subCategories,
};
});

res.status(200).send(response);
const response = mainCategories.map(({ id, name, title }) => {
const subCategories = allCategories
.filter(({ categoryId, sub }) => sub && id === categoryId)
.map(({ id, name, title }) => ({ id, name, title }));
return {
id,
name,
title,
subCategories,
};
});
res.status(200).json(response);
} catch (err) {
next(err);
}
},
};
40 changes: 28 additions & 12 deletions src/server/controller/subAchievementsController.js
@@ -1,23 +1,39 @@
const subAchievements = require('../model/subAchievements');

module.exports = {
async get(req, res) {
const [achievements] = await subAchievements.getById(req.pool, req.params.id);
res.send(achievements);
async get(req, res, next) {
try {
const [achievements] = await subAchievements.getById(req.pool, req.params.id);
res.status(200).json(achievements);
} catch (err) {
next(err);
}
},

async store(req, res) {
const [result] = await subAchievements.create(req.pool, req.body);
res.send(result);
async store(req, res, next) {
try {
const [result] = await subAchievements.create(req.pool, req.body);
res.send(result);
} catch (err) {
next(err);
}
},

async update(req, res) {
const [result] = await subAchievements.update(req.pool, req.params.id, req.body);
res.send(result);
async update(req, res, next) {
try {
const [result] = await subAchievements.update(req.pool, req.params.id, req.body);
res.send(result);
} catch (err) {
next(err);
}
},

async delete(req, res) {
const [result] = await subAchievements.delete(req.pool, req.params.id);
res.send(result);
async delete(req, res, next) {
try {
const [result] = await subAchievements.delete(req.pool, req.params.id);
res.send(result);
} catch (err) {
next(err);
}
},
};
26 changes: 12 additions & 14 deletions src/server/model/achievements.js
Expand Up @@ -46,18 +46,18 @@ module.exports = {
const { subCategoryId, title, description, completed, completedDate, review, images, isFailure } = data;
return await pool.execute(
`INSERT INTO achievements
(
sub_category_id
, title
, description
, completed
, completed_date
, review
, images
, is_failure
) VALUES
(
?, ?, ?, ?, ?, ?, ?, ?);`,
(
sub_category_id
, title
, description
, completed
, completed_date
, review
, images
, is_failure
) VALUES
(
?, ?, ?, ?, ?, ?, ?, ?);`,
[subCategoryId, title, description, completed, completedDate, review, images, isFailure],
);
},
Expand Down Expand Up @@ -87,11 +87,9 @@ module.exports = {

await conn.execute(`DELETE FROM sub_achievements WHERE achievements_id = ?`, [id]);
const result = await conn.execute(`DELETE FROM achievements WHERE id = ?`, [id]);

await conn.commit();
return result;
} catch (err) {
console.log(err);
await conn.rollback();
} finally {
if (conn) {
Expand Down

0 comments on commit 8f70c02

Please sign in to comment.