Skip to content

Commit e9c59e3

Browse files
author
Carmine DiMascio
committed
add 429 too many requests
1 parent 528ef86 commit e9c59e3

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-0
lines changed

japi-errors-1.3.0.pom

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<name>jwcp-errors</name>
8+
<description>Errors for http apis</description>
9+
<url>https://github.com/cdimascio/jwcp-errors</url>
10+
11+
<groupId>io.github.cdimascio</groupId>
12+
<artifactId>japi-errors</artifactId>
13+
<version>1.3.0</version>
14+
15+
<licenses>
16+
<license>
17+
<name>Apache License, Version 2.0</name>
18+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
19+
<distribution>repo</distribution>
20+
<comments>A business-friendly OSS license</comments>
21+
</license>
22+
</licenses>
23+
24+
<organization>
25+
<name>Carmine DiMascio OSS</name>
26+
<url>https://github.com/cdimascio</url>
27+
</organization>
28+
29+
<scm>
30+
<connection>scm:github:https://github.com/cdimascio/jwcp-errors</connection>
31+
<developerConnection>scm:github:https://github.com/cdimascio/jwcp-errors</developerConnection>
32+
<tag>master</tag>
33+
<url>https://github.com/cdimascio/jwcp-errors</url>
34+
</scm>
35+
36+
<developers>
37+
<developer>
38+
<id>cdimascio</id>
39+
<name>Carmine DiMascio</name>
40+
<email>cdimascio@gmail.com</email>
41+
<url>https://www.github.com/cdimascio</url>
42+
<organization>Carmine DiMascio OSS</organization>
43+
<organizationUrl>https://www.github.com/cdimascio</organizationUrl>
44+
<roles>
45+
<role>developer</role>
46+
</roles>
47+
<timezone>America/New_York</timezone>
48+
</developer>
49+
</developers>
50+
51+
52+
<properties>
53+
<kotlin.version>1.3.0</kotlin.version>
54+
<main.class>io.github.cdimascio.japierrors.ApiErrors</main.class>
55+
<junit.version>4.12</junit.version>
56+
<jackson.version>2.9.7</jackson.version>
57+
<unirest.version>1.4.9</unirest.version>
58+
<junit.jupiter.version>5.3.1</junit.jupiter.version>
59+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
60+
61+
<maven.source.plugin>3.0.1</maven.source.plugin>
62+
<maven.javadoc.plugin>3.0.0</maven.javadoc.plugin>
63+
<dokka.version>0.9.15</dokka.version>
64+
<bintray.subject>cdimascio</bintray.subject>
65+
<bintray.repo>maven</bintray.repo>
66+
<bintray.package>java-dotenv</bintray.package>
67+
</properties>
68+
69+
<pluginRepositories>
70+
<pluginRepository>
71+
<id>jcenter</id>
72+
<name>JCenter</name>
73+
<url>https://jcenter.bintray.com/</url>
74+
</pluginRepository>
75+
</pluginRepositories>
76+
77+
<dependencies>
78+
<dependency>
79+
<groupId>com.fasterxml.jackson.core</groupId>
80+
<artifactId>jackson-annotations</artifactId>
81+
<version>${jackson.version}</version>
82+
<scope>compile</scope>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>com.fasterxml.jackson.core</groupId>
87+
<artifactId>jackson-databind</artifactId>
88+
<version>${jackson.version}</version>
89+
<scope>test</scope>
90+
</dependency>
91+
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+
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter-api</artifactId>
102+
<version>${junit.jupiter.version}</version>
103+
<scope>test</scope>
104+
</dependency>
105+
106+
<dependency>
107+
<groupId>org.junit.jupiter</groupId>
108+
<artifactId>junit-jupiter-params</artifactId>
109+
<version>${junit.jupiter.version}</version>
110+
<scope>test</scope>
111+
</dependency>
112+
113+
<dependency>
114+
<groupId>org.junit.jupiter</groupId>
115+
<artifactId>junit-jupiter-engine</artifactId>
116+
<version>${junit.jupiter.version}</version>
117+
<scope>test</scope>
118+
</dependency>
119+
</dependencies>
120+
121+
<build>
122+
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
123+
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-jar-plugin</artifactId>
128+
<version>2.6</version>
129+
<configuration>
130+
<archive>
131+
<manifest>
132+
<addClasspath>true</addClasspath>
133+
<mainClass>${main.class}</mainClass>
134+
</manifest>
135+
</archive>
136+
</configuration>
137+
</plugin>
138+
<plugin>
139+
<groupId>org.apache.maven.plugins</groupId>
140+
<artifactId>maven-surefire-plugin</artifactId>
141+
<version>2.22.0</version>
142+
<configuration>
143+
<workingDirectory>${project.basedir}</workingDirectory>
144+
</configuration>
145+
</plugin>
146+
<plugin>
147+
<groupId>org.apache.maven.plugins</groupId>
148+
<artifactId>maven-source-plugin</artifactId>
149+
<version>${maven.source.plugin}</version>
150+
<executions>
151+
<execution>
152+
<id>attach-sources</id>
153+
<goals>
154+
<goal>jar</goal>
155+
</goals>
156+
</execution>
157+
</executions>
158+
</plugin>
159+
<plugin>
160+
<groupId>org.apache.maven.plugins</groupId>
161+
<artifactId>maven-javadoc-plugin</artifactId>
162+
<version>${maven.javadoc.plugin}</version>
163+
<executions>
164+
<execution>
165+
<id>attach-javadocs</id>
166+
<goals>
167+
<goal>jar</goal>
168+
</goals>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
<plugin>
173+
<groupId>org.jetbrains.dokka</groupId>
174+
<artifactId>dokka-maven-plugin</artifactId>
175+
<version>${dokka.version}</version>
176+
<executions>
177+
<execution>
178+
<phase>pre-site</phase>
179+
<goals>
180+
<goal>dokka</goal>
181+
</goals>
182+
</execution>
183+
</executions>
184+
</plugin>
185+
<plugin>
186+
<groupId>org.jacoco</groupId>
187+
<artifactId>jacoco-maven-plugin</artifactId>
188+
<version>0.7.6.201602180812</version>
189+
<executions>
190+
<execution>
191+
<id>prepare-agent</id>
192+
<goals>
193+
<goal>prepare-agent</goal>
194+
</goals>
195+
</execution>
196+
</executions>
197+
</plugin>
198+
<plugin>
199+
<groupId>org.eluder.coveralls</groupId>
200+
<artifactId>coveralls-maven-plugin</artifactId>
201+
<version>4.3.0</version>
202+
<configuration>
203+
<repoToken>i3Bl4av26PMqZNekY8X3Jt7t6YhwFXKFu</repoToken>
204+
</configuration>
205+
</plugin>
206+
<plugin>
207+
<groupId>org.apache.maven.plugins</groupId>
208+
<artifactId>maven-compiler-plugin</artifactId>
209+
<version>3.8.0</version>
210+
<configuration>
211+
<source>8</source>
212+
<target>8</target>
213+
<compilerArgs>
214+
<arg>-verbose</arg>
215+
<arg>-Xlint:unchecked</arg>
216+
</compilerArgs>
217+
</configuration>
218+
</plugin>
219+
</plugins>
220+
</build>
221+
</project>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,32 @@ public static <T extends ApiError> T serviceUnavailable() {
375375
return (T) error.create(HttpStatus.SERVICE_UNAVAILABLE, "service unavailable");
376376
}
377377

378+
/**
379+
* Creates a too many requests error with the specified exception or throwable
380+
* @param t The exception or throwable
381+
* @return The api error
382+
*/
383+
public static <T extends ApiError> T tooManyRequests(Throwable t) {
384+
return (T) error.create(HttpStatus.TOO_MANY_REQUESTS, t);
385+
}
386+
387+
/**
388+
* Creates a too many requests error with the specified message
389+
* @param message The message
390+
* @return The api error
391+
*/
392+
public static <T extends ApiError> T tooManyRequests(String message) {
393+
return (T) error.create(HttpStatus.TOO_MANY_REQUESTS, message);
394+
}
395+
396+
/**
397+
* Creates a too many requests error
398+
* @return The api error
399+
*/
400+
public static <T extends ApiError> T tooManyRequests() {
401+
return (T) error.create(HttpStatus.TOO_MANY_REQUESTS, "too many requests");
402+
}
403+
378404
/**
379405
* Creates an unauthorized error with the specified exception or throwable
380406
* @param t The exception or throwable

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum HttpStatus {
1313
NOT_IMPLEMENTED(501, "not implemented"),
1414
PRECONDITION_FAILED(412, "precondition failed"),
1515
PRECONDITION_REQUIRED(428, "precondition required"),
16+
TOO_MANY_REQUESTS(429, "too many requests"),
1617
PROXY_AUTHENTICATION_REQUIRED(407, "proxy authentication required"),
1718
REQUEST_ENTITY_TOO_LARGE(413, "request entity too large"),
1819
SERVICE_UNAVAILABLE(503, "service unavailable"),
@@ -61,6 +62,7 @@ public static HttpStatus fromCode(int code) {
6162
case 407: return PROXY_AUTHENTICATION_REQUIRED;
6263
case 413: return REQUEST_ENTITY_TOO_LARGE;
6364
case 503: return SERVICE_UNAVAILABLE;
65+
case 429: return TOO_MANY_REQUESTS;
6466
case 401: return UNAUTHORIZED;
6567
case 451: return UNAVAILABLE_FOR_LEGAL_REASONS;
6668
case 415: return UNSUPPORTED_MEDIA_TYPE;

src/test/java/ApiErrorBasicSpec.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.Test;
99

1010
import static io.github.cdimascio.japierrors.ApiError.badRequest;
11+
import static io.github.cdimascio.japierrors.ApiError.tooManyRequests;
1112
import static org.junit.jupiter.api.Assertions.assertEquals;
1213
import static org.junit.jupiter.api.Assertions.assertNull;
1314

@@ -38,4 +39,24 @@ public void badRequestJson() {
3839
assertEquals("bad request", node.get("error").asText());
3940
assertEquals(400, node.get("code").asInt());
4041
}
42+
43+
@Test
44+
public void tooManyRequestsErr() {
45+
String message = "oh no";
46+
ApiErrorBasic error = Assertions.assertThrows(ApiErrorBasic.class, () -> {
47+
throw tooManyRequests(message);
48+
});
49+
Assertions.assertNotNull(error.getError());
50+
assertEquals(429, error.getCode());
51+
assertEquals(message, error.getError());
52+
}
53+
54+
@Test
55+
public void tooManyRequestsJson() {
56+
ApiErrorBasic error = ApiError.tooManyRequests();
57+
JsonNode node = m.convertValue(error, JsonNode.class);
58+
assertNull(node.get("message"));
59+
assertEquals("too many requests", node.get("error").asText());
60+
assertEquals(429, node.get("code").asInt());
61+
}
4162
}

src/test/java/ApiErrorWcpSpec.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,24 @@ public void badRequestJson() {
3838
assertEquals("bad_request", node.get("errors").get(0).get("code").asText());
3939
assertEquals("bad request", node.get("errors").get(0).get("message").asText());
4040
}
41+
42+
@Test
43+
public void tooManyRequests() {
44+
String message = "oh no";
45+
ApiErrorWcp error = Assertions.assertThrows(ApiErrorWcp.class, () -> {
46+
throw ApiErrorWcp.tooManyRequests(message);
47+
});
48+
Assertions.assertNotNull(error.getTrace());
49+
Assertions.assertEquals(error.getStatus().getCode(), 429);
50+
Assertions.assertEquals(error.getErrors().get(0).getCode(), "too_many_requests");
51+
Assertions.assertEquals(error.getErrors().get(0).getMessage(), message);
52+
}
53+
@Test
54+
public void tooManyRequestsJson() {
55+
ApiError error = ApiError.tooManyRequests();
56+
JsonNode node = m.convertValue(error, JsonNode.class);
57+
assertNull(node.get("message"));
58+
assertEquals("too_many_requests", node.get("errors").get(0).get("code").asText());
59+
assertEquals("too many requests", node.get("errors").get(0).get("message").asText());
60+
}
4161
}

0 commit comments

Comments
 (0)