json 4xx and 5xx in api

merge-requests/208/head
fatchan 5 years ago
parent f92adeb610
commit 49afd3a414
  1. 33
      controllers/api.js

@ -36,11 +36,11 @@ router.get('/api/board/:board/recent/:page', Boards.exists, async (req, res, nex
try { try {
threads = await Posts.getRecent(req.params.board, req.params.page || 1); threads = await Posts.getRecent(req.params.board, req.params.page || 1);
} catch (err) { } catch (err) {
return next(err); return res.status(500).json({ 'message': 'Error fetching from DB' });
} }
if (!threads || threads.lenth === 0) { if (!threads || threads.lenth === 0) {
return next(); return res.status(404).json({ 'message': 'Not found' });
} }
return res.json(threads) return res.json(threads)
@ -55,11 +55,11 @@ router.get('/api/board/:board/thread/:thread([a-f\d]{24})', Boards.exists, async
try { try {
thread = await Posts.getThread(req.params.board, req.params.thread); thread = await Posts.getThread(req.params.board, req.params.thread);
} catch (err) { } catch (err) {
return next(err); return res.status(500).json({ 'message': 'Error fetching from DB' });
} }
if (!thread) { if (!thread) {
return next(); return res.status(404).json({ 'message': 'Not found' });
} }
return res.json(thread) return res.json(thread)
@ -74,24 +74,39 @@ router.get('/api/board/:board/catalog', Boards.exists, async (req, res, next) =>
try { try {
data = await Posts.getCatalog(req.params.board); data = await Posts.getCatalog(req.params.board);
} catch (err) { } catch (err) {
return next(err); return res.status(500).json({ 'message': 'Error fetching from DB' });
} }
if (!data) { if (!data) {
return next(); return res.status(404).json({ 'message': 'Not found' });
} }
return res.json(data) return res.json(data)
}); });
/* //get list of boards
router.get('/api/boards', Boards.exists, async (req, res, next) => {
//get a list of boards
let boards;
try {
boards = await Boards.find();
} catch (err) {
return res.status(500).json({ 'message': 'Error fetching from DB' })
}
//render the page
res.json(boards)
});
(async () => { (async () => {
await Boards.deleteAll(); await Boards.deleteAll();
await Boards.insertOne({ await Boards.insertOne({
_id: 'b', _id: 'b',
name: 'random', name: 'random',
title: 'random posts',
description: 'post anything here', description: 'post anything here',
}) })
await Posts.deleteAll('b'); await Posts.deleteAll('b');
@ -115,7 +130,7 @@ router.get('/api/board/:board/catalog', Boards.exists, async (req, res, next) =>
} }
} }
})(); })();
*/
module.exports = router; module.exports = router;

Loading…
Cancel
Save