소스 검색

fix: fix several TS errors

Kristian Vos 1 년 전
부모
커밋
39cba6eb60
6개의 변경된 파일50개의 추가작업 그리고 20개의 파일을 삭제
  1. 6 1
      backend/src/BaseModule.ts
  2. 2 1
      backend/src/Job.ts
  3. 1 1
      backend/src/LogBook.ts
  4. 2 1
      backend/src/main.ts
  5. 30 16
      backend/src/modules/CacheModule.ts
  6. 9 0
      backend/src/utils/getErrorMessage.ts

+ 6 - 1
backend/src/BaseModule.ts

@@ -85,7 +85,12 @@ export default abstract class BaseModule {
 				)
 			);
 		} catch (error) {
-			if (error.code === "ENOENT") return;
+			if (
+				error instanceof Error &&
+				"code" in error &&
+				error.code === "ENOENT"
+			)
+				return;
 
 			throw error;
 		}

+ 2 - 1
backend/src/Job.ts

@@ -4,6 +4,7 @@ import LogBook, { Log } from "@/LogBook";
 import { JobOptions } from "@/types/JobOptions";
 import BaseModule from "./BaseModule";
 import EventsModule from "./modules/EventsModule";
+import { getErrorMessage } from "./utils/getErrorMessage";
 import { generateUuid } from "@/utils/generateUuid";
 
 export enum JobStatus {
@@ -213,7 +214,7 @@ export default abstract class Job {
 
 			return data;
 		} catch (error: unknown) {
-			const message = error?.message ?? error;
+			const message = getErrorMessage(error);
 
 			const socketId = this._context.getSocketId();
 			const callbackRef = this._context.getCallbackRef();

+ 1 - 1
backend/src/LogBook.ts

@@ -5,7 +5,7 @@ export type Log = {
 	message: string;
 	type?: "info" | "success" | "error" | "debug";
 	category?: string;
-	data?: Record<string, unknown>;
+	data?: Record<string, unknown> | Error;
 };
 
 export type LogFilters = {

+ 2 - 1
backend/src/main.ts

@@ -7,6 +7,7 @@ import JobStatistics from "@/JobStatistics";
 import DataModule from "@/modules/DataModule";
 import EventsModule from "./modules/EventsModule";
 import { NewsModel } from "./modules/DataModule/models/news/schema";
+import { FilterType } from "./modules/DataModule/plugins/getData";
 
 process.removeAllListeners("uncaughtException");
 process.on("uncaughtException", err => {
@@ -42,7 +43,7 @@ ModuleManager.startup().then(async () => {
 				{
 					data: "v7",
 					filter: { property: "title" },
-					filterType: "contains"
+					filterType: FilterType.CONTAINS
 				}
 			],
 			operator: "and"

+ 30 - 16
backend/src/modules/CacheModule.ts

@@ -1,10 +1,22 @@
 import config from "config";
-import { RedisClientType, createClient } from "redis";
+import {
+	RedisClientOptions,
+	RedisClientType,
+	RedisDefaultModules,
+	RedisFunctions,
+	RedisModules,
+	RedisScripts,
+	createClient
+} from "redis";
 import BaseModule, { ModuleStatus } from "@/BaseModule";
 import { forEachIn } from "@/utils/forEachIn";
 
 export class CacheModule extends BaseModule {
-	private _redisClient?: RedisClientType;
+	private _redisClient?: RedisClientType<
+		RedisDefaultModules & RedisModules,
+		RedisFunctions,
+		RedisScripts
+	>;
 
 	/**
 	 * Cache Module
@@ -20,23 +32,25 @@ export class CacheModule extends BaseModule {
 		await super.startup();
 
 		this._redisClient = createClient({
-			...config.get("redis"),
-			reconnectStrategy: (retries: number, error) => {
-				if (
-					retries >= 10 ||
-					![ModuleStatus.STARTING, ModuleStatus.STARTED].includes(
-						this.getStatus()
+			...config.get<RedisClientOptions>("redis"),
+			socket: {
+				reconnectStrategy: (retries: number, error) => {
+					if (
+						retries >= 10 ||
+						![ModuleStatus.STARTING, ModuleStatus.STARTED].includes(
+							this.getStatus()
+						)
 					)
-				)
-					return false;
+						return false;
 
-				this.log({
-					type: "debug",
-					message: `Redis reconnect attempt ${retries}`,
-					data: error
-				});
+					this.log({
+						type: "debug",
+						message: `Redis reconnect attempt ${retries}`,
+						data: error
+					});
 
-				return Math.min(retries * 50, 500);
+					return Math.min(retries * 50, 500);
+				}
 			}
 		});
 

+ 9 - 0
backend/src/utils/getErrorMessage.ts

@@ -0,0 +1,9 @@
+export const getErrorMessage = (
+	error: unknown,
+	defaultErrorMessage: string | null = null
+) => {
+	if (error instanceof Error) return error.message;
+	if (error) return String(error);
+	if (defaultErrorMessage) return defaultErrorMessage;
+	return "Unknown error";
+};