Skip to content

Commit

Permalink
Changed error format to model messaging sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
jmulford-bw committed Nov 8, 2018
1 parent f944e38 commit 0aec289
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 18 deletions.
18 changes: 0 additions & 18 deletions src/main/java/com/bandwidth/sdk/numbers/NumbersException.java

This file was deleted.

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,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;
}
}
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 {
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();

}

0 comments on commit 0aec289

Please sign in to comment.