Skip to content

Commit 0df1e14

Browse files
authored
0.2.0: Add DwollaException (#17)
* Add DwollaException * Bump version to 0.2.0 * Refactor * ktlint -F * Rethrow DwollaExceptions without rewrapping them * Formatting changes
1 parent 8e62a3c commit 0df1e14

8 files changed

+106
-58
lines changed

.travis.yml

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
language: java
2+
23
install: true
4+
35
jdk:
4-
#- openjdk13 # see jobs.include
5-
- openjdk12
6-
- openjdk11
7-
- openjdk10
8-
- openjdk9
9-
#- openjdk8 # see jobs.include
10-
- oraclejdk13
11-
- oraclejdk12
12-
- oraclejdk11
13-
- oraclejdk9
6+
#- openjdk13 # see jobs.include
7+
- openjdk12
8+
- openjdk11
9+
- openjdk10
10+
- openjdk9
11+
#- openjdk8 # see jobs.include
12+
- oraclejdk13
13+
- oraclejdk12
14+
- oraclejdk11
15+
- oraclejdk9
16+
1417
jobs:
1518
include:
1619
- jdk: openjdk13
1720
script:
18-
- "./gradlew ktlintCheck"
19-
- "./gradlew test --stacktrace"
21+
- "./gradlew ktlintCheck"
22+
- "./gradlew test --stacktrace"
2023
- jdk: openjdk8
2124
script:
22-
- "./gradlew ktlintCheck"
23-
- "./gradlew test --stacktrace"
25+
- "./gradlew ktlintCheck"
26+
- "./gradlew test --stacktrace"
27+
2428
script:
25-
- "./gradlew ktlintCheck"
26-
- "./gradlew test -PjUnitExcludeTags=api --stacktrace"
29+
- "./gradlew ktlintCheck"
30+
- "./gradlew test -PjUnitExcludeTags=api --stacktrace"
31+
2732
before_cache:
28-
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
29-
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
33+
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
34+
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
35+
3036
cache:
3137
directories:
32-
- "$HOME/.gradle/caches/"
33-
- "$HOME/.gradle/wrapper/"
38+
- "$HOME/.gradle/caches/"
39+
- "$HOME/.gradle/wrapper/"
40+
3441
env:
3542
secure: btlewfZncfLs+aYQ8Ccdp2QDAqspV9fS7qqW7ZfRtrLq4ZhgxxaK5SNXXGYFHmovdvVWwKjTAGskV8DbJKuSO6II9HeZDLRkN/J1Qco6hUTl44YhDaodxF9gzC/6tJE24Kd6+mn4dT8Ca+LCpNZnlYbMWqzRibjBPBCWwDcqB6o07GhlQHHsTaGs4d0FvNEvw6tbq51cNKHJfc+s+WPZs3sX1efHa+k5uqJCkVQakhZOX9jMBrzuRcnyY62Fhrr5Vj+xQgHOCY8P/Na+hHNQdFyuiBMFlK7z4Jjs4Xd7iLzpxgGD6WNjjba2XEZrC9dTzn+gCctfoX4Fz8VvPs+ToZHDmZLnJTgp7oKKFJqAIbIjxHG5pi8OOCLJM8hPT5a08HN1M0DSvg6FhiCOX4ya3FdGeB+El4UPFahh3zcblhoAsk8qVUK9y+Jh1bNViviGUxk7tZ9J+y6XmMemnA1OJBeufBiJrFTrkv8dfQJAtIr7n/JKXnM4v6LTcknIQNeqSVfvPCdajjrcDqechsDTb4MGKoWHLumxQxqwp+knBhL6Dy0uR0nsQVJCv6W0pAtEr18DqR+yJlJ1WpUHUneh9XnzrfmAh5F9rcGfyw1qvUh3yn6EK8uLGuk3I7fstgMghLu5lQd513GjLgV75SwzL1WXPRgt1iioM9ayuzRusBU=

README.md

+23-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Add this to your project's POM:
2222
<dependency>
2323
<groupId>com.github.Dwolla</groupId>
2424
<artifactId>dwolla-v2-kotlin</artifactId>
25-
<version>0.1.2</version>
25+
<version>0.2.0</version>
2626
</dependency>
2727
```
2828

@@ -41,7 +41,7 @@ repositories {
4141

4242
```groovy
4343
dependencies {
44-
implementation("com.github.Dwolla:dwolla-v2-kotlin:0.1.2")
44+
implementation("com.github.Dwolla:dwolla-v2-kotlin:0.2.0")
4545
}
4646
```
4747

@@ -128,13 +128,19 @@ As of now, a subset of the Dwolla API has high-level methods available:
128128

129129
## Handling errors
130130

131-
Requests made with Dwolla V2 Kotlin throw two types of exceptions:
131+
Dwolla V2 Kotlin has 3 types of exceptions:
132132

133-
- `DwollaApiException`: Thrown when an API request is unsuccessful. This could occur for a variety of reasons such as
134-
invalid request parameters. Details can be found in the exception's `error (DwollaApiError)` object.
133+
```
134+
DwollaException
135+
├── DwollaApiException
136+
└── DwollaAuthException
137+
```
138+
139+
- `DwollaApiException`: Thrown when the Dwolla API returns an error response. This could occur
140+
for a variety of reasons such as invalid request parameters.
135141
- `DwollaAuthException`: Thrown when an error occurs obtaining authenticating with the API. You should not encounter
136-
this exception unless your `Dwolla` key and/or secret is incorrect. Details can be found in the exception's
137-
`error (DwollaAuthError)` object.
142+
this exception unless your `Dwolla` key/secret are incorrect.
143+
- `DwollaException`: The base class other exceptions inherit from.
138144

139145
#### Kotlin
140146

@@ -146,11 +152,14 @@ try {
146152
e.statusCode // Int
147153
e.headers // Headers
148154
e.error // DwollaError
149-
} catch (e: DwollaApiException) {
155+
} catch (e: DwollaAuthException) {
150156
e.message // String
151157
e.statusCode // Int
152158
e.headers // Headers
153159
e.error // OAuthError
160+
} catch (e: DwollaException) {
161+
e.message // String
162+
e.cause // Throwable?
154163
}
155164
```
156165

@@ -169,6 +178,9 @@ try {
169178
Integer statusCode = e.statusCode;
170179
Headers headers = e.headers;
171180
OAuthError error = e.error;
181+
} catch (DwollaAuthException e) {
182+
String message = e.message;
183+
Throwable cause = e.cause;
172184
}
173185
```
174186

@@ -179,6 +191,9 @@ If you have any feedback please [reach out](https://discuss.dwolla.com/) to us o
179191

180192
## Changelog
181193

194+
- **0.2.0**
195+
- Add `DwollaException` base exception class
196+
- Swallow and rethrow exceptions using `DwollaException`
182197
- **0.1.2**
183198
- Add `delete` methods to `DwollaClient`
184199
- **0.1.1**

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = "com.dwolla"
2-
version = "0.1.2"
2+
version = "0.2.0"
33

44
plugins {
55
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.

src/main/kotlin/com/dwolla/Dwolla.kt

+27-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.dwolla
22

33
import com.dwolla.api.* // ktlint-disable no-wildcard-imports
44
import com.dwolla.exception.DwollaAuthException
5+
import com.dwolla.exception.DwollaException
56
import com.dwolla.http.Headers
67
import com.dwolla.resource.DwollaAuthError
78
import com.dwolla.util.Deserializer
@@ -64,27 +65,33 @@ class Dwolla(
6465
}
6566

6667
internal fun fetchToken(vararg params: Pair<String, String?>): TokenResponse {
67-
val result = fuelManager
68-
.post(environment.tokenUrl, params.filterNot { (_, v) -> v.isNullOrBlank() }.toList())
69-
.header(USER_AGENT_HEADER)
70-
.authentication()
71-
.basic(key, secret)
72-
.responseObject(Deserializer(gson, TokenResponse::class.java))
73-
val responseHeaders = Headers(*result.second.headers.map { h -> h.key to h.value.first() }.toTypedArray())
74-
return result.third.fold(
75-
success = { res -> res },
76-
failure = {
77-
val rawBody = result.second.data.toString(Charsets.UTF_8)
78-
if (rawBody.isBlank() && result.second.statusCode == 401) {
79-
// for some reason we can't read the response body when a 401 is returned
80-
val error = DwollaAuthError("", null, null)
81-
throw DwollaAuthException("401 Unauthorized", result.second.statusCode, responseHeaders, error)
82-
} else {
83-
val error = Deserializer(gson, DwollaAuthError::class.java).deserialize(rawBody)
84-
throw DwollaAuthException(rawBody, result.second.statusCode, responseHeaders, error)
68+
try {
69+
val result = fuelManager
70+
.post(environment.tokenUrl, params.filterNot { (_, v) -> v.isNullOrBlank() }.toList())
71+
.header(USER_AGENT_HEADER)
72+
.authentication()
73+
.basic(key, secret)
74+
.responseObject(Deserializer(gson, TokenResponse::class.java))
75+
val responseHeaders = Headers(*result.second.headers.map { h -> h.key to h.value.first() }.toTypedArray())
76+
return result.third.fold(
77+
success = { res -> res },
78+
failure = {
79+
val rawBody = result.second.data.toString(Charsets.UTF_8)
80+
if (rawBody.isBlank() && result.second.statusCode == 401) {
81+
// for some reason we can't read the response body when a 401 is returned
82+
val error = DwollaAuthError("", null, null)
83+
throw DwollaAuthException("401 Unauthorized", result.second.statusCode, responseHeaders, error)
84+
} else {
85+
val error = Deserializer(gson, DwollaAuthError::class.java).deserialize(rawBody)
86+
throw DwollaAuthException(rawBody, result.second.statusCode, responseHeaders, error)
87+
}
8588
}
86-
}
87-
)
89+
)
90+
} catch (e: DwollaException) {
91+
throw e
92+
} catch (e: Exception) {
93+
throw DwollaException("See stack trace for more details...", e)
94+
}
8895
}
8996

9097
// for testing purposes

src/main/kotlin/com/dwolla/DwollaClient.kt

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.dwolla
22

33
import com.dwolla.exception.DwollaApiException
44
import com.dwolla.exception.DwollaAuthException
5+
import com.dwolla.exception.DwollaException
56
import com.dwolla.http.* // ktlint-disable no-wildcard-imports
67
import com.dwolla.resource.DwollaApiError
78
import com.dwolla.util.Deserializer
@@ -28,7 +29,7 @@ abstract class DwollaClient(@JvmField val environment: DwollaEnvironment) {
2829

2930
companion object {
3031
val ACCEPT_HEADER = mapOf("accept" to "application/vnd.dwolla.v1.hal+json")
31-
val USER_AGENT_HEADER = mapOf("user-agent" to "dwolla-v2-kotlin/0.1.2")
32+
val USER_AGENT_HEADER = mapOf("user-agent" to "dwolla-v2-kotlin/0.2.0")
3233
}
3334

3435
@Throws(DwollaApiException::class, DwollaAuthException::class)
@@ -254,15 +255,27 @@ abstract class DwollaClient(@JvmField val environment: DwollaEnvironment) {
254255
}
255256

256257
private fun makeRequest(request: Request): Response<String> {
257-
val preparedRequest = prepareRequest(request)
258-
val result = preparedRequest.responseString()
259-
return handleResponse(result)
258+
try {
259+
val preparedRequest = prepareRequest(request)
260+
val result = preparedRequest.responseString()
261+
return handleResponse(result)
262+
} catch (e: DwollaException) {
263+
throw e
264+
} catch (e: Exception) {
265+
throw DwollaException("See stack trace for more details...", e)
266+
}
260267
}
261268

262269
private fun <T : Any> makeRequest(deserializeAs: Class<T>, request: Request): Response<T> {
263-
val preparedRequest = prepareRequest(request)
264-
val result = preparedRequest.responseObject(Deserializer(gson, deserializeAs))
265-
return handleResponse(result)
270+
try {
271+
val preparedRequest = prepareRequest(request)
272+
val result = preparedRequest.responseObject(Deserializer(gson, deserializeAs))
273+
return handleResponse(result)
274+
} catch (e: DwollaException) {
275+
throw e
276+
} catch (e: Exception) {
277+
throw DwollaException("See stack trace for more details...", e)
278+
}
266279
}
267280

268281
private fun prepareRequest(request: Request): Request {

src/main/kotlin/com/dwolla/exception/DwollaApiException.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ open class DwollaApiException(
88
@JvmField val statusCode: Int,
99
@JvmField val headers: Headers,
1010
@JvmField val error: DwollaApiError
11-
) : Exception(message)
11+
) : DwollaException(message)

src/main/kotlin/com/dwolla/exception/DwollaAuthException.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ open class DwollaAuthException(
88
@JvmField val statusCode: Int,
99
@JvmField val headers: Headers,
1010
@JvmField val error: DwollaAuthError
11-
) : Exception(message)
11+
) : DwollaException(message)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.dwolla.exception
2+
3+
open class DwollaException(
4+
message: String,
5+
cause: Throwable? = null
6+
) : Exception(message, cause)

0 commit comments

Comments
 (0)