Skip to content

Commit

Permalink
test(common/utils/catch-spotify-error): fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Dec 9, 2023
1 parent cdc9e73 commit c4d39f9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
49 changes: 37 additions & 12 deletions src/common/utils/catch-spotify-error.spec.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,78 @@
import { test, describe, expect } from 'vitest'
import { BadGatewayException, UnauthorizedException } from '@nestjs/common'

import { catchSpotifyError } from './catch-spotify-error'
import {
SPOTIFY_DEFAULT_ERROR_MESSAGE,
SpotifyAuthError,
catchSpotifyError,
} from './catch-spotify-error'

import { axiosResponseMockFactory } from '@common/mocks'

describe('catchSpotifyError', () => {
test('should throw UnauthorizedException', () => {
const message = 'Unauthorized'

expect(() =>
catchSpotifyError(
axiosResponseMockFactory(
{
error: {
message: 'Unauthorized',
message,
status: 401,
},
},
401
)
)
).toThrowError(UnauthorizedException)
).toThrowError(new UnauthorizedException(message))
})

test('should throw UnauthorizedException as invalid grant', () => {
const message = 'Invalid token'

expect(() =>
catchSpotifyError(
axiosResponseMockFactory(
{
error: {
message: 'invalid_grant',
status: 401,
},
error: 'invalid_grant',
error_description: message,
},
401
)
)
).toThrowError(UnauthorizedException)
).toThrowError(new UnauthorizedException(message))
})

test('should throw InternalServerErrorException', () => {
test('should throw BadGatewayException', () => {
const message = 'Bad Gateway'

expect(() =>
catchSpotifyError(
axiosResponseMockFactory({
error: {
message: 'Bad Gateway',
status: 500,
message,
status: 502,
},
})
)
).toThrowError(BadGatewayException)
).toThrowError(
new BadGatewayException(SPOTIFY_DEFAULT_ERROR_MESSAGE + message)
)
})

test('should throw BadGatewayException', () => {
const message = 'Bad Gateway'

expect(() =>
catchSpotifyError(
axiosResponseMockFactory<SpotifyAuthError>({
error: message,
error_description: message,
})
)
).toThrowError(
new BadGatewayException(SPOTIFY_DEFAULT_ERROR_MESSAGE + message)
)
})
})
16 changes: 9 additions & 7 deletions src/common/utils/catch-spotify-error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BadGatewayException, UnauthorizedException } from '@nestjs/common'
import { AxiosResponse } from 'axios'

export abstract class SpotifyAuthError {
export interface SpotifyAuthError {
error: string
error_description: string
}

export abstract class SpotifyError {
export interface SpotifyError {
error: {
status: number
message: string
Expand All @@ -17,18 +17,21 @@ export type SpotifyResponseError = AxiosResponse<
SpotifyError | SpotifyAuthError
>

export const SPOTIFY_DEFAULT_ERROR_MESSAGE =
'Something went wrong with fetching data from spotify API:'

export const catchSpotifyError = (response: SpotifyResponseError) => {
console.log(response.data.error)

const { data, status } = response

if (data instanceof SpotifyAuthError) {
if ('error_description' in data) {
console.log('ee')
if (data.error === 'invalid_grant')
throw new UnauthorizedException('Invalid token')

throw new BadGatewayException(
'Something went wrong with fetching data from spotify API',
data?.error_description
SPOTIFY_DEFAULT_ERROR_MESSAGE + data.error_description
)
}

Expand All @@ -37,7 +40,6 @@ export const catchSpotifyError = (response: SpotifyResponseError) => {
if (status === 401) throw new UnauthorizedException(data.error.message)

throw new BadGatewayException(
'Something went wrong with fetching data from spotify API',
data.error.message
SPOTIFY_DEFAULT_ERROR_MESSAGE + data.error.message
)
}

0 comments on commit c4d39f9

Please sign in to comment.