Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
#14 Removed builder
Browse files Browse the repository at this point in the history
  • Loading branch information
axelerod committed Sep 30, 2015
1 parent 0d1a5bf commit fa16d9b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

// TODO(AShesterov): refactor API-SDK: rename ApiException to SmartlingApiException

import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.JsonReader;
import com.smartling.api.sdk.dto.ApiCode;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.EmptyResponse;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -49,6 +55,21 @@ public class ApiException extends Exception
this.apiCode = apiCode;
}

public static ApiException newException(String contents, int httpCode)
{
ApiResponse<EmptyResponse> apiResponse = JsonReader.parseApiResponse(contents, new TypeToken<ApiResponseWrapper<EmptyResponse>>()
{
}
);
ApiCode apiCode = apiResponse.getCode();
List<String> messages = apiResponse.getMessages();
return new ApiException(messages, apiCode, httpCode);
}

public static ApiException newException(IOException e) {
return new ApiException(e, ApiCode.NETWORK_ERROR);
}

This comment has been minimized.

Copy link
@PavloMyrotiuk

PavloMyrotiuk Sep 30, 2015

Contributor

I don't like static method in Exception class. I would prefer constructor

public ApiCode getApiCode()
{
return apiCode;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.smartling.api.sdk.ProxyConfiguration;
import com.smartling.api.sdk.dto.file.StringResponse;
import com.smartling.api.sdk.exceptions.ApiException;
import com.smartling.api.sdk.exceptions.ApiExceptionBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -89,12 +88,12 @@ public StringResponse executeHttpCall(final HttpRequestBase httpRequest, final P
if (statusCode == HttpStatus.SC_OK)
return stringResponse;

throw new ApiExceptionBuilder().newException(stringResponse.getContents(), statusCode);
throw ApiException.newException(stringResponse.getContents(), statusCode);
}
catch (final IOException ioe)
{
logger.error(String.format(LOG_MESSAGE_ERROR_TEMPLATE, ioe.getMessage()));
throw new ApiExceptionBuilder().newException(ioe);
throw ApiException.newException(ioe);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,46 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ApiExceptionBuilderTest
public class ApiExceptionTest
{

public static final String ERROR_RESPONSE = "{\"response\":{\"data\":null,\"code\":\"VALIDATION_ERROR\",\"messages\":[\"apiKey parameter is required\",\"apiVersion parameter is required\"]}}";

private final ApiExceptionBuilder testedInstance = new ApiExceptionBuilder();

@Test
public void shouldReturnExceptionUsingContentsAndHttpCode()
{
ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0);
ApiException apiException = ApiException.newException(ERROR_RESPONSE, 0);

assertThat(apiException, instanceOf(ApiException.class));
}

@Test
public void shouldRetrieveCode()
{
ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0);
ApiException apiException = ApiException.newException(ERROR_RESPONSE, 0);

assertThat(apiException.getApiCode(), is(equalTo(ApiCode.VALIDATION_ERROR)));
}

@Test
public void shouldRetrieveMessages()
{
ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0);
ApiException apiException = ApiException.newException(ERROR_RESPONSE, 0);

assertThat(apiException.getMessages(), hasItems(equalTo("apiKey parameter is required"), equalTo("apiVersion parameter is required")));
}

@Test
public void shouldPassHttpCode()
{
ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 123);
ApiException apiException = ApiException.newException(ERROR_RESPONSE, 123);

assertThat(apiException.getHttpCode(), is(123));
}

@Test
public void shouldSetNetworkErrorCodeInCaseIoException() {
ApiException apiException = testedInstance.newException(new IOException("Some exception"));
ApiException apiException = ApiException.newException(new IOException("Some exception"));

assertThat(apiException.getApiCode(), is(ApiCode.NETWORK_ERROR));
}
Expand Down

0 comments on commit fa16d9b

Please sign in to comment.