-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added custom NumbersException class #15
Changes from all commits
7de4553
f944e38
0aec289
20ae808
3873149
af027a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.bandwidth.sdk.numbers.exception; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class ExceptionUtils { | ||
|
||
public static <T> CompletableFuture<T> catchAsyncClientExceptions(ThrowableSupplier<CompletableFuture<T>> supplier) { | ||
try { | ||
return supplier.get(); | ||
} catch (Throwable e) { | ||
CompletableFuture<T> future = new CompletableFuture<>(); | ||
future.completeExceptionally(new NumbersClientException(e)); | ||
return future; | ||
} | ||
} | ||
|
||
public static <T> T catchClientExceptions(ThrowableSupplier<T> supplier) { | ||
try { | ||
return supplier.get(); | ||
} catch (Throwable e) { | ||
throw new NumbersClientException(e); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.bandwidth.sdk.numbers.exception; | ||
|
||
|
||
public class NumbersClientException extends RuntimeException { | ||
|
||
|
||
public NumbersClientException(String message) { | ||
super(message); | ||
} | ||
|
||
public NumbersClientException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public NumbersClientException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.bandwidth.sdk.numbers.exception; | ||
|
||
import com.bandwidth.sdk.numbers.serde.NumbersSerde; | ||
import com.bandwidth.sdk.numbers.models.NumbersApiError; | ||
import org.asynchttpclient.Response; | ||
|
||
public class NumbersServiceException extends RuntimeException { | ||
|
||
private NumbersApiError error; | ||
|
||
public NumbersServiceException(NumbersApiError error) { | ||
super(error.toString()); | ||
this.error = error; | ||
} | ||
|
||
public NumbersApiError getError() { | ||
return error; | ||
} | ||
|
||
public static void throwIfApiError(Response apiResponse) { | ||
if (!isSuccessfulHttpStatusCode(apiResponse.getStatusCode())) { | ||
try { | ||
throw new NumbersServiceException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code that was copied here had a bug: https://github.com/Bandwidth/messaging-java-sdk/pull/40/files |
||
new NumbersSerde().deserialize(apiResponse.getResponseBody(), NumbersApiError.class) | ||
); | ||
} catch (Exception e) { | ||
throw new NumbersClientException("Unknown error response from API: " + apiResponse); | ||
} | ||
} | ||
} | ||
|
||
static boolean isSuccessfulHttpStatusCode(int statusCode) { | ||
return (statusCode / 100) == 2; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.bandwidth.sdk.numbers.exception; | ||
|
||
public interface ThrowableSupplier<T> { | ||
T get() throws Throwable; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.bandwidth.sdk.numbers.models; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import org.immutables.value.Value; | ||
|
||
import java.util.List; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableNumbersApiError.class) | ||
@JsonDeserialize(as = ImmutableNumbersApiError.class) | ||
public abstract class NumbersApiError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the Numbers error's look different than our Messages api. We should probably just remove this (And NumbersApiFieldError) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's right. Based on the docs, all of the responses look like this
All have the same ResponseStatus fields, but the outer tag looks different for all of them |
||
public abstract String getType(); | ||
public abstract String getDescription(); | ||
public abstract List<NumbersApiFieldError> getFieldErrors(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.bandwidth.sdk.numbers.models; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableNumbersApiFieldError.class) | ||
@JsonDeserialize(as = ImmutableNumbersApiFieldError.class) | ||
public abstract class NumbersApiFieldError { | ||
public abstract String getFieldName(); | ||
public abstract String getDescription(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.bandwidth.sdk.numbers.serde; | ||
|
||
import static com.bandwidth.sdk.numbers.exception.ExceptionUtils.catchClientExceptions; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import isn't needed. |
||
|
||
|
||
public class NumbersSerde { | ||
|
||
private static final XmlMapper mapper = new XmlMapper(); | ||
|
||
public <T> T deserialize(String messageBody, TypeReference<T> clazz) { | ||
return catchClientExceptions(() -> mapper.readValue(messageBody, clazz)); | ||
} | ||
|
||
public <T> T deserialize(String messageBody, Class<T> clazz) { | ||
return catchClientExceptions(() -> mapper.readValue(messageBody, clazz)); | ||
} | ||
|
||
public <T> String serialize(T objectToMap) { | ||
return catchClientExceptions(() -> mapper.writeValueAsString(objectToMap)); | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this used pattern in Messaging SDK and Number SDK (with only the Exception name being different). Might make sense to pull this out functionality to a shared lib.