Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies {
compile 'org.asynchttpclient:async-http-client:2.6.0'
compile 'com.google.guava:guava:27.0-jre'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-guava:2.9.5'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.4'
testCompile('org.assertj:assertj-core:3.6.2')
testCompile('org.mockito:mockito-core:2.7.9')
testCompile('junit:junit:4.4')
Expand Down
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 {

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.


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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

<BillingReportCreationResponse>
    <ResponseStatus>
        <ErrorCode>15501</ErrorCode>
        <Description>Invalid date range. Start date 2014-05-21 must be before end date 2013-05-29</Description>
    </ResponseStatus>
</BillingReportCreationResponse>

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();

}
27 changes: 27 additions & 0 deletions src/main/java/com/bandwidth/sdk/numbers/serde/NumbersSerde.java
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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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));
}

}