Improve centralized error handling
This commit is contained in:
@@ -2,7 +2,6 @@ import * as crmEmailService from '../services/crm-email.service.js';
|
||||
import * as contactService from '../services/contact.service.js';
|
||||
import * as emailAccountService from '../services/email-account.service.js';
|
||||
import { markEmailAsRead, sendEmail, getJmapConfig, getJmapConfigFromAccount, syncEmailsFromSender, searchEmailsJMAP as searchEmailsJMAPService } from '../services/jmap.service.js';
|
||||
import { formatErrorResponse } from '../utils/errors.js';
|
||||
import { getUserById } from '../services/auth.service.js';
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
@@ -10,7 +9,7 @@ import { logger } from '../utils/logger.js';
|
||||
* Get all emails for authenticated user
|
||||
* GET /api/emails?accountId=xxx (REQUIRED)
|
||||
*/
|
||||
export const getEmails = async (req, res) => {
|
||||
export const getEmails = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { accountId } = req.query;
|
||||
@@ -36,8 +35,7 @@ export const getEmails = async (req, res) => {
|
||||
data: emails,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -45,7 +43,7 @@ export const getEmails = async (req, res) => {
|
||||
* Get emails by thread (conversation)
|
||||
* GET /api/emails/thread/:threadId?accountId=xxx (accountId required)
|
||||
*/
|
||||
export const getThread = async (req, res) => {
|
||||
export const getThread = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { threadId } = req.params;
|
||||
@@ -72,8 +70,7 @@ export const getThread = async (req, res) => {
|
||||
data: thread,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -81,7 +78,7 @@ export const getThread = async (req, res) => {
|
||||
* Search emails
|
||||
* GET /api/emails/search?q=query&accountId=xxx (accountId required)
|
||||
*/
|
||||
export const searchEmails = async (req, res) => {
|
||||
export const searchEmails = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { q, accountId } = req.query;
|
||||
@@ -107,8 +104,7 @@ export const searchEmails = async (req, res) => {
|
||||
data: results,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -117,7 +113,7 @@ export const searchEmails = async (req, res) => {
|
||||
* GET /api/emails/unread-count
|
||||
* Returns total unread count and per-account counts
|
||||
*/
|
||||
export const getUnreadCount = async (req, res) => {
|
||||
export const getUnreadCount = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
|
||||
@@ -139,8 +135,7 @@ export const getUnreadCount = async (req, res) => {
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('ERROR in getUnreadCount', { error: error.message });
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -149,7 +144,7 @@ export const getUnreadCount = async (req, res) => {
|
||||
* POST /api/emails/sync
|
||||
* Body: { accountId } (optional - defaults to primary account)
|
||||
*/
|
||||
export const syncEmails = async (req, res) => {
|
||||
export const syncEmails = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { accountId } = req.body;
|
||||
@@ -213,8 +208,7 @@ export const syncEmails = async (req, res) => {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -222,7 +216,7 @@ export const syncEmails = async (req, res) => {
|
||||
* Mark email as read/unread
|
||||
* PATCH /api/emails/:jmapId/read?accountId=xxx
|
||||
*/
|
||||
export const markAsRead = async (req, res) => {
|
||||
export const markAsRead = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { jmapId } = req.params;
|
||||
@@ -249,8 +243,7 @@ export const markAsRead = async (req, res) => {
|
||||
message: `Email označený ako ${isRead ? 'prečítaný' : 'neprečítaný'}`,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -258,7 +251,7 @@ export const markAsRead = async (req, res) => {
|
||||
* Mark all emails from contact as read
|
||||
* POST /api/emails/contact/:contactId/read?accountId=xxx
|
||||
*/
|
||||
export const markContactEmailsRead = async (req, res) => {
|
||||
export const markContactEmailsRead = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { contactId } = req.params;
|
||||
@@ -308,8 +301,7 @@ export const markContactEmailsRead = async (req, res) => {
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('ERROR in markContactEmailsRead', { error: error.message });
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -317,7 +309,7 @@ export const markContactEmailsRead = async (req, res) => {
|
||||
* Mark entire thread as read
|
||||
* POST /api/emails/thread/:threadId/read?accountId=xxx
|
||||
*/
|
||||
export const markThreadRead = async (req, res) => {
|
||||
export const markThreadRead = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { threadId } = req.params;
|
||||
@@ -361,8 +353,7 @@ export const markThreadRead = async (req, res) => {
|
||||
count,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -371,7 +362,7 @@ export const markThreadRead = async (req, res) => {
|
||||
* POST /api/emails/reply
|
||||
* Body: { to, subject, body, inReplyTo, threadId, accountId }
|
||||
*/
|
||||
export const replyToEmail = async (req, res) => {
|
||||
export const replyToEmail = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { to, subject, body, inReplyTo = null, threadId = null, accountId } = req.body;
|
||||
@@ -414,8 +405,7 @@ export const replyToEmail = 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -423,7 +413,7 @@ export const replyToEmail = async (req, res) => {
|
||||
* Get emails for a specific contact
|
||||
* GET /api/emails/contact/:contactId?accountId=xxx
|
||||
*/
|
||||
export const getContactEmails = async (req, res) => {
|
||||
export const getContactEmails = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { contactId } = req.params;
|
||||
@@ -450,8 +440,7 @@ export const getContactEmails = async (req, res) => {
|
||||
data: emails,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||
res.status(error.statusCode || 500).json(errorResponse);
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -460,7 +449,7 @@ export const getContactEmails = async (req, res) => {
|
||||
* GET /api/emails/search-jmap?query=text&limit=50&offset=0&accountId=xxx
|
||||
* Searches in: from, to, subject, and email body
|
||||
*/
|
||||
export const searchEmailsJMAP = async (req, res) => {
|
||||
export const searchEmailsJMAP = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const { query = '', limit = 50, offset = 0, accountId } = req.query;
|
||||
@@ -505,7 +494,6 @@ export const searchEmailsJMAP = async (req, res) => {
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('ERROR in searchEmailsJMAP', { error: error.message });
|
||||
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