Skip to content

Commit b47880c

Browse files
author
Carmine DiMascio
committed
add tests
2 parents c1e3f1a + 9de57d1 commit b47880c

File tree

5 files changed

+48
-19
lines changed

5 files changed

+48
-19
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,45 @@ Maven
3232

3333
## Usage
3434

35-
Import
35+
Import error methods
3636

3737
```java
3838
import static io.github.cdimascio.japierrors.ApiError.badRequest;
39+
// ...
3940
```
4041

41-
Throw
42+
Throw any HTTP error and optionally pass an exception or custom message.
4243

4344
```java
4445
throw notFound();
4546
throw badRequest("id required.");
4647
throw internalServerError(exception);
48+
// ...
4749
```
4850

4951
Assign
5052

5153
```shell
52-
AbstractApiError error = unauthorized();
54+
ApiError error = unauthorized();
5355
```
5456

5557
See [examples](#examples) with Spring MVC
5658

5759
## Configure
5860

59-
japi-errors supports two error formats "out of the box". They are enabled as follows:
61+
**japi-errors** supports two error formats "out of the box". They are enabled as follows:
6062

6163
```
6264
ApiError.creator(ApiErrorCreators.BASIC); // The default
6365
ApiError.creator(ApiErrorCreators.WCP);
6466
```
6567

66-
### Basic
68+
### Basic (default)
6769

6870
```json
6971
{
7072
"code": 400,
71-
"error": "'id' required."
73+
"error": "id required."
7274
}
7375
```
7476

@@ -79,7 +81,7 @@ ApiError.creator(ApiErrorCreators.WCP);
7981
"trace": "1f96a430-dfd8-11e8-9f32-f2801f1b9fd1",
8082
"errors": [{
8183
"code": "bad_request",
82-
"message": "'id' required."
84+
"message": "id required."
8385
}]
8486
}
8587
```
@@ -109,6 +111,7 @@ public class MyApiErrorCreator implements IApiErrorCreator {
109111

110112
```java
111113
// Create a custom api error object
114+
// Add Json ignore properties (see source code link above)
112115
public class MyApiError extends ApiError {
113116
@JsonProperty
114117
private String message;
@@ -122,7 +125,7 @@ public class MyApiError extends ApiError {
122125
}
123126

124127
public String getMessage() {
125-
return message
128+
return message;
126129
}
127130
}
128131
```

japi-errors-1.1.2.pom renamed to japi-errors-1.1.3.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>io.github.cdimascio</groupId>
1212
<artifactId>japi-errors</artifactId>
13-
<version>1.1.2</version>
13+
<version>1.1.3</version>
1414

1515
<licenses>
1616
<license>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>io.github.cdimascio</groupId>
1212
<artifactId>japi-errors</artifactId>
13-
<version>1.1.2</version>
13+
<version>1.1.3</version>
1414

1515
<licenses>
1616
<license>

src/main/java/io/github/cdimascio/japierrors/wcp/ApiErrorWcp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
1717
creatorVisibility = JsonAutoDetect.Visibility.NONE
1818
)
19-
@JsonIgnoreProperties
19+
@JsonIgnoreProperties(ignoreUnknown = true)
2020
public class ApiErrorWcp extends ApiError {
2121
@JsonProperty
2222
private String trace;

src/test/java/UsageSpec.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import com.fasterxml.jackson.core.JsonProcessingException;
22
import com.fasterxml.jackson.databind.JsonNode;
33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.mashape.unirest.http.HttpResponse;
54
import com.mashape.unirest.http.Unirest;
65
import com.mashape.unirest.http.exceptions.UnirestException;
76
import io.github.cdimascio.japierrors.ApiError;
@@ -25,7 +24,7 @@ public static void beforeAll() {
2524
}
2625

2726
@Test
28-
public void autoConvert() {
27+
public void autoConvertToType() {
2928
ApiErrorBasic notFound = ApiError.unauthorized();
3029
assertEquals(401, notFound.getCode());
3130
}
@@ -43,20 +42,47 @@ public void unirestAutoConvert() {
4342
ApiErrorBasic error = Unirest.get("http://mockbin.org/status/404/not_found").asObject(ApiErrorBasic.class).getBody();
4443
assertEquals(404, error.getCode());
4544
} catch (UnirestException e) {
45+
fail("exception unexpected");
46+
}
47+
}
48+
49+
@Test
50+
public void serializeToJson() {
51+
try {
52+
String s = m.writeValueAsString(ApiError.badRequest());
53+
System.out.println(s);
54+
JsonNode json = m.readTree(s);
55+
assertNull(json.get("cause"));
56+
assertNull(json.get("message"));
57+
assertEquals(400, json.get("code").asInt());
58+
assertEquals("bad request", json.get("error").asText());
59+
} catch (IOException e) {
4660
e.printStackTrace();
4761
fail("unexpected exception");
4862
}
4963
}
5064

5165
@Test
52-
public void unirestJson() {
66+
public void deserializeToApiError() {
67+
JsonNode json = m.createObjectNode()
68+
.put("code", 400)
69+
.put("error", "bad request")
70+
.put("extra", "junk");
71+
72+
ApiErrorBasic error = m.convertValue(json, ApiErrorBasic.class);
73+
74+
assertEquals(400, error.getCode());
75+
assertEquals("bad request", error.getError());
76+
}
77+
78+
@Test
79+
public void httpErrorToApiError() {
5380
try {
54-
HttpResponse<JsonNode> res = Unirest.get("http://mockbin.org/status/404/not_found").asObject(JsonNode.class);
55-
assertEquals(404, res.getStatus());
56-
JsonNode body = res.getBody();
57-
System.out.println(body);
58-
assertNull(body.get("cause"));
81+
ApiErrorBasic error = Unirest
82+
.get("http://mockbin.org/status/404/not_found")
83+
.asObject(ApiErrorBasic.class).getBody();
5984

85+
assertEquals(404, error.getCode());
6086
} catch (UnirestException e) {
6187
e.printStackTrace();
6288
fail("unexpected exception");

0 commit comments

Comments
 (0)