-
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.
feat: exceptions handling improvements
- Loading branch information
Showing
9 changed files
with
131 additions
and
22 deletions.
There are no files selected for viewing
20 changes: 8 additions & 12 deletions
20
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'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { RuntimeException } from '../../exceptions/runtime.exception'; | ||
|
||
export class CustomNotFoundExceptionFixture | ||
extends NotFoundException | ||
implements ExceptionInterface | ||
{ | ||
export class CustomNotFoundExceptionFixture extends RuntimeException { | ||
httpStatus = HttpStatus.NOT_FOUND; | ||
errorCode = 'CUSTOM_NOT_FOUND'; | ||
|
||
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], | ||
}); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
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'; | ||
|
||
export class RuntimeException | ||
extends Error | ||
implements RuntimeExceptionInterface | ||
{ | ||
errorCode = 'RUNTIME_EXCEPTION'; | ||
httpStatus?: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR; | ||
safeMessage?: string; | ||
|
||
context: { | ||
originalError: Error; | ||
}; | ||
|
||
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), | ||
}; | ||
} | ||
} |
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,9 @@ | ||
// 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; | ||
} |
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { ExceptionInterface } from '@concepta/ts-core/src/exceptions/interfaces/exception.interface'; | ||
|
||
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?: Record<string, unknown> & { originalError?: Error }; | ||
} |
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