type handleErrors
This commit is contained in:
1 parent
501664509b
commit
9646e7dbdc
5 files changed
+21
-18
No files matched your search
@@ -37,6 +37,7 @@ export default [
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }], // Ignore unused variables that start with '_'
|
||||
"@typescript-eslint/no-explicit-any": "warn", // Warn on 'any' types
|
||||
"mocha/max-top-level-suites": "warn", // Warn if there are too many top-level suites instead of failing
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Schema, model, type Types } from "mongoose";
|
||||
import type { MaintenanceWindow, DurationUnit } from "@/types/maintenanceWindow.js";
|
||||
import type { MaintenanceWindow } from "@/types/maintenanceWindow.js";
|
||||
|
||||
type MaintenanceWindowDocumentBase = Omit<MaintenanceWindow, "id" | "monitorId" | "teamId" | "start" | "end" | "createdAt" | "updatedAt"> & {
|
||||
monitorId: Types.ObjectId;
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
import { logger } from "@/utils/logger.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
|
||||
const handleErrors = (error: any, req: Request, res: Response, next: NextFunction) => {
|
||||
const status = error.status || 500;
|
||||
const message = error.message || "Server error";
|
||||
const service = error.service || "unknownService";
|
||||
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
||||
const status = error instanceof AppError ? error.status || 500 : 500;
|
||||
const message = error instanceof AppError ? error.message : "Server error";
|
||||
const service = error instanceof AppError ? error.service : "unknownService";
|
||||
const method = error instanceof AppError ? error.method : "unknownMethod";
|
||||
logger.error({
|
||||
message: message,
|
||||
service: service,
|
||||
method: error.method,
|
||||
stack: error.stack,
|
||||
method: method,
|
||||
stack: error instanceof AppError ? error.stack : undefined,
|
||||
details: error instanceof AppError ? error.details : undefined,
|
||||
});
|
||||
res.status(status).json({
|
||||
status,
|
||||
|
||||
@@ -360,8 +360,7 @@ export class StatusService implements IStatusService {
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
throw new AppError({
|
||||
message: `Failed to update monitor status: ${error instanceof Error ? error.message : "Unknown error"}`,
|
||||
details: `Error updating status for monitor with id ${check.metadata.monitorId}`,
|
||||
message: `Failed to update monitor with id ${check.metadata.monitorId} with status: ${error instanceof Error ? error.message : "Unknown error"}`,
|
||||
service: SERVICE_NAME,
|
||||
method: "updateMonitorStatus",
|
||||
});
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
export interface AppErrorConfig {
|
||||
message: string;
|
||||
message?: string;
|
||||
status?: number;
|
||||
service?: string | null;
|
||||
method?: string | null;
|
||||
details?: unknown;
|
||||
service?: string;
|
||||
method?: string;
|
||||
details?: Record<string, unknown> | undefined;
|
||||
}
|
||||
|
||||
export class AppError extends Error {
|
||||
private status: number;
|
||||
private service: string | null;
|
||||
private method: string | null;
|
||||
private details: unknown;
|
||||
readonly status: number;
|
||||
readonly service: string;
|
||||
readonly method: string;
|
||||
readonly details: Record<string, unknown> | undefined;
|
||||
|
||||
constructor({ message, status = 500, service = null, method = null, details = null }: AppErrorConfig) {
|
||||
constructor({ message, status = 500, service = "unknownService", method = "unknownMethod", details = undefined }: AppErrorConfig) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.service = service;
|
||||
|
||||
Reference in new issue
Block a user