-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-statistics-endpoints
- Loading branch information
Showing
33 changed files
with
209 additions
and
107 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { describe, test, expect } from 'vitest' | ||
|
||
import { | ||
spotifyArtistMock, | ||
artistMock, | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { describe, test, expect } from 'vitest' | ||
|
||
import { | ||
artistsMock, | ||
spotifyArtistsMock, | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { test, describe, expect } from 'vitest' | ||
|
||
import { | ||
spotifyTracksMock, | ||
tracksMock, | ||
|
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 |
---|---|---|
@@ -1,52 +1,77 @@ | ||
import { test, describe, expect } from 'vitest' | ||
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) | ||
) | ||
}) | ||
}) |
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,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 | ||
) | ||
} |
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
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
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.