Skip to content

Commit 72d0659

Browse files
committed
Introduce common exception types into the API to allow for more precise reactions to errors.
1 parent 09b7917 commit 72d0659

File tree

18 files changed

+441
-75
lines changed

18 files changed

+441
-75
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package net.adoptopenjdk.v3.api;
16+
17+
import java.util.Objects;
18+
19+
/**
20+
* The base type of exceptions raised by the API.
21+
*/
22+
23+
public class AOV3Exception extends Exception
24+
{
25+
/**
26+
* Construct an exception.
27+
*
28+
* @param message The message
29+
*/
30+
31+
public AOV3Exception(
32+
final String message)
33+
{
34+
super(Objects.requireNonNull(message, "message"));
35+
}
36+
37+
/**
38+
* Construct an exception.
39+
*
40+
* @param cause The cause
41+
* @param message The message
42+
*/
43+
44+
public AOV3Exception(
45+
final String message,
46+
final Throwable cause)
47+
{
48+
super(
49+
Objects.requireNonNull(message, "message"),
50+
Objects.requireNonNull(cause, "cause")
51+
);
52+
}
53+
54+
/**
55+
* Construct an exception.
56+
*
57+
* @param cause The cause
58+
*/
59+
60+
public AOV3Exception(
61+
final Throwable cause)
62+
{
63+
super(Objects.requireNonNull(cause, "cause"));
64+
}
65+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package net.adoptopenjdk.v3.api;
16+
17+
import java.net.URI;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
/**
23+
* An HTTP request failed because the server returned some sort of error.
24+
*/
25+
26+
public final class AOV3ExceptionHTTPRequestFailed extends AOV3Exception
27+
{
28+
private final int statusCode;
29+
private final Map<String, List<String>> headers;
30+
private final URI uri;
31+
32+
/**
33+
* Construct an exception.
34+
*
35+
* @param inStatusCode The HTTP status code
36+
* @param inUri The URI of the request
37+
* @param inMessage The error message
38+
* @param inHeaders The response headers
39+
*/
40+
41+
public AOV3ExceptionHTTPRequestFailed(
42+
final int inStatusCode,
43+
final URI inUri,
44+
final String inMessage,
45+
final Map<String, List<String>> inHeaders)
46+
{
47+
super(inMessage);
48+
this.statusCode =
49+
inStatusCode;
50+
this.uri =
51+
Objects.requireNonNull(inUri, "uri");
52+
this.headers =
53+
Objects.requireNonNull(inHeaders, "headers");
54+
}
55+
56+
/**
57+
* @return The URI of the request
58+
*/
59+
60+
public URI uri()
61+
{
62+
return this.uri;
63+
}
64+
65+
/**
66+
* @return The HTTP status code
67+
*/
68+
69+
public int statusCode()
70+
{
71+
return this.statusCode;
72+
}
73+
74+
/**
75+
* @return The response headers
76+
*/
77+
78+
public Map<String, List<String>> headers()
79+
{
80+
return this.headers;
81+
}
82+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package net.adoptopenjdk.v3.api;
16+
17+
import java.net.URI;
18+
import java.util.Objects;
19+
20+
/**
21+
* An HTTP request due to an I/O error.
22+
*/
23+
24+
public final class AOV3ExceptionHTTPRequestIOFailed extends AOV3Exception
25+
{
26+
private final URI uri;
27+
28+
/**
29+
* Construct an exception.
30+
*
31+
* @param inUri The request URI
32+
* @param cause The cause of the error
33+
*/
34+
35+
public AOV3ExceptionHTTPRequestIOFailed(
36+
final URI inUri,
37+
final Throwable cause)
38+
{
39+
super(cause);
40+
this.uri =
41+
Objects.requireNonNull(inUri, "uri");
42+
}
43+
44+
/**
45+
* @return The request URI
46+
*/
47+
48+
public URI uri()
49+
{
50+
return this.uri;
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package net.adoptopenjdk.v3.api;
16+
17+
/**
18+
* An exception caused by a parse error.
19+
*/
20+
21+
public final class AOV3ExceptionParseFailed extends AOV3Exception
22+
{
23+
/**
24+
* Construct an exception.
25+
*
26+
* @param cause The cause of the exception
27+
*/
28+
29+
public AOV3ExceptionParseFailed(
30+
final Throwable cause)
31+
{
32+
super(cause);
33+
}
34+
}

net.adoptopenjdk.v3.api/src/main/java/net/adoptopenjdk/v3/api/AOV3RequestType.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package net.adoptopenjdk.v3.api;
1616

17-
import java.io.IOException;
18-
1917
/**
2018
* An executable request. Requests can be created once and re-executed an
2119
* arbitrary number of times.
@@ -30,10 +28,10 @@ public interface AOV3RequestType<T>
3028
*
3129
* @return The received data
3230
*
33-
* @throws IOException On I/O errors
31+
* @throws AOV3Exception On errors
3432
* @throws InterruptedException If the thread hosting the operation was interrupted
3533
*/
3634

3735
T execute()
38-
throws IOException, InterruptedException;
36+
throws AOV3Exception, InterruptedException;
3937
}

net.adoptopenjdk.v3.tests/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<version>${project.version}</version>
3535
</dependency>
3636

37+
<dependency>
38+
<groupId>commons-io</groupId>
39+
<artifactId>commons-io</artifactId>
40+
</dependency>
3741
<dependency>
3842
<groupId>org.slf4j</groupId>
3943
<artifactId>slf4j-api</artifactId>

net.adoptopenjdk.v3.tests/src/test/java/net/adoptopenjdk/v3/tests/AOV3ClientsTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package net.adoptopenjdk.v3.tests;
1616

1717
import net.adoptopenjdk.v3.api.AOV3Error;
18+
import net.adoptopenjdk.v3.api.AOV3ExceptionHTTPRequestFailed;
1819
import net.adoptopenjdk.v3.api.AOV3ReleaseKind;
1920
import net.adoptopenjdk.v3.vanilla.AOV3Clients;
2021
import net.adoptopenjdk.v3.vanilla.AOV3Messages;
@@ -95,7 +96,7 @@ public void testRequestFailure0()
9596

9697
try (var client = clients.createClient()) {
9798
final var exception =
98-
Assertions.assertThrows(IOException.class, () -> {
99+
Assertions.assertThrows(AOV3ExceptionHTTPRequestFailed.class, () -> {
99100
client.assetsForRelease(
100101
this::logError,
101102
BigInteger.ZERO,
@@ -113,7 +114,14 @@ public void testRequestFailure0()
113114
).execute();
114115
});
115116

116-
Assertions.assertTrue(exception.getMessage().contains("500"));
117+
Assertions.assertEquals(
118+
500,
119+
exception.statusCode()
120+
);
121+
Assertions.assertEquals(
122+
"urn:test",
123+
exception.uri().toString()
124+
);
117125
}
118126
}
119127
}

0 commit comments

Comments
 (0)