-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TravelTimeError: add retrieveCause() with optional return type
This exposes the causes that e.g. IOError detects. This can be useful for users to show more detailed errors in their applications. IOError and XmlError support returning the cause. Other subclasses return Option.none(). Added ErrorTest to check whether Error classes are properly implemented. Checking wrong credentials (ResponseError) and explicitly setting up IOError and XmlError objects to check the retrieved exceptions. Extended documentation for how to set up testing.
- Loading branch information
1 parent
498feba
commit cc1e385
Showing
10 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/com/traveltime/sdk/dto/responses/errors/TravelTimeError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |