Skip to content

Commit

Permalink
feat: add some property protection
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMaz committed May 23, 2024
1 parent 8a7f85f commit c6cfad8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
5 changes: 5 additions & 0 deletions packages/nestjs-exception/src/exception.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExceptionContext } from '@concepta/ts-core';

export type RuntimeExceptionContext = ExceptionContext & {
originalError?: Error;
};
36 changes: 24 additions & 12 deletions packages/nestjs-exception/src/exceptions/runtime.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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;
}
}
3 changes: 3 additions & 0 deletions packages/nestjs-exception/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// types
export { RuntimeExceptionContext } from './exception.types';

// filters
export { ExceptionsFilter } from './filters/exceptions.filter';

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand All @@ -19,5 +20,5 @@ export interface RuntimeExceptionInterface extends ExceptionInterface {
/**
* Additional context
*/
context?: Record<string, unknown> & { originalError?: Error };
context: RuntimeExceptionContext;
}
3 changes: 3 additions & 0 deletions packages/ts-core/src/core.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type ExceptionContext = Record<string, unknown> & {
originalError?: unknown;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExceptionContext } from '../../core.types';

export interface ExceptionInterface extends Error {
/**
* The error code.
Expand All @@ -7,5 +9,5 @@ export interface ExceptionInterface extends Error {
/**
* Additional context
*/
context?: Record<string, unknown> & { originalError?: unknown };
context?: ExceptionContext;
}
2 changes: 2 additions & 0 deletions packages/ts-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { ExceptionContext } from './core.types';

export { ExceptionInterface } from './exceptions/interfaces/exception.interface';

export { Type } from './utils/interfaces/type.interface';
Expand Down

0 comments on commit c6cfad8

Please sign in to comment.