Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logging #100

Merged
merged 3 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auth/strategies/firebase-admin-user-login.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Strategy, VerifiedCallback } from 'passport-custom'
import { PassportStrategy } from '@nestjs/passport'
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { Injectable, UnauthorizedException, InternalServerErrorException } from '@nestjs/common'
import { ExtractJwt } from 'passport-jwt'
import { Request } from 'express'
import * as firebaseAdmin from 'firebase-admin'
Expand Down
47 changes: 47 additions & 0 deletions src/shared/filters/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common'
import { AppLogger } from '../logger/logger.service'

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
constructor(private appLogger: AppLogger) {
this.appLogger.setContext(AllExceptionsFilter.name)
}

catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp()
const response = ctx.getResponse()
const request = ctx.getRequest()

if (exception instanceof HttpException) {
const status = exception.getStatus()
const errorMessage = exception.getResponse() as HttpException

const responseObject = {
statusCode: status,
timestamp: new Date().toISOString(),
url: request.url,
...errorMessage,
}

this.appLogger.debug(JSON.stringify({ status, responseObject }))
return response.status(status).json(responseObject)
}

// Log the stack for non-HttpException errors
if (exception instanceof Error) {
this.appLogger.error(exception.message, exception.stack, exception.name)
} else {
console.log(exception)
}

const status = HttpStatus.INTERNAL_SERVER_ERROR
const responseObject = {
statusCode: status,
timestamp: new Date().toISOString(),
url: request.url,
}

this.appLogger.debug(JSON.stringify({ status, responseObject }))
return response.status(status).json(responseObject)
}
}
6 changes: 2 additions & 4 deletions src/shared/interceptors/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class LoggingInterceptor implements NestInterceptor {
const request = context.switchToHttp().getRequest()
const method = request.method
const url = request.originalUrl
this.appLogger.debug(`method: ${method} | url: ${url}`)
this.appLogger.debug(JSON.stringify({ method, url }))

const now = Date.now()
return next.handle().pipe(
Expand All @@ -22,9 +22,7 @@ export class LoggingInterceptor implements NestInterceptor {
const statusCode = response.statusCode

const responseTime = Date.now() - now
this.appLogger.debug(
`method: ${method} | url: ${url} | statusCode: ${statusCode} | responseTime: ${responseTime}ms`
)
this.appLogger.debug(JSON.stringify({ method, url, statusCode, responseTime }))
})
)
}
Expand Down
11 changes: 9 additions & 2 deletions src/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { configModuleOptions } from '../shared/config/module-options'
import { FirebaseModule } from './firebase/firebase.module'
import { LoggerModule } from './logger/logger.module'
import { LoggingInterceptor } from './interceptors/logging.interceptor'
import { APP_INTERCEPTOR } from '@nestjs/core'
import { APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core'
import { AllExceptionsFilter } from './filters/all-exceptions.filter'

@Module({
imports: [ConfigModule.forRoot(configModuleOptions), FirebaseModule, LoggerModule],
providers: [{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor }],
providers: [
{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },
{
provide: APP_FILTER,
useClass: AllExceptionsFilter,
},
],
exports: [ConfigModule, FirebaseModule, LoggerModule],
})
export class SharedModule {}