Skip to content

Commit 19e9a53

Browse files
committed
replace inherited exceptions with a sealed SpotifyException class
1 parent 1b57e14 commit 19e9a53

29 files changed

+161
-147
lines changed

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pluginManagement {
1616
repositories {
1717
mavenCentral()
1818
jcenter()
19+
maven { url 'https://plugins.gradle.org/m2/' }
1920
}
2021
}
2122

src/commonMain/kotlin/com.adamratzman.spotify/Builder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.adamratzman.spotify
33

44
import com.adamratzman.spotify.http.HttpConnection
55
import com.adamratzman.spotify.http.HttpRequestMethod
6+
import com.adamratzman.spotify.models.SpotifyAuthenticationException
67
import com.adamratzman.spotify.models.Token
78
import com.adamratzman.spotify.models.serialization.toObject
89
import kotlinx.coroutines.Dispatchers
@@ -273,7 +274,7 @@ class SpotifyClientApiBuilder(
273274
options.testTokenValidity
274275
)
275276
} catch (e: Exception) {
276-
throw SpotifyException("Invalid credentials provided in the login process", e)
277+
throw SpotifyAuthenticationException("Invalid credentials provided in the login process", e)
277278
}
278279
authorization.token != null -> SpotifyClientApi(
279280
clientId,
@@ -402,7 +403,7 @@ class SpotifyAppApiBuilder(
402403
options.testTokenValidity
403404
)
404405
} catch (e: Exception) {
405-
throw SpotifyException("Invalid credentials provided in the login process", e)
406+
throw SpotifyAuthenticationException("Invalid credentials provided in the login process", e)
406407
}
407408
}
408409
}

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyApi.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.adamratzman.spotify.http.HttpResponse
2222
import com.adamratzman.spotify.http.SpotifyEndpoint
2323
import com.adamratzman.spotify.http.base64ByteEncode
2424
import com.adamratzman.spotify.models.AuthenticationError
25-
import com.adamratzman.spotify.models.BadRequestException
25+
import com.adamratzman.spotify.models.SpotifyAuthenticationException
2626
import com.adamratzman.spotify.models.Token
2727
import com.adamratzman.spotify.models.TokenValidityResponse
2828
import com.adamratzman.spotify.models.serialization.toObject
@@ -87,7 +87,7 @@ abstract class SpotifyApi internal constructor(
8787
try {
8888
isTokenValid(true)
8989
} catch (e: Exception) {
90-
throw SpotifyException("Invalid token provided on initialization", e)
90+
throw SpotifyAuthenticationException("Invalid token provided on initialization", e)
9191
}
9292
}
9393
}
@@ -164,7 +164,7 @@ abstract class SpotifyApi internal constructor(
164164
fun isTokenValid(makeTestRequest: Boolean = true): TokenValidityResponse {
165165
if (token.shouldRefresh()) return TokenValidityResponse(
166166
false,
167-
SpotifyException("Token needs to be refreshed (is it expired?)")
167+
SpotifyAuthenticationException("Token needs to be refreshed (is it expired?)")
168168
)
169169
if (!makeTestRequest) return TokenValidityResponse(true, null)
170170

@@ -418,7 +418,12 @@ class SpotifyClientApi internal constructor(
418418

419419
logger.logInfo("Successfully refreshed the Spotify token")
420420
return currentToken
421-
} else throw BadRequestException(response.body.toObject(AuthenticationError.serializer(), this))
421+
} else throw SpotifyException.BadRequestException(
422+
response.body.toObject(
423+
AuthenticationError.serializer(),
424+
this
425+
)
426+
)
422427
}
423428

424429
override val endpoints: List<SpotifyEndpoint>
@@ -466,7 +471,8 @@ class SpotifyClientApi internal constructor(
466471
*/
467472
fun getAuthorizationUrl(vararg scopes: SpotifyScope): String {
468473
require(clientId != null && clientSecret != null) { "Either the client id or the client secret is not set" }
469-
return redirectUri?.let { getAuthUrlFull(*scopes, clientId = clientId, redirectUri = it) } ?: throw IllegalArgumentException("The redirect uri must be set")
474+
return redirectUri?.let { getAuthUrlFull(*scopes, clientId = clientId, redirectUri = it) }
475+
?: throw IllegalArgumentException("The redirect uri must be set")
470476
}
471477

472478
/**
@@ -507,7 +513,7 @@ fun getCredentialedToken(clientId: String, clientSecret: String, api: SpotifyApi
507513

508514
if (response.responseCode / 200 == 1) return response.body.toObject(Token.serializer(), null)
509515

510-
throw BadRequestException(response.body.toObject(AuthenticationError.serializer(), null))
516+
throw SpotifyException.BadRequestException(response.body.toObject(AuthenticationError.serializer(), null))
511517
}
512518

513519
internal fun executeTokenRequest(httpConnection: HttpConnection, clientId: String, clientSecret: String): HttpResponse {
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
11
/* Spotify Web API - Kotlin Wrapper; MIT License, 2019; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

4-
open class SpotifyException(message: String, cause: Throwable? = null) : Exception(message, cause)
4+
import com.adamratzman.spotify.models.AuthenticationError
5+
import com.adamratzman.spotify.models.ErrorObject
6+
import io.ktor.client.features.ResponseException
7+
8+
sealed class SpotifyException(message: String, cause: Throwable? = null) : Exception(message, cause) {
9+
abstract class UnNullableException(message: String) : SpotifyException(message)
10+
11+
/**
12+
* Thrown when a request fails
13+
*/
14+
open class BadRequestException(message: String, val statusCode: Int? = null, cause: Throwable? = null) :
15+
SpotifyException(message, cause) {
16+
constructor(message: String, cause: Throwable? = null) : this(message, null, cause)
17+
constructor(error: ErrorObject, cause: Throwable? = null) : this(
18+
"Received Status Code ${error.status}. Error cause: ${error.message}",
19+
error.status,
20+
cause
21+
)
22+
23+
constructor(authenticationError: AuthenticationError) :
24+
this(
25+
"Authentication error: ${authenticationError.error}. Description: ${authenticationError.description}",
26+
401
27+
)
28+
constructor(responseException: ResponseException) :
29+
this(
30+
responseException.message ?: "Bad Request",
31+
responseException.response.status.value,
32+
responseException
33+
)
34+
}
35+
36+
class SpotifyParseException(message: String, cause: Throwable? = null) : SpotifyException(message, cause)
37+
}

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientPlayerApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.adamratzman.spotify.http.SpotifyEndpoint
1010
import com.adamratzman.spotify.http.encodeUrl
1111
import com.adamratzman.spotify.models.AlbumUri
1212
import com.adamratzman.spotify.models.ArtistUri
13-
import com.adamratzman.spotify.models.BadRequestException
1413
import com.adamratzman.spotify.models.CurrentlyPlayingContext
1514
import com.adamratzman.spotify.models.CurrentlyPlayingObject
1615
import com.adamratzman.spotify.models.CursorBasedPagingObject

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientPlaylistApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package com.adamratzman.spotify.endpoints.client
33

44
import com.adamratzman.spotify.SpotifyApi
55
import com.adamratzman.spotify.SpotifyClientApi
6+
import com.adamratzman.spotify.SpotifyException
67
import com.adamratzman.spotify.SpotifyRestAction
78
import com.adamratzman.spotify.SpotifyRestActionPaging
89
import com.adamratzman.spotify.SpotifyScope
910
import com.adamratzman.spotify.endpoints.public.PlaylistApi
1011
import com.adamratzman.spotify.http.EndpointBuilder
1112
import com.adamratzman.spotify.http.encodeUrl
12-
import com.adamratzman.spotify.models.BadRequestException
1313
import com.adamratzman.spotify.models.ErrorObject
1414
import com.adamratzman.spotify.models.PagingObject
1515
import com.adamratzman.spotify.models.Playlist
@@ -65,7 +65,7 @@ class ClientPlaylistApi(api: SpotifyApi) : PlaylistApi(api) {
6565
collaborative: Boolean? = null,
6666
user: String = (api as SpotifyClientApi).userId
6767
): SpotifyRestAction<Playlist> {
68-
if (name.isEmpty()) throw BadRequestException(ErrorObject(400, "Name cannot be empty"))
68+
if (name.isEmpty()) throw SpotifyException.BadRequestException(ErrorObject(400, "Name cannot be empty"))
6969
return toAction {
7070
val body = jsonMap()
7171
body += json { "name" to name }

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/AlbumApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.adamratzman.spotify.http.encodeUrl
1010
import com.adamratzman.spotify.models.Album
1111
import com.adamratzman.spotify.models.AlbumUri
1212
import com.adamratzman.spotify.models.AlbumsResponse
13-
import com.adamratzman.spotify.models.BadRequestException
1413
import com.adamratzman.spotify.models.PagingObject
1514
import com.adamratzman.spotify.models.SimpleTrack
1615
import com.adamratzman.spotify.models.serialization.toObject

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/ArtistApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.adamratzman.spotify.http.encodeUrl
1010
import com.adamratzman.spotify.models.Artist
1111
import com.adamratzman.spotify.models.ArtistList
1212
import com.adamratzman.spotify.models.ArtistUri
13-
import com.adamratzman.spotify.models.BadRequestException
1413
import com.adamratzman.spotify.models.CursorBasedPagingObject
1514
import com.adamratzman.spotify.models.PagingObject
1615
import com.adamratzman.spotify.models.SimpleAlbum

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/BrowseApi.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
package com.adamratzman.spotify.endpoints.public
33

44
import com.adamratzman.spotify.SpotifyApi
5+
import com.adamratzman.spotify.SpotifyException
56
import com.adamratzman.spotify.SpotifyRestAction
67
import com.adamratzman.spotify.SpotifyRestActionPaging
78
import com.adamratzman.spotify.http.EndpointBuilder
89
import com.adamratzman.spotify.http.SpotifyEndpoint
910
import com.adamratzman.spotify.http.encodeUrl
1011
import com.adamratzman.spotify.models.ArtistUri
11-
import com.adamratzman.spotify.models.BadRequestException
1212
import com.adamratzman.spotify.models.ErrorObject
1313
import com.adamratzman.spotify.models.FeaturedPlaylists
1414
import com.adamratzman.spotify.models.PagingObject
@@ -290,7 +290,12 @@ class BrowseApi(api: SpotifyApi) : SpotifyEndpoint(api) {
290290
maxAttributes: Map<TuneableTrackAttribute<*>, Number> = mapOf()
291291
): SpotifyRestAction<RecommendationResponse> {
292292
if (seedArtists?.isEmpty() != false && seedGenres?.isEmpty() != false && seedTracks?.isEmpty() != false) {
293-
throw BadRequestException(ErrorObject(400, "At least one seed (genre, artist, track) must be provided."))
293+
throw SpotifyException.BadRequestException(
294+
ErrorObject(
295+
400,
296+
"At least one seed (genre, artist, track) must be provided."
297+
)
298+
)
294299
}
295300

296301
return toAction {

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/FollowingApi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.adamratzman.spotify.SpotifyRestAction
66
import com.adamratzman.spotify.http.EndpointBuilder
77
import com.adamratzman.spotify.http.SpotifyEndpoint
88
import com.adamratzman.spotify.http.encodeUrl
9-
import com.adamratzman.spotify.models.BadRequestException
109
import com.adamratzman.spotify.models.PlaylistUri
1110
import com.adamratzman.spotify.models.UserUri
1211
import com.adamratzman.spotify.models.serialization.toList

0 commit comments

Comments
 (0)