From 0aec28970bec343b7a4de40d5d35baf25e78cc31 Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Thu, 8 Nov 2018 14:04:31 -0500 Subject: [PATCH] Changed error format to model messaging sdk --- .../sdk/numbers/NumbersException.java | 18 ---------- .../exception/NumbersClientException.java | 18 ++++++++++ .../exception/NumbersServiceException.java | 36 +++++++++++++++++++ .../sdk/numbers/models/NumbersApiError.java | 16 +++++++++ .../numbers/models/NumbersApiFieldError.java | 14 ++++++++ 5 files changed, 84 insertions(+), 18 deletions(-) delete mode 100644 src/main/java/com/bandwidth/sdk/numbers/NumbersException.java create mode 100644 src/main/java/com/bandwidth/sdk/numbers/exception/NumbersClientException.java create mode 100644 src/main/java/com/bandwidth/sdk/numbers/exception/NumbersServiceException.java create mode 100644 src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiError.java create mode 100644 src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiFieldError.java diff --git a/src/main/java/com/bandwidth/sdk/numbers/NumbersException.java b/src/main/java/com/bandwidth/sdk/numbers/NumbersException.java deleted file mode 100644 index 2e8ec4a..0000000 --- a/src/main/java/com/bandwidth/sdk/numbers/NumbersException.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.bandwidth.sdk; - - -public class NumbersException extends RuntimeException { - - - public NumbersException(String message) { - super(message); - } - - public NumbersException(Throwable cause) { - super(cause); - } - - public NumbersException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersClientException.java b/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersClientException.java new file mode 100644 index 0000000..ea859a3 --- /dev/null +++ b/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersClientException.java @@ -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); + } +} diff --git a/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersServiceException.java b/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersServiceException.java new file mode 100644 index 0000000..9a2b0dc --- /dev/null +++ b/src/main/java/com/bandwidth/sdk/numbers/exception/NumbersServiceException.java @@ -0,0 +1,36 @@ +package com.bandwidth.sdk.numbers.exception; + +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( + //TODO: After NumbersSerde() is created, change this to something like this: + //new NumbersSerde().deserialize(apiResponse.getResponseBody(), NumbersApiError.class) + new NumbersApiError() + ); + } catch (Exception e) { + throw new NumbersClientException("Unknown error response from API: " + apiResponse); + } + } + } + + static boolean isSuccessfulHttpStatusCode(int statusCode) { + return (statusCode / 100) == 2; + } +} diff --git a/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiError.java b/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiError.java new file mode 100644 index 0000000..f49ae33 --- /dev/null +++ b/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiError.java @@ -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 { + public abstract String getType(); + public abstract String getDescription(); + public abstract List getFieldErrors(); +} diff --git a/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiFieldError.java b/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiFieldError.java new file mode 100644 index 0000000..e681367 --- /dev/null +++ b/src/main/java/com/bandwidth/sdk/numbers/models/NumbersApiFieldError.java @@ -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(); + +}