Skip to content

Commit 90f411a

Browse files
committed
Add ImClientServerErrorException class #58
1 parent e7b52d7 commit 90f411a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (C) GRyCAP - I3M - UPV
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* <p>http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* <p>Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.upv.i3m.grycap.im.exceptions;
18+
19+
import es.upv.i3m.grycap.im.pojo.ResponseError;
20+
21+
public class ImClientServerErrorException extends ImClientException {
22+
23+
private static final long serialVersionUID = 6007438931649254821L;
24+
private final ResponseError responseError;
25+
26+
public ImClientServerErrorException(ResponseError responseError) {
27+
this.responseError = responseError;
28+
}
29+
30+
public ResponseError getResponseError() {
31+
return responseError;
32+
}
33+
34+
}

src/main/java/es/upv/i3m/grycap/im/rest/client/ImClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import es.upv.i3m.grycap.file.Utf8File;
2323
import es.upv.i3m.grycap.im.exceptions.ImClientErrorException;
2424
import es.upv.i3m.grycap.im.exceptions.ImClientException;
25+
import es.upv.i3m.grycap.im.exceptions.ImClientServerErrorException;
2526
import es.upv.i3m.grycap.im.lang.ImMessages;
2627
import es.upv.i3m.grycap.im.pojo.ResponseError;
2728
import es.upv.i3m.grycap.im.rest.client.parameters.RestParameter;
@@ -30,6 +31,7 @@
3031

3132
import java.nio.file.Path;
3233

34+
import javax.ws.rs.ServerErrorException;
3335
import javax.ws.rs.WebApplicationException;
3436
import javax.ws.rs.client.Client;
3537
import javax.ws.rs.client.Entity;
@@ -144,6 +146,8 @@ public <T> T get(final String path, final Class<T> type,
144146
logCallInfo(HttpMethods.GET, path);
145147
return configureClient(path, parameters).get(type);
146148

149+
} catch (ServerErrorException exception) {
150+
throw new ImClientServerErrorException(createReponseError(exception));
147151
} catch (WebApplicationException exception) {
148152
throw new ImClientErrorException(createReponseError(exception));
149153
}
@@ -159,6 +163,8 @@ public <T> T delete(final String path, final Class<T> type,
159163
Builder clientConfigured = configureClient(path, parameters);
160164
return clientConfigured.delete(type);
161165

166+
} catch (ServerErrorException exception) {
167+
throw new ImClientServerErrorException(createReponseError(exception));
162168
} catch (WebApplicationException exception) {
163169
throw new ImClientErrorException(createReponseError(exception));
164170
}
@@ -195,6 +201,8 @@ public <T> T post(final String path, final String bodyContent,
195201
Entity.entity(normalizedBodyContent, contentType);
196202
Builder clientConfigured = configureClient(path, parameters);
197203
return clientConfigured.post(content, type);
204+
} catch (ServerErrorException exception) {
205+
throw new ImClientServerErrorException(createReponseError(exception));
198206
} catch (WebApplicationException exception) {
199207
throw new ImClientErrorException(createReponseError(exception));
200208
}
@@ -225,6 +233,8 @@ public <T> T put(final String path, final String bodyContent,
225233
Entity.entity(normalizedBodyContent, contentType);
226234
Builder clientConfigured = configureClient(path, parameters);
227235
return clientConfigured.put(content, type);
236+
} catch (ServerErrorException exception) {
237+
throw new ImClientServerErrorException(createReponseError(exception));
228238
} catch (WebApplicationException exception) {
229239
throw new ImClientErrorException(createReponseError(exception));
230240
}

src/test/java/es/upv/i3m/grycap/im/rest/client/ImClientTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package es.upv.i3m.grycap.im.rest.client;
1818

1919
import es.upv.i3m.grycap.ImTestWatcher;
20+
import es.upv.i3m.grycap.im.exceptions.ImClientServerErrorException;
2021
import es.upv.i3m.grycap.im.exceptions.ImClientErrorException;
2122
import es.upv.i3m.grycap.im.exceptions.ImClientException;
2223
import es.upv.i3m.grycap.im.pojo.ResponseError;
@@ -34,6 +35,7 @@ public class ImClientTest extends ImTestWatcher {
3435
private static final String AUTH_FILE_PATH = "./src/test/resources/auth.dat";
3536
private static final Integer EXPECTED_ERROR_CODE = 404;
3637
private static final String EXPECTED_ERROR_MESSAGE = "Not found: '/'";
38+
private static final String IM_FAKE_PROVIDER_URL = "http://localhost:1234";
3739

3840
private ImClient getImClient() {
3941
return imClient;
@@ -92,6 +94,22 @@ public void testPutError() throws ImClientException {
9294
}
9395
}
9496

97+
@Test
98+
public void testServerError() throws ImClientException {
99+
try {
100+
imClient = new ImClient(IM_FAKE_PROVIDER_URL, AUTH_FILE_PATH);
101+
} catch (ImClientException exception) {
102+
ImJavaApiLogger.severe(ImClientTest.class, exception.getMessage());
103+
Assert.fail();
104+
}
105+
106+
try {
107+
// Force an error
108+
getImClient().delete("", String.class);
109+
} catch (ImClientServerErrorException exception) {
110+
}
111+
}
112+
95113
private void checkError(ImClientErrorException exception) {
96114
ResponseError error = exception.getResponseError();
97115
Assert.assertEquals(EXPECTED_ERROR_CODE, error.getCode());

0 commit comments

Comments
 (0)