Improve centralized error handling
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import * as todoService from '../services/todo.service.js';
|
||||
import { formatErrorResponse } from '../utils/errors.js';
|
||||
|
||||
/**
|
||||
* Get all todos
|
||||
* GET /api/todos?search=query&projectId=xxx&companyId=xxx&assignedTo=xxx&status=xxx
|
||||
*/
|
||||
export const getAllTodos = async (req, res) => {
|
||||
export const getAllTodos = async (req, res, next) => {
|
||||
try {
|
||||
const { search, projectId, companyId, assignedTo, status, completed, priority } = req.query;
|
||||
|
||||
@@ -32,8 +31,7 @@ export const getAllTodos = async (req, res) => {
|
||||
data: todos,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -41,7 +39,7 @@ export const getAllTodos = async (req, res) => {
|
||||
* Get my todos (assigned to current user)
|
||||
* GET /api/todos/my?status=xxx
|
||||
*/
|
||||
export const getMyTodos = async (req, res) => {
|
||||
export const getMyTodos = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { status } = req.query;
|
||||
@@ -59,8 +57,7 @@ export const getMyTodos = async (req, res) => {
|
||||
data: todos,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -68,7 +65,7 @@ export const getMyTodos = async (req, res) => {
|
||||
* Get todo by ID
|
||||
* GET /api/todos/:todoId
|
||||
*/
|
||||
export const getTodoById = async (req, res) => {
|
||||
export const getTodoById = async (req, res, next) => {
|
||||
try {
|
||||
const { todoId } = req.params;
|
||||
|
||||
@@ -79,8 +76,7 @@ export const getTodoById = async (req, res) => {
|
||||
data: todo,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -88,7 +84,7 @@ export const getTodoById = async (req, res) => {
|
||||
* Get todo with relations (project, company, assigned user, notes)
|
||||
* GET /api/todos/:todoId/details
|
||||
*/
|
||||
export const getTodoWithRelations = async (req, res) => {
|
||||
export const getTodoWithRelations = async (req, res, next) => {
|
||||
try {
|
||||
const { todoId } = req.params;
|
||||
|
||||
@@ -99,8 +95,7 @@ export const getTodoWithRelations = async (req, res) => {
|
||||
data: todo,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -109,7 +104,7 @@ export const getTodoWithRelations = async (req, res) => {
|
||||
* POST /api/todos
|
||||
* Body: { title, description, projectId, companyId, assignedUserIds, status, priority, dueDate }
|
||||
*/
|
||||
export const createTodo = async (req, res) => {
|
||||
export const createTodo = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const data = req.body;
|
||||
@@ -123,8 +118,7 @@ export const createTodo = async (req, res) => {
|
||||
message: 'Todo bolo vytvorené',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -133,7 +127,7 @@ export const createTodo = async (req, res) => {
|
||||
* PATCH /api/todos/:todoId
|
||||
* Body: { title, description, projectId, companyId, assignedUserIds, status, priority, dueDate }
|
||||
*/
|
||||
export const updateTodo = async (req, res) => {
|
||||
export const updateTodo = async (req, res, next) => {
|
||||
try {
|
||||
const { todoId } = req.params;
|
||||
const data = req.body;
|
||||
@@ -147,8 +141,7 @@ export const updateTodo = async (req, res) => {
|
||||
message: 'Todo bolo aktualizované',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -156,7 +149,7 @@ export const updateTodo = async (req, res) => {
|
||||
* Delete todo
|
||||
* DELETE /api/todos/:todoId
|
||||
*/
|
||||
export const deleteTodo = async (req, res) => {
|
||||
export const deleteTodo = async (req, res, next) => {
|
||||
try {
|
||||
const { todoId } = req.params;
|
||||
|
||||
@@ -167,8 +160,7 @@ export const deleteTodo = async (req, res) => {
|
||||
message: result.message,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -176,7 +168,7 @@ export const deleteTodo = async (req, res) => {
|
||||
* Toggle todo completion status
|
||||
* PATCH /api/todos/:todoId/toggle
|
||||
*/
|
||||
export const toggleTodo = async (req, res) => {
|
||||
export const toggleTodo = async (req, res, next) => {
|
||||
try {
|
||||
const { todoId } = req.params;
|
||||
|
||||
@@ -194,7 +186,6 @@ export const toggleTodo = async (req, res) => {
|
||||
message: 'Todo status aktualizovaný',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user