Skip to content

Commit 528ef86

Browse files
author
Carmine DiMascio
committed
provide mechanism to create an ApiError from an http status code
1 parent aef0c34 commit 528ef86

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ All 'out of the box' errors are Jackson ready and can be serialized as *JSON*, *
1717
Gradle
1818

1919
```groovy
20-
compile 'io.github.cdimascio:japi-errors:1.1.4'
20+
compile 'io.github.cdimascio:japi-errors:1.2.0'
2121
```
2222

2323
Maven
@@ -26,7 +26,7 @@ Maven
2626
<dependency>
2727
<groupId>io.github.cdimascio</groupId>
2828
<artifactId>japi-errors</artifactId>
29-
<version>1.1.4</version>
29+
<version>1.2.0</version>
3030
</dependency>
3131
```
3232

japi-errors-1.1.4.pom renamed to japi-errors-1.2.0.pom

Lines changed: 14 additions & 2 deletions
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.3</version>
13+
<version>1.2.0</version>
1414

1515
<licenses>
1616
<license>
@@ -50,10 +50,11 @@
5050

5151

5252
<properties>
53-
<kotlin.version>1.2.60</kotlin.version>
53+
<kotlin.version>1.3.0</kotlin.version>
5454
<main.class>io.github.cdimascio.japierrors.ApiErrors</main.class>
5555
<junit.version>4.12</junit.version>
5656
<jackson.version>2.9.7</jackson.version>
57+
<unirest.version>1.4.9</unirest.version>
5758
<junit.jupiter.version>5.3.1</junit.jupiter.version>
5859
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5960

@@ -88,6 +89,13 @@
8889
<scope>test</scope>
8990
</dependency>
9091

92+
<dependency>
93+
<groupId>com.mashape.unirest</groupId>
94+
<artifactId>unirest-java</artifactId>
95+
<version>${unirest.version}</version>
96+
<scope>test</scope>
97+
</dependency>
98+
9199
<dependency>
92100
<groupId>org.junit.jupiter</groupId>
93101
<artifactId>junit-jupiter-api</artifactId>
@@ -202,6 +210,10 @@
202210
<configuration>
203211
<source>8</source>
204212
<target>8</target>
213+
<compilerArgs>
214+
<arg>-verbose</arg>
215+
<arg>-Xlint:unchecked</arg>
216+
</compilerArgs>
205217
</configuration>
206218
</plugin>
207219
</plugins>

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.4</version>
13+
<version>1.2.0</version>
1414

1515
<licenses>
1616
<license>

src/main/java/io/github/cdimascio/japierrors/ApiError.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static <T extends ApiError> void creator(IApiErrorCreator<T> creator) {
1111
private static <T extends ApiError> IApiErrorCreator<T> getCreator() {
1212
return error;
1313
}
14+
1415
/**
1516
* Create a bad request api error using the specified throwable
1617
* @param t The exceptioo or throwable
@@ -477,6 +478,32 @@ public static <T extends ApiError> T upgradeRequired(String message) {
477478
public static <T extends ApiError> T upgradeRequired() {
478479
return (T) error.create(HttpStatus.UPGRADE_REQUIRED, "upgrade required");
479480
}
481+
482+
/**
483+
* Creates an upgrade required error with the specified exception or throwable
484+
* @param t The exception or throwable
485+
* @return The api error
486+
*/
487+
public static <T extends ApiError> T error(int code, Throwable t) {
488+
return (T) error.create(HttpStatus.fromCode(code), t);
489+
}
490+
491+
/**
492+
* Creates an upgrade required error with the specified message
493+
* @param message The message
494+
* @return The api error
495+
*/
496+
public static <T extends ApiError> T error(int code, String message) {
497+
return (T) error.create(HttpStatus.fromCode(code), message);
498+
}
499+
500+
/**
501+
* Creates an upgrade required error
502+
* @return The api error
503+
*/
504+
public static <T extends ApiError> T error(int code) {
505+
return (T) error.create(HttpStatus.fromCode(code), "error");
506+
}
480507
}
481508

482509

src/main/java/io/github/cdimascio/japierrors/HttpStatus.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ public String getDescription() {
4545
return this.description;
4646
}
4747

48+
public static HttpStatus fromCode(int code) {
49+
switch(code) {
50+
case 400: return BAD_REQUEST;
51+
case 409: return CONFLICT;
52+
case 403: return FORBIDDEN;
53+
case 504: return GATEWAY_TIMEOUT;
54+
case 410: return GONE;
55+
case 500: return INTERNAL_SERVER_ERROR;
56+
case 406: return NOT_ACCEPTABLE;
57+
case 404: return NOT_FOUND;
58+
case 501: return NOT_IMPLEMENTED;
59+
case 412: return PRECONDITION_FAILED;
60+
case 428: return PRECONDITION_REQUIRED;
61+
case 407: return PROXY_AUTHENTICATION_REQUIRED;
62+
case 413: return REQUEST_ENTITY_TOO_LARGE;
63+
case 503: return SERVICE_UNAVAILABLE;
64+
case 401: return UNAUTHORIZED;
65+
case 451: return UNAVAILABLE_FOR_LEGAL_REASONS;
66+
case 415: return UNSUPPORTED_MEDIA_TYPE;
67+
case 426: return UPGRADE_REQUIRED;
68+
default: throw new IllegalArgumentException("unknown status code " +code);
69+
}
70+
}
4871
@Override
4972
public String toString() {
5073
return "HttpStatus{" +

0 commit comments

Comments
 (0)