add an error service, base controller

This commit is contained in:
Alex Holliday committed 2025-07-24 15:06:46 -07:00
1 parent 0cfe9d60fb
commit ddf3e8251d
20 files changed
+412 -301

No files matched your search

+8 -10
View File
@@ -1,12 +1,10 @@
import { createMonitorsBodyValidation } from "../../validation/joi.js";
import { createServerError } from "../../utils/errorUtils.js";
import logger from "../../utils/logger.js";
const SERVICE_NAME = "MonitorService";
class MonitorService {
SERVICE_NAME = SERVICE_NAME;
constructor({ db, settingsService, jobQueue, stringService, emailService, papaparse }) {
constructor({ db, settingsService, jobQueue, stringService, emailService, papaparse, logger, errorService }) {
this.db = db;
this.settingsService = settingsService;
this.jobQueue = jobQueue;
@@ -14,14 +12,14 @@ class MonitorService {
this.emailService = emailService;
this.papaparse = papaparse;
this.logger = logger;
this.errorService = errorService;
this.asyncHandler = errorService.asyncHandler;
}
verifyTeamAccess = async ({ teamId, monitorId }) => {
const monitor = await this.db.getMonitorById(monitorId);
if (!monitor?.teamId?.equals(teamId)) {
const error = new Error("Unauthorized");
error.status = 403;
throw error;
throw this.errorService.createAuthorizationError();
}
};
@@ -103,11 +101,11 @@ class MonitorService {
complete: async ({ data, errors }) => {
try {
if (errors.length > 0) {
throw createServerError("Error parsing CSV");
throw this.errorService.createServerError("Error parsing CSV");
}
if (!data || data.length === 0) {
throw createServerError("CSV file contains no data rows");
throw this.errorService.createServerError("CSV file contains no data rows");
}
const enrichedData = data.map((monitor) => ({
@@ -156,7 +154,7 @@ class MonitorService {
await this.db.deletePageSpeedChecksByMonitorId(monitor._id);
await this.db.deleteNotificationsByMonitorId(monitor._id);
} catch (error) {
logger.warn({
this.logger.warn({
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
service: SERVICE_NAME,
method: "deleteAllMonitors",
@@ -195,7 +193,7 @@ class MonitorService {
const messageId = await this.emailService.sendEmail(to, subject, html);
if (!messageId) {
throw createServerError("Failed to send test email.");
throw this.errorService.createServerError("Failed to send test email.");
}
return messageId;