Improve centralized error handling
This commit is contained in:
@@ -2,13 +2,12 @@ import * as companyService from '../services/company.service.js';
|
||||
import * as noteService from '../services/note.service.js';
|
||||
import * as companyReminderService from '../services/company-reminder.service.js';
|
||||
import * as companyEmailService from '../services/company-email.service.js';
|
||||
import { formatErrorResponse } from '../utils/errors.js';
|
||||
|
||||
/**
|
||||
* Get all companies
|
||||
* GET /api/companies?search=query
|
||||
*/
|
||||
export const getAllCompanies = async (req, res) => {
|
||||
export const getAllCompanies = async (req, res, next) => {
|
||||
try {
|
||||
const { search } = req.query;
|
||||
|
||||
@@ -20,8 +19,7 @@ export const getAllCompanies = async (req, res) => {
|
||||
data: companies,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,7 +27,7 @@ export const getAllCompanies = async (req, res) => {
|
||||
* Get company by ID
|
||||
* GET /api/companies/:companyId
|
||||
*/
|
||||
export const getCompanyById = async (req, res) => {
|
||||
export const getCompanyById = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
|
||||
@@ -40,8 +38,7 @@ export const getCompanyById = async (req, res) => {
|
||||
data: company,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -49,7 +46,7 @@ export const getCompanyById = async (req, res) => {
|
||||
* Get company email threads aggregated across user's email accounts
|
||||
* GET /api/companies/:companyId/email-threads
|
||||
*/
|
||||
export const getCompanyEmailThreads = async (req, res) => {
|
||||
export const getCompanyEmailThreads = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { companyId } = req.params;
|
||||
@@ -64,8 +61,7 @@ export const getCompanyEmailThreads = async (req, res) => {
|
||||
data: result,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -73,7 +69,7 @@ export const getCompanyEmailThreads = async (req, res) => {
|
||||
* Get unread email counts grouped by company for current user
|
||||
* GET /api/companies/email-unread
|
||||
*/
|
||||
export const getCompanyUnreadCounts = async (req, res) => {
|
||||
export const getCompanyUnreadCounts = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const counts = await companyEmailService.getCompanyUnreadCounts(userId);
|
||||
@@ -83,8 +79,7 @@ export const getCompanyUnreadCounts = async (req, res) => {
|
||||
data: counts,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -92,7 +87,7 @@ export const getCompanyUnreadCounts = async (req, res) => {
|
||||
* Get company with relations (projects, todos, notes)
|
||||
* GET /api/companies/:companyId/details
|
||||
*/
|
||||
export const getCompanyWithRelations = async (req, res) => {
|
||||
export const getCompanyWithRelations = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
|
||||
@@ -103,8 +98,7 @@ export const getCompanyWithRelations = async (req, res) => {
|
||||
data: company,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,7 +107,7 @@ export const getCompanyWithRelations = async (req, res) => {
|
||||
* POST /api/companies
|
||||
* Body: { name, description, address, city, country, phone, email, website }
|
||||
*/
|
||||
export const createCompany = async (req, res) => {
|
||||
export const createCompany = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const data = req.body;
|
||||
@@ -126,8 +120,7 @@ export const createCompany = async (req, res) => {
|
||||
message: 'Firma bola vytvorená',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -136,7 +129,7 @@ export const createCompany = async (req, res) => {
|
||||
* PATCH /api/companies/:companyId
|
||||
* Body: { name, description, address, city, country, phone, email, website }
|
||||
*/
|
||||
export const updateCompany = async (req, res) => {
|
||||
export const updateCompany = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
const data = req.body;
|
||||
@@ -149,8 +142,7 @@ export const updateCompany = async (req, res) => {
|
||||
message: 'Firma bola aktualizovaná',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -158,7 +150,7 @@ export const updateCompany = async (req, res) => {
|
||||
* Delete company
|
||||
* DELETE /api/companies/:companyId
|
||||
*/
|
||||
export const deleteCompany = async (req, res) => {
|
||||
export const deleteCompany = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
|
||||
@@ -169,8 +161,7 @@ export const deleteCompany = 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,7 +169,7 @@ export const deleteCompany = async (req, res) => {
|
||||
* Get company notes
|
||||
* GET /api/companies/:companyId/notes
|
||||
*/
|
||||
export const getCompanyNotes = async (req, res) => {
|
||||
export const getCompanyNotes = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
|
||||
@@ -190,8 +181,7 @@ export const getCompanyNotes = 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -199,7 +189,7 @@ export const getCompanyNotes = async (req, res) => {
|
||||
* Add company note
|
||||
* POST /api/companies/:companyId/notes
|
||||
*/
|
||||
export const addCompanyNote = async (req, res) => {
|
||||
export const addCompanyNote = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { companyId } = req.params;
|
||||
@@ -216,8 +206,7 @@ export const addCompanyNote = async (req, res) => {
|
||||
message: 'Poznámka bola pridaná',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -225,7 +214,7 @@ export const addCompanyNote = async (req, res) => {
|
||||
* Update company note
|
||||
* PATCH /api/companies/:companyId/notes/:noteId
|
||||
*/
|
||||
export const updateCompanyNote = async (req, res) => {
|
||||
export const updateCompanyNote = async (req, res, next) => {
|
||||
try {
|
||||
const { noteId } = req.params;
|
||||
const { content } = req.body;
|
||||
@@ -240,8 +229,7 @@ export const updateCompanyNote = 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -249,7 +237,7 @@ export const updateCompanyNote = async (req, res) => {
|
||||
* Delete company note
|
||||
* DELETE /api/companies/:companyId/notes/:noteId
|
||||
*/
|
||||
export const deleteCompanyNote = async (req, res) => {
|
||||
export const deleteCompanyNote = async (req, res, next) => {
|
||||
try {
|
||||
const { noteId } = req.params;
|
||||
|
||||
@@ -260,8 +248,7 @@ export const deleteCompanyNote = 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -269,7 +256,7 @@ export const deleteCompanyNote = async (req, res) => {
|
||||
* Company reminders
|
||||
* CRUD for /api/companies/:companyId/reminders
|
||||
*/
|
||||
export const getCompanyReminders = async (req, res) => {
|
||||
export const getCompanyReminders = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
|
||||
@@ -281,12 +268,11 @@ export const getCompanyReminders = 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);
|
||||
}
|
||||
};
|
||||
|
||||
export const createCompanyReminder = async (req, res) => {
|
||||
export const createCompanyReminder = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId } = req.params;
|
||||
const { description, dueDate, isChecked } = req.body;
|
||||
@@ -299,12 +285,11 @@ export const createCompanyReminder = async (req, res) => {
|
||||
message: 'Reminder bol pridaný',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const updateCompanyReminder = async (req, res) => {
|
||||
export const updateCompanyReminder = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId, reminderId } = req.params;
|
||||
const { description, dueDate, isChecked } = req.body;
|
||||
@@ -317,12 +302,11 @@ export const updateCompanyReminder = async (req, res) => {
|
||||
message: 'Reminder bol aktualizovaný',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteCompanyReminder = async (req, res) => {
|
||||
export const deleteCompanyReminder = async (req, res, next) => {
|
||||
try {
|
||||
const { companyId, reminderId } = req.params;
|
||||
|
||||
@@ -333,12 +317,11 @@ export const deleteCompanyReminder = 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);
|
||||
}
|
||||
};
|
||||
|
||||
export const getReminderSummary = async (_req, res) => {
|
||||
export const getReminderSummary = async (_req, res, next) => {
|
||||
try {
|
||||
const summary = await companyReminderService.getReminderSummary();
|
||||
res.status(200).json({
|
||||
@@ -346,12 +329,11 @@ export const getReminderSummary = async (_req, res) => {
|
||||
data: summary,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getReminderCountsByCompany = async (_req, res) => {
|
||||
export const getReminderCountsByCompany = async (_req, res, next) => {
|
||||
try {
|
||||
const counts = await companyReminderService.getReminderCountsByCompany();
|
||||
res.status(200).json({
|
||||
@@ -359,12 +341,11 @@ export const getReminderCountsByCompany = async (_req, res) => {
|
||||
data: counts,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getUpcomingReminders = async (_req, res) => {
|
||||
export const getUpcomingReminders = async (_req, res, next) => {
|
||||
try {
|
||||
const reminders = await companyReminderService.getUpcomingReminders();
|
||||
res.status(200).json({
|
||||
@@ -373,7 +354,6 @@ export const getUpcomingReminders = 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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user