-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from conceptadev/feature/improve-exception-ha…
…ndling feat: exceptions handling improvements
- Loading branch information
Showing
12 changed files
with
161 additions
and
24 deletions.
There are no files selected for viewing
24 changes: 10 additions & 14 deletions
24
packages/nestjs-exception/src/__fixtures__/exceptions/custom-not-found.exception.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { format } from 'util'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
import { ExceptionInterface } from '@concepta/ts-core'; | ||
|
||
export class CustomNotFoundExceptionFixture | ||
extends NotFoundException | ||
implements ExceptionInterface | ||
{ | ||
errorCode = 'CUSTOM_NOT_FOUND'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { RuntimeException } from '../../exceptions/runtime.exception'; | ||
|
||
export class CustomNotFoundExceptionFixture extends RuntimeException { | ||
constructor(itemId: number) { | ||
super( | ||
NotFoundException.createBody( | ||
format('Item with id %d was not found.', itemId), | ||
), | ||
); | ||
super({ | ||
message: 'Item with id %d was not found.', | ||
messageParams: [itemId], | ||
httpStatus: HttpStatus.NOT_FOUND, | ||
}); | ||
|
||
this.errorCode = 'CUSTOM_NOT_FOUND'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
57 changes: 57 additions & 0 deletions
57
packages/nestjs-exception/src/exceptions/runtime.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { format } from 'util'; | ||
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 | ||
{ | ||
private _errorCode = 'RUNTIME_EXCEPTION'; | ||
private _httpStatus: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR; | ||
private _safeMessage?: string; | ||
public context: RuntimeExceptionContext = {}; | ||
|
||
constructor( | ||
options: RuntimeExceptionOptions = { message: 'Runtime Exception' }, | ||
) { | ||
const { | ||
message = '', | ||
messageParams = [], | ||
safeMessage, | ||
safeMessageParams = [], | ||
originalError, | ||
httpStatus, | ||
} = options; | ||
|
||
super(format(message, ...messageParams)); | ||
|
||
if (httpStatus) { | ||
this._httpStatus = httpStatus; | ||
} | ||
|
||
if (safeMessage) { | ||
this._safeMessage = format(safeMessage, ...safeMessageParams); | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
// types | ||
export { RuntimeExceptionContext } from './exception.types'; | ||
|
||
// filters | ||
export { ExceptionsFilter } from './filters/exceptions.filter'; | ||
|
||
// interfaces | ||
export { RuntimeExceptionOptions } from './interfaces/runtime-exception-options.interface'; | ||
export { RuntimeExceptionInterface } from './interfaces/runtime-exception.interface'; | ||
|
||
// exceptions | ||
export { RuntimeException } from './exceptions/runtime.exception'; |
10 changes: 10 additions & 0 deletions
10
packages/nestjs-exception/src/interfaces/runtime-exception-options.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
export interface RuntimeExceptionOptions { | ||
httpStatus?: HttpStatus; | ||
message?: string; | ||
messageParams?: (string | number)[]; | ||
safeMessage?: string; | ||
safeMessageParams?: (string | number)[]; | ||
originalError?: unknown; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/nestjs-exception/src/interfaces/runtime-exception.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 { | ||
/** | ||
* Optional HTTP status code to use only when this exception is sent over an HTTP service. | ||
* | ||
* Please consider this to be a hint for API error responses. | ||
*/ | ||
httpStatus?: HttpStatus; | ||
|
||
/** | ||
* If set, this message will be used on responses instead of `message`. | ||
* | ||
* Use this when the main message might expose | ||
*/ | ||
safeMessage?: string; | ||
|
||
/** | ||
* Additional context | ||
*/ | ||
context: RuntimeExceptionContext; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ExceptionContext = Record<string, unknown> & { | ||
originalError?: unknown; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters