From c6cfad8aa20d5bf177c3aeb58cc00661d4457b80 Mon Sep 17 00:00:00 2001 From: Marshall Sorenson Date: Thu, 23 May 2024 13:16:43 -0400 Subject: [PATCH] feat: add some property protection --- .../custom-not-found.exception.fixture.ts | 6 ++-- .../nestjs-exception/src/exception.types.ts | 5 +++ .../src/exceptions/runtime.exception.ts | 36 ++++++++++++------- packages/nestjs-exception/src/index.ts | 3 ++ .../interfaces/runtime-exception.interface.ts | 3 +- packages/ts-core/src/core.types.ts | 3 ++ .../interfaces/exception.interface.ts | 4 ++- packages/ts-core/src/index.ts | 2 ++ 8 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 packages/nestjs-exception/src/exception.types.ts create mode 100644 packages/ts-core/src/core.types.ts diff --git a/packages/nestjs-exception/src/__fixtures__/exceptions/custom-not-found.exception.fixture.ts b/packages/nestjs-exception/src/__fixtures__/exceptions/custom-not-found.exception.fixture.ts index 0ad87c347..93c3f6a84 100644 --- a/packages/nestjs-exception/src/__fixtures__/exceptions/custom-not-found.exception.fixture.ts +++ b/packages/nestjs-exception/src/__fixtures__/exceptions/custom-not-found.exception.fixture.ts @@ -2,13 +2,13 @@ import { HttpStatus } from '@nestjs/common'; import { RuntimeException } from '../../exceptions/runtime.exception'; export class CustomNotFoundExceptionFixture extends RuntimeException { - httpStatus = HttpStatus.NOT_FOUND; - errorCode = 'CUSTOM_NOT_FOUND'; - constructor(itemId: number) { super({ message: 'Item with id %d was not found.', messageParams: [itemId], + httpStatus: HttpStatus.NOT_FOUND, }); + + this.errorCode = 'CUSTOM_NOT_FOUND'; } } diff --git a/packages/nestjs-exception/src/exception.types.ts b/packages/nestjs-exception/src/exception.types.ts new file mode 100644 index 000000000..74ace7955 --- /dev/null +++ b/packages/nestjs-exception/src/exception.types.ts @@ -0,0 +1,5 @@ +import { ExceptionContext } from '@concepta/ts-core'; + +export type RuntimeExceptionContext = ExceptionContext & { + originalError?: Error; +}; diff --git a/packages/nestjs-exception/src/exceptions/runtime.exception.ts b/packages/nestjs-exception/src/exceptions/runtime.exception.ts index 80fbcdbbb..3eec5f3c4 100644 --- a/packages/nestjs-exception/src/exceptions/runtime.exception.ts +++ b/packages/nestjs-exception/src/exceptions/runtime.exception.ts @@ -3,18 +3,16 @@ import { HttpStatus } from '@nestjs/common'; import { mapNonErrorToException } from '@concepta/ts-core'; import { RuntimeExceptionInterface } from '../interfaces/runtime-exception.interface'; import { RuntimeExceptionOptions } from '../interfaces/runtime-exception-options.interface'; +import { RuntimeExceptionContext } from '../exception.types'; export class RuntimeException extends Error implements RuntimeExceptionInterface { - errorCode = 'RUNTIME_EXCEPTION'; - httpStatus?: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR; - safeMessage?: string; - - context: { - originalError: Error; - }; + private _errorCode = 'RUNTIME_EXCEPTION'; + private _httpStatus: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + private _safeMessage?: string; + public context: RuntimeExceptionContext = {}; constructor( options: RuntimeExceptionOptions = { message: 'Runtime Exception' }, @@ -31,15 +29,29 @@ export class RuntimeException super(format(message, ...messageParams)); if (httpStatus) { - this.httpStatus = httpStatus; + this._httpStatus = httpStatus; } if (safeMessage) { - this.safeMessage = format(safeMessage, ...safeMessageParams); + this._safeMessage = format(safeMessage, ...safeMessageParams); } - this.context = { - originalError: mapNonErrorToException(originalError), - }; + this.context.originalError = mapNonErrorToException(originalError); + } + + public get errorCode() { + return this._errorCode; + } + + protected set errorCode(v: string) { + this._errorCode = v; + } + + public get httpStatus() { + return this._httpStatus; + } + + public get safeMessage() { + return this._safeMessage; } } diff --git a/packages/nestjs-exception/src/index.ts b/packages/nestjs-exception/src/index.ts index 5da099035..563b64ac4 100644 --- a/packages/nestjs-exception/src/index.ts +++ b/packages/nestjs-exception/src/index.ts @@ -1,3 +1,6 @@ +// types +export { RuntimeExceptionContext } from './exception.types'; + // filters export { ExceptionsFilter } from './filters/exceptions.filter'; diff --git a/packages/nestjs-exception/src/interfaces/runtime-exception.interface.ts b/packages/nestjs-exception/src/interfaces/runtime-exception.interface.ts index f253b1e56..e1e1df3b1 100644 --- a/packages/nestjs-exception/src/interfaces/runtime-exception.interface.ts +++ b/packages/nestjs-exception/src/interfaces/runtime-exception.interface.ts @@ -1,5 +1,6 @@ import { HttpStatus } from '@nestjs/common'; import { ExceptionInterface } from '@concepta/ts-core/src/exceptions/interfaces/exception.interface'; +import { RuntimeExceptionContext } from '../exception.types'; export interface RuntimeExceptionInterface extends ExceptionInterface { /** @@ -19,5 +20,5 @@ export interface RuntimeExceptionInterface extends ExceptionInterface { /** * Additional context */ - context?: Record & { originalError?: Error }; + context: RuntimeExceptionContext; } diff --git a/packages/ts-core/src/core.types.ts b/packages/ts-core/src/core.types.ts new file mode 100644 index 000000000..904ae2899 --- /dev/null +++ b/packages/ts-core/src/core.types.ts @@ -0,0 +1,3 @@ +export type ExceptionContext = Record & { + originalError?: unknown; +}; diff --git a/packages/ts-core/src/exceptions/interfaces/exception.interface.ts b/packages/ts-core/src/exceptions/interfaces/exception.interface.ts index 31f11eb22..441e2e82c 100644 --- a/packages/ts-core/src/exceptions/interfaces/exception.interface.ts +++ b/packages/ts-core/src/exceptions/interfaces/exception.interface.ts @@ -1,3 +1,5 @@ +import { ExceptionContext } from '../../core.types'; + export interface ExceptionInterface extends Error { /** * The error code. @@ -7,5 +9,5 @@ export interface ExceptionInterface extends Error { /** * Additional context */ - context?: Record & { originalError?: unknown }; + context?: ExceptionContext; } diff --git a/packages/ts-core/src/index.ts b/packages/ts-core/src/index.ts index 8558be0d0..21fa9817e 100644 --- a/packages/ts-core/src/index.ts +++ b/packages/ts-core/src/index.ts @@ -1,3 +1,5 @@ +export { ExceptionContext } from './core.types'; + export { ExceptionInterface } from './exceptions/interfaces/exception.interface'; export { Type } from './utils/interfaces/type.interface';