-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gap 2421 add logging in middleware (#92)
* logs incoming and outgoing request * add error filter to log errors * remove unused log * fix status in errorFIlter
- Loading branch information
Showing
4 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
Catch, | ||
ArgumentsHost, | ||
Logger, | ||
HttpException, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
|
||
@Catch(Error) | ||
export class ErrorFilter extends BaseExceptionFilter { | ||
catch(exception: Error, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse(); | ||
const stack = exception.stack; | ||
const message = exception.message; | ||
const request = ctx.getRequest<Request>(); | ||
const httpStatus = | ||
exception instanceof HttpException | ||
? exception.getStatus() | ||
: HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
Logger.error({ | ||
message: 'Caught Error', | ||
path: request.url, | ||
error: message, | ||
status: httpStatus, | ||
stack: stack, | ||
}); | ||
|
||
response.status(httpStatus).json({ | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
errorMessage: message, | ||
}); | ||
} | ||
} |
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,8 +1,10 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
import { ErrorFilter } from './filters/error.filter'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
app.useGlobalFilters(new ErrorFilter()); | ||
await app.listen(process.env.PORT || 3001); | ||
} | ||
bootstrap(); |
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