Skip to content

Commit

Permalink
Merge branch 'main' into 111-setup-auto-import-in-vitest-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Dec 9, 2023
2 parents 388fbd7 + c4d39f9 commit 8d7881c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 57 deletions.
5 changes: 3 additions & 2 deletions src/common/mocks/axios-response.factory.mock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios'

export const axiosResponseMockFactory = <TData = unknown>(
data: TData
data: TData,
status = 200
): AxiosResponse<TData> => ({
data,
status: 200,
status,
statusText: 'OK',
headers: {},
config: {} as InternalAxiosRequestConfig,
Expand Down
86 changes: 56 additions & 30 deletions src/common/utils/catch-spotify-error.spec.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
import { BadGatewayException, UnauthorizedException } from '@nestjs/common'

import {
InternalServerErrorException,
UnauthorizedException,
} from '@nestjs/common'
SPOTIFY_DEFAULT_ERROR_MESSAGE,
SpotifyAuthError,
catchSpotifyError,
} from './catch-spotify-error'

import { catchSpotifyError } from './catch-spotify-error'
import { axiosResponseMockFactory } from '@common/mocks'

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

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

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

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

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

expect(() =>
catchSpotifyError({
response: {
data: {
error: {
message: 'Internal Server Error',
},
catchSpotifyError(
axiosResponseMockFactory({
error: {
message,
status: 502,
},
status: 500,
},
})
).toThrowError(InternalServerErrorException)
})
)
).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)
)
})
})
66 changes: 41 additions & 25 deletions src/common/utils/catch-spotify-error.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
import {
ForbiddenException,
InternalServerErrorException,
UnauthorizedException,
} from '@nestjs/common'

export const catchSpotifyError = error => {
console.log(error)

const {
response: { data, status },
} = error

if (data?.error === 'invalid_grant')
throw new UnauthorizedException('Invalid token')
if (status === 401) throw new UnauthorizedException(data?.error?.message)
if (
status === 403 &&
data === 'User not registered in the Developer Dashboard'
)
throw new ForbiddenException(
'User not registered in the Developer Dashboard'
import { BadGatewayException, UnauthorizedException } from '@nestjs/common'
import { AxiosResponse } from 'axios'

export interface SpotifyAuthError {
error: string
error_description: string
}

export interface SpotifyError {
error: {
status: number
message: string
}
}

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 ('error_description' in data) {
console.log('ee')
if (data.error === 'invalid_grant')
throw new UnauthorizedException('Invalid token')

throw new BadGatewayException(
SPOTIFY_DEFAULT_ERROR_MESSAGE + data.error_description
)
}

console.log(status)

if (status === 401) throw new UnauthorizedException(data.error.message)

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

0 comments on commit 8d7881c

Please sign in to comment.