Skip to content

Commit

Permalink
Refactor ErrorFormatterService and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkeksz committed Dec 9, 2023
1 parent 776752a commit c86e3f4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
49 changes: 32 additions & 17 deletions src/error-formatter/error-formatter.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
import {Test, TestingModule} from '@nestjs/testing'
import {ReasonPhrases, StatusCodes} from 'http-status-codes'
import {ErrorFormatterService} from './error-formatter.service'
import {ReasonPhrases} from 'http-status-codes'

describe('ErrorFormatterService', () => {
let errorFormatterService: ErrorFormatterService
describe(ErrorFormatterService.name, () => {
let service: ErrorFormatterService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ErrorFormatterService],
}).compile()

errorFormatterService = module.get(ErrorFormatterService)
service = module.get(ErrorFormatterService)
})

it('should be defined', () => {
expect(service).toBeDefined()
})

describe('formatHttpErrorCode', () => {
it('string: should return IM_A_TEAPOT', () => {
const result = errorFormatterService.formatHttpErrorCode(ReasonPhrases.IM_A_TEAPOT)
expect(result).toBe('IM_A_TEAPOT')
})
it('should return formatted error if http error code is known', () => {
const httpErrorCode = StatusCodes.NOT_FOUND
const expectedResult = service.formatHttpErrorCode(ReasonPhrases.NOT_FOUND)

it('number: should return IM_A_TEAPOT', () => {
const result = errorFormatterService.formatHttpErrorCode(418)
expect(result).toBe('IM_A_TEAPOT')
const result = service.formatHttpErrorCode(httpErrorCode)

expect(result).toEqual(expectedResult)
})

it('unknown string: should return INTERNAL_SERVER_ERROR', () => {
const result = errorFormatterService.formatHttpErrorCode('unknown')
expect(result).toBe('INTERNAL_SERVER_ERROR')
it('should return formatted INTERNAL_SERVER_ERROR if http error code is unknown', () => {
const httpErrorCode = 999
const expectedResult = service.formatHttpErrorCode(ReasonPhrases.INTERNAL_SERVER_ERROR)

const result = service.formatHttpErrorCode(httpErrorCode)

expect(result).toEqual(expectedResult)
})
})

describe('convertPhraseToHttpErrorCodeFormat', () => {
it('should convert phrase to http error code format', () => {
const text = 'Not Found'
const expectedResult = 'NOT_FOUND'

// convertPhraseToHttpErrorCodeFormat is a private method, so a direct test cannot be written
// we need to use formatHttpErrorCode for testing the functionality of convertPhraseToHttpErrorCodeFormat indirectly
const result = service.formatHttpErrorCode(text)

it('unknown number: should return INTERNAL_SERVER_ERROR', () => {
const result = errorFormatterService.formatHttpErrorCode(999)
expect(result).toBe('INTERNAL_SERVER_ERROR')
expect(result).toEqual(expectedResult)
})
})
})
13 changes: 10 additions & 3 deletions src/error-formatter/error-formatter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ export class ErrorFormatterService {
const isPhrase = !!Object.values(ReasonPhrases).find(value => value === httpErrorCode)
const isCode = !!Object.values(StatusCodes).find(value => value === httpErrorCode)
const isKnownHttpErrorCode = isPhrase || isCode

if (!isKnownHttpErrorCode) {
this.logger.warn(`Unknown HTTP error code: ${httpErrorCode}`)
return this.formatHttpErrorCode(ReasonPhrases.INTERNAL_SERVER_ERROR)
}

const text = isPhrase ? (httpErrorCode as string) : getReasonPhrase(httpErrorCode)
const result = text
const result = this.convertPhraseToHttpErrorCodeFormat(text)

this.logger.debug(`HTTP error code: ${result}`)
return result
}

private convertPhraseToHttpErrorCodeFormat(text: string): string {
return text
.toUpperCase()
.replaceAll(/[^A-Za-z\s]/g, '')
.replaceAll(' ', '_')
this.logger.debug(`HTTP error code: ${result}`)
return result
}
}
1 change: 0 additions & 1 deletion src/verification-codes/verification-codes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export class VerificationCodesService {
public readonly verificationCodeRepository: Repository<VerificationCode>,
public readonly sendingVerificationCodeAttemptsService: SendingVerificationCodeAttemptsService,
private readonly usersService: UsersService,
private readonly authConfigService: AuthConfigService,
) {}

public async sendEmailVerificationCode(email: User['email'], clientIP: string): Promise<void> {
Expand Down

0 comments on commit c86e3f4

Please sign in to comment.