Skip to content

Commit

Permalink
Merge pull request #113 from traveltime-dev/add-optional-getcause-to-…
Browse files Browse the repository at this point in the history
…travel-time-error

TravelTimeError: add retrieveCause() with optional return type
  • Loading branch information
gergelytraveltime authored Dec 13, 2023
2 parents 498feba + cc1e385 commit 87302da
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,25 @@ TravelTimeSDK sdk = TravelTimeSDK
.build();
```

## Development

We use Maven as a build automation tool in this project.

To run unit tests you can execute:

```shell
mvn test
```

Please note that you will need to export some valid credentials as environment variables in your shell to make tests work:

```shell
export APP_ID="..."
export API_KEY="..."
export PROTO_USERNAME="..."
export PROTO_PASSWORD="..."
```

## Support

If you have problems, please write an issue or contact us by writing to [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Getter;
import lombok.NonNull;

import io.vavr.control.Option;

@Getter
@AllArgsConstructor
public class IOError implements TravelTimeError {
Expand All @@ -14,6 +16,9 @@ public class IOError implements TravelTimeError {
@NonNull
String errorMessage;

@Override
public Option<Throwable> retrieveCause() { return Option.of(cause); }

@Override
public String getMessage() {
return errorMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
Expand All @@ -10,6 +11,9 @@ public class JsonProcessingError implements TravelTimeError {
@NonNull
String errorMsg;

@Override
public Option<Throwable> retrieveCause() { return Option.none(); }

@Override
public String getMessage() {
return errorMsg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
Expand All @@ -10,6 +11,9 @@ public class ProtoError implements TravelTimeError {
@NonNull
String errorMsg;

@Override
public Option<Throwable> retrieveCause() { return Option.none(); }

@Override
public String getMessage() {
return errorMsg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
Expand All @@ -10,6 +11,9 @@ public class RequestError implements TravelTimeError {
@NonNull
String errorMsg;

@Override
public Option<Throwable> retrieveCause() { return Option.none(); }

@Override
public String getMessage() {
return errorMsg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.traveltime.sdk.dto.responses.errors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.vavr.control.Option;
import lombok.*;
import lombok.extern.jackson.Jacksonized;

Expand All @@ -25,6 +26,10 @@ public class ResponseError implements TravelTimeError {
@NonNull
Map<String, List<String>> additionalInfo;

@Override
@JsonIgnore
public Option<Throwable> retrieveCause() { return Option.none(); }

@Override
@JsonIgnore
public String getMessage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;

public interface TravelTimeError {
Option<Throwable> retrieveCause();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
Expand All @@ -10,6 +11,9 @@ public class ValidationError implements TravelTimeError {
@NonNull
String errorMsg;

@Override
public Option<Throwable> retrieveCause() { return Option.none(); }

@Override
public String getMessage() {
return errorMsg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.traveltime.sdk.dto.responses.errors;

import io.vavr.control.Option;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
Expand All @@ -12,6 +13,9 @@ public class XmlError implements TravelTimeError {
@NonNull
Throwable cause;

@Override
public Option<Throwable> retrieveCause() { return Option.of(cause); }

@Override
public String getMessage() {
return cause.getMessage();
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/com/traveltime/sdk/ErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.traveltime.sdk;

import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.requests.MapInfoRequest;
import com.traveltime.sdk.dto.responses.MapInfoResponse;
import com.traveltime.sdk.dto.responses.errors.IOError;
import com.traveltime.sdk.dto.responses.errors.ResponseError;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import com.traveltime.sdk.dto.responses.errors.XmlError;
import io.vavr.control.Either;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class ErrorTest {
TravelTimeSDK sdk;

@Before
public void init() {
TravelTimeCredentials credentials = new TravelTimeCredentials(
"wrong-api-id-does-not-exist-123456789",
"wrong-api-key-12345678"
);
sdk = new TravelTimeSDK(credentials);
}

@Test
public void shouldDetectErrorForWrongCredentials() {
MapInfoRequest request = new MapInfoRequest();
Either<TravelTimeError, MapInfoResponse> response = sdk.send(request);
Assert.assertTrue(response.isLeft());
Assert.assertTrue(response.getLeft() instanceof ResponseError);
ResponseError responseError = (ResponseError) response.getLeft();
Assert.assertEquals(7, (int) responseError.getErrorCode());
Assert.assertEquals(401, (int) responseError.getHttpStatus());
Assert.assertTrue(responseError.retrieveCause().isEmpty());
}

@Test
public void shouldRetrieveCauseForIOError() {
String message = "Booom";
Throwable cause = new IllegalStateException(message);
IOError ioError = new IOError(cause, "some error occurred");
Assert.assertFalse(ioError.retrieveCause().isEmpty());
Assert.assertSame(message, ioError.retrieveCause().get().getMessage());
}

@Test
public void shouldRetrieveCauseForXmlError() {
String message = "Booom";
Throwable cause = new IllegalStateException(message);
XmlError xmlError = new XmlError(cause);
Assert.assertFalse(xmlError.retrieveCause().isEmpty());
Assert.assertSame(message, xmlError.retrieveCause().get().getMessage());
}
}

0 comments on commit 87302da

Please sign in to comment.