-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create new service for logging metadata from filter
The new `OgmaFilterLogger` an be injected into a service and take in an exception and an arguments host object from Nest and print out metadata according to how the request failed. This will **only** happen, however, if the request has not been logged by an interceptor, meaning there will not be double logging of requests which would make logs noiser. This is done by checking for the existence of the `requestId` added by the interceptor earlier in the call, and the major gain here is that now errors caused by guards or middleware can be caught can logged just as other requests are properly automatically logged as well, making Ogma now fully capable of logging **any** request type into Nest! This feature is opt-in, just as the interceptor is, but the power it brings is major due to now not leaving any requests unlogged. The only downside at the moment is not being able to determing the time length of the request as most underlying transports don't grab the metadata of the start time, but there will be mentions of how users can implement such a feature to allow for that if they so choose.
- Loading branch information
Showing
38 changed files
with
336 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter as NestExceptionFilter, | ||
HttpException, | ||
} from '@nestjs/common'; | ||
import { OgmaFilterLogger } from '@ogma/nestjs-module'; | ||
|
||
@Catch() | ||
export class ExceptionFilter implements NestExceptionFilter { | ||
constructor(private readonly service: OgmaFilterLogger) {} | ||
catch(exception: HttpException, host: ArgumentsHost) { | ||
this.service.log(exception, host); | ||
return exception; | ||
} | ||
} |
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
Empty file.
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,15 @@ | ||
import { ArgumentsHost, Catch } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { OgmaFilterLogger } from '@ogma/nestjs-module'; | ||
|
||
@Catch() | ||
export class ErrorLoggingFilter extends BaseExceptionFilter { | ||
constructor(private readonly logger: OgmaFilterLogger) { | ||
super(); | ||
} | ||
|
||
catch(exception: Error, host: ArgumentsHost) { | ||
this.logger.log(exception, host); | ||
return super.catch(exception, host); | ||
} | ||
} |
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
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,8 @@ | ||
import { CanActivate, Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class FailGuard implements CanActivate { | ||
canActivate() { | ||
return false; | ||
} | ||
} |
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,10 +1,15 @@ | ||
import { Catch, HttpException } from '@nestjs/common'; | ||
import { ArgumentsHost, Catch, HttpException, Optional } from '@nestjs/common'; | ||
import { BaseRpcExceptionFilter } from '@nestjs/microservices'; | ||
import { OgmaFilterLogger } from '@ogma/nestjs-module'; | ||
import { Observable, throwError } from 'rxjs'; | ||
|
||
@Catch() | ||
export class ExceptionFilter extends BaseRpcExceptionFilter { | ||
catch(exception: HttpException): Observable<any> { | ||
constructor(@Optional() private readonly service?: OgmaFilterLogger) { | ||
super(); | ||
} | ||
catch(exception: HttpException, host: ArgumentsHost): Observable<any> { | ||
this.service?.log(exception, host); | ||
return throwError(() => exception); | ||
} | ||
} |
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
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
Oops, something went wrong.