From 4d8cabadf75386770e9d3edc665441c205d3d687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20D=C3=A9moulins?= Date: Sun, 31 Oct 2021 23:02:29 +0100 Subject: [PATCH] Remove usage of `any` --- src/middlewares/error.spec.ts | 2 +- src/middlewares/error.ts | 9 +++++---- src/utils.spec.ts | 23 ++++++++++++++++++++++- src/utils.ts | 12 ++++++++---- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/middlewares/error.spec.ts b/src/middlewares/error.spec.ts index e30e85ca..8a244f42 100644 --- a/src/middlewares/error.spec.ts +++ b/src/middlewares/error.spec.ts @@ -43,7 +43,7 @@ describe("ErrorMiddleware", () => { // act handler(fakeError, fakeReq, fakeRes, fakeNext) // assert - const expectedMessage = `Cannot ${fakeMethod} ${fakeUrl}` + const expectedMessage = `${fakeMethod} ${fakeUrl}` resMock .verify((e) => e.status(500), Times.Once()) .verify((e) => e.send(`${expectedMessage}: ${fakeError.message}`), Times.Once()) diff --git a/src/middlewares/error.ts b/src/middlewares/error.ts index fc6acae8..5a32a4fc 100644 --- a/src/middlewares/error.ts +++ b/src/middlewares/error.ts @@ -1,15 +1,16 @@ import { NextFunction, Request, Response } from "express" import { ILogger } from "../logger" +import { hasMember } from "../utils" export function getErrorHandler(logger: ILogger) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars - return (err: any, req: Request, res: Response, _next: NextFunction) => { - const message = `Cannot ${req.method} ${req.originalUrl}` + // eslint-disable-next-line @typescript-eslint/no-unused-vars + return (err: unknown, req: Request, res: Response, _next: NextFunction) => { + const message = `${req.method} ${req.originalUrl}` logger.error(`${message}:`, err) logger.debug("Headers", req.headers) if (Object.keys(req.params).length) { logger.debug("Body", req.params) } - res.status(500).send(`${message}: ${err.message ?? err}`) + res.status(500).send(`${message}: ${hasMember(err, "message") ? err.message : err}`) } } diff --git a/src/utils.spec.ts b/src/utils.spec.ts index c5905622..2fe8b15a 100644 --- a/src/utils.spec.ts +++ b/src/utils.spec.ts @@ -1,4 +1,4 @@ -import { getFirstSupportedOrDefault } from "./utils" +import { getFirstSupportedOrDefault, hasMember } from "./utils" describe("getFirstSupportedOrDefault", () => { abstract class Base { @@ -42,3 +42,24 @@ describe("getFirstSupportedOrDefault", () => { expect(result).toEqual(new B()) }) }) + +describe("hasMember", () => { + const expectedValue = "value" + const sut = { a: expectedValue } as unknown + + it("returns false if the object doesn't have a specific member", () => { + // act + const result = hasMember(sut, "b") + // assert + expect(result).toBeFalsy() + expect(result ? sut.b : expectedValue).toEqual(expectedValue) + }) + + it("returns true if the object has a specific member", () => { + // act + const result = hasMember(sut, "a") + // assert + expect(result).toBeTruthy() + expect(result ? sut.a : undefined).toEqual(expectedValue) + }) +}) diff --git a/src/utils.ts b/src/utils.ts index d81ffd01..84be222a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,8 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type Constructor = new (...args: TArgs) => T + +export type NewReturnType = T extends (...args: infer TArgs) => unknown ? (...args: TArgs) => TNew : never + export function getFirstSupportedOrDefault>(values: T[], types: C[]) { for (const type of types) { for (const value of values) { @@ -6,7 +11,6 @@ export function getFirstSupportedOrDefault>(values: } } -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type Constructor = new (...args: TArgs) => T - -export type NewReturnType = T extends (...args: infer TArgs) => unknown ? (...args: TArgs) => TNew : never +export function hasMember(value: unknown, member: T): value is { [member in T]: unknown } { + return typeof value === "object" && value !== null && member in value +}