Improve centralized error handling

This commit is contained in:
richardtekula
2025-12-04 07:39:52 +01:00
parent 109cae1167
commit 35dfa07668
14 changed files with 266 additions and 336 deletions

View File

@@ -1,11 +1,10 @@
import * as noteService from '../services/note.service.js';
import { formatErrorResponse } from '../utils/errors.js';
/**
* Get all notes
* GET /api/notes?search=query&companyId=xxx&projectId=xxx&todoId=xxx&contactId=xxx
*/
export const getAllNotes = async (req, res) => {
export const getAllNotes = async (req, res, next) => {
try {
const { search, companyId, projectId, todoId, contactId } = req.query;
@@ -25,8 +24,7 @@ export const getAllNotes = async (req, res) => {
data: notes,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};
@@ -34,7 +32,7 @@ export const getAllNotes = async (req, res) => {
* Get note by ID
* GET /api/notes/:noteId
*/
export const getNoteById = async (req, res) => {
export const getNoteById = async (req, res, next) => {
try {
const { noteId } = req.params;
@@ -45,8 +43,7 @@ export const getNoteById = async (req, res) => {
data: note,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};
@@ -55,7 +52,7 @@ export const getNoteById = async (req, res) => {
* POST /api/notes
* Body: { title, content, companyId, projectId, todoId, contactId }
*/
export const createNote = async (req, res) => {
export const createNote = async (req, res, next) => {
try {
const userId = req.userId;
const data = req.body;
@@ -68,8 +65,7 @@ export const createNote = async (req, res) => {
message: 'Poznámka bola vytvorená',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};
@@ -78,7 +74,7 @@ export const createNote = async (req, res) => {
* PATCH /api/notes/:noteId
* Body: { title, content, companyId, projectId, todoId, contactId }
*/
export const updateNote = async (req, res) => {
export const updateNote = async (req, res, next) => {
try {
const { noteId } = req.params;
const data = req.body;
@@ -91,8 +87,7 @@ export const updateNote = async (req, res) => {
message: 'Poznámka bola aktualizovaná',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};
@@ -100,7 +95,7 @@ export const updateNote = async (req, res) => {
* Delete note
* DELETE /api/notes/:noteId
*/
export const deleteNote = async (req, res) => {
export const deleteNote = async (req, res, next) => {
try {
const { noteId } = req.params;
@@ -111,8 +106,7 @@ export const deleteNote = 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);
}
};
@@ -120,7 +114,7 @@ export const deleteNote = async (req, res) => {
* Get upcoming reminders for current user
* GET /api/notes/my-reminders
*/
export const getMyReminders = async (req, res) => {
export const getMyReminders = async (req, res, next) => {
try {
const userId = req.userId;
@@ -132,8 +126,7 @@ export const getMyReminders = async (req, res) => {
data: reminders,
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};
@@ -141,7 +134,7 @@ export const getMyReminders = async (req, res) => {
* Mark reminder as sent
* POST /api/notes/:noteId/mark-reminder-sent
*/
export const markReminderSent = async (req, res) => {
export const markReminderSent = async (req, res, next) => {
try {
const { noteId } = req.params;
@@ -153,7 +146,6 @@ export const markReminderSent = async (req, res) => {
message: 'Reminder označený ako odoslaný',
});
} catch (error) {
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
res.status(error.statusCode || 500).json(errorResponse);
return next(error);
}
};