feat: Error handling middleware
feat: Ability to get the db variable from everywhere with getDatabase function feat: Hello world when getting root url fix: Now mods model is able to communicate with db fix: fixed a syntax error in routes/mods.js file which caused sending two responses to the client
This commit is contained in:
parent
eb4be67c89
commit
306c87fca2
10 changed files with 315 additions and 19 deletions
|
|
@ -5,10 +5,11 @@ async function getAllMods(req, res) {
|
|||
console.debug("Calling controller");
|
||||
const query_result = await mod_service.getAllMods();
|
||||
console.debug("Controller OK");
|
||||
return res.json(query_result);
|
||||
res.json(query_result);
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: error.message });
|
||||
console.debug("Error at controller");
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {getAllMods};
|
||||
module.exports = { getAllMods };
|
||||
|
|
@ -1,15 +1,24 @@
|
|||
const MySQLDatabase = require("./mysql");
|
||||
const SQLiteDatabase = require("./sqlite");
|
||||
|
||||
function getDatabase(config) {
|
||||
let db;
|
||||
|
||||
async function connectDatabase(config) {
|
||||
if (config.type === "mysql") {
|
||||
return new MySQLDatabase(config);
|
||||
db = new MySQLDatabase(config);
|
||||
} else if (config.type === "sqlite") {
|
||||
return new SQLiteDatabase(config);
|
||||
db = new SQLiteDatabase(config);
|
||||
} else {
|
||||
throw new Error("Invalid database type: ", config.type);
|
||||
}
|
||||
|
||||
await db.connect();
|
||||
return db;
|
||||
}
|
||||
|
||||
function getDatabase() {
|
||||
return db;
|
||||
}
|
||||
|
||||
|
||||
module.exports = { getDatabase };
|
||||
module.exports = { getDatabase, connectDatabase };
|
||||
19
src/middleware/errors.js
Normal file
19
src/middleware/errors.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
const AppError = require("../utils/AppError");
|
||||
|
||||
const handleError = (err, req, res, next) => {
|
||||
// Send
|
||||
if (err instanceof AppError) {
|
||||
return res.status(err.statusCode).json({
|
||||
message: err.message,
|
||||
status: err.status
|
||||
});
|
||||
}
|
||||
|
||||
// Default error
|
||||
res.status(500).json({
|
||||
message: 'Internal server error',
|
||||
status: 500
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = handleError;
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
// const db = require("...");
|
||||
const { getDatabase } = require('../database/index');
|
||||
const db = getDatabase();
|
||||
|
||||
async function getAllMods() {
|
||||
console.debug("Calling model");
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ const router = express.Router();
|
|||
// List mods
|
||||
router.get("/", async (req,res) => {
|
||||
console.debug("Accessing mods list");
|
||||
res.send(controller.getAllMods());
|
||||
controller.getAllMods(req,res);
|
||||
// res.send("No list yet");
|
||||
});
|
||||
|
||||
|
|
|
|||
10
src/routes/root.js
Normal file
10
src/routes/root.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
const express = require("express");
|
||||
const controller = require("../controllers/mods");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (res, req) => {
|
||||
res.send("Hello there!");
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
22
src/utils/AppError.js
Normal file
22
src/utils/AppError.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class AppError extends Error {
|
||||
constructor(statusCode, message) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
|
||||
if (status_code.ToString().startsWith("4")) {
|
||||
this.status = "fail";
|
||||
} else {
|
||||
this.status = "error";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.tryCatch = (controller) => async (req, res, next) => {
|
||||
try {
|
||||
await controller(req, res, next);
|
||||
} catch(err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AppError;
|
||||
Loading…
Add table
Add a link
Reference in a new issue