Skip to content

Commit 17798c7

Browse files
committed
Added VndErrorException
1 parent 4e1e5ed commit 17798c7

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* 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+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* 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+
package org.springframework.hateoas;
17+
18+
import org.springframework.http.HttpHeaders;
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.web.client.HttpStatusCodeException;
21+
22+
import java.nio.charset.Charset;
23+
24+
/**
25+
* A base exception used for propagating the {@link VndErrors} received from remote service call.
26+
*
27+
* @author Jakub Narloch
28+
* @see VndErrors
29+
*/
30+
public class VndErrorException extends HttpStatusCodeException {
31+
32+
/**
33+
* The vnd errors.
34+
*/
35+
private final VndErrors vndErrors;
36+
37+
/**
38+
* Creates new instance of {@link VndErrorException} with status code and vnd errors.
39+
*
40+
* @param statusCode the status code
41+
* @param vndErrors the vnd errors
42+
*/
43+
public VndErrorException(HttpStatus statusCode, VndErrors vndErrors) {
44+
super(statusCode);
45+
this.vndErrors = vndErrors;
46+
}
47+
48+
/**
49+
* Creates new instance of {@link VndErrorException} with status code and vnd errors.
50+
*
51+
* @param statusCode the status code
52+
* @param statusText the status text
53+
* @param vndErrors the vnd errors
54+
*/
55+
public VndErrorException(HttpStatus statusCode, String statusText, VndErrors vndErrors) {
56+
super(statusCode, statusText);
57+
this.vndErrors = vndErrors;
58+
}
59+
60+
/**
61+
* Creates new instance of {@link VndErrorException} with status code, response body and vnd errors.
62+
*
63+
* @param statusCode the status code
64+
* @param statusText the status text
65+
* @param responseBody the response body
66+
* @param responseCharset the response charset
67+
* @param vndErrors the vnd errors
68+
*/
69+
public VndErrorException(HttpStatus statusCode, String statusText, byte[] responseBody, Charset responseCharset,
70+
VndErrors vndErrors) {
71+
super(statusCode, statusText, responseBody, responseCharset);
72+
this.vndErrors = vndErrors;
73+
}
74+
75+
/**
76+
* Creates new instance of {@link VndErrorException} with status code, http headers, response body and vnd errors.
77+
*
78+
* @param statusCode the status code
79+
* @param statusText the status text
80+
* @param responseHeaders the response headers
81+
* @param responseBody the response body
82+
* @param responseCharset the response charset
83+
* @param vndErrors the vnd errors
84+
*/
85+
public VndErrorException(HttpStatus statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody,
86+
Charset responseCharset, VndErrors vndErrors) {
87+
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
88+
this.vndErrors = vndErrors;
89+
}
90+
91+
/**
92+
* Retrieves the vnd errors.
93+
*
94+
* @return the vnd errors
95+
*/
96+
public VndErrors getVndErrors() {
97+
return vndErrors;
98+
}
99+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* 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+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* 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+
package org.springframework.hateoas;
17+
18+
import org.junit.Test;
19+
20+
import java.nio.charset.Charset;
21+
22+
import static org.hamcrest.CoreMatchers.is;
23+
import static org.hamcrest.CoreMatchers.notNullValue;
24+
import static org.junit.Assert.assertThat;
25+
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
26+
27+
/**
28+
* Tests the {@link VndErrorException}
29+
*
30+
* @author Jakub Narloch
31+
*/
32+
public class VndErrorExceptionTest {
33+
34+
private static final String LOGREF = "5814b624-a66d-4ef3-9bfa-2b0d2a7baaf9";
35+
36+
private static final String UNEXPECTED = "Unexpected";
37+
38+
private VndErrorException exception;
39+
40+
@Test
41+
public void shouldCreateVndErrorWithStatus() {
42+
43+
exception = new VndErrorException(INTERNAL_SERVER_ERROR, new VndErrors(LOGREF, UNEXPECTED));
44+
45+
assertThat(exception.getStatusCode(), is(INTERNAL_SERVER_ERROR));
46+
assertThat(exception.getVndErrors(), is(notNullValue()));
47+
VndErrors.VndError vndError = exception.getVndErrors().iterator().next();
48+
assertThat(vndError.getLogref(), is(LOGREF));
49+
assertThat(vndError.getMessage(), is(UNEXPECTED));
50+
}
51+
52+
@Test
53+
public void shouldCreateVndErrorWithStatusText() {
54+
55+
exception = new VndErrorException(INTERNAL_SERVER_ERROR, "500", new VndErrors(LOGREF, UNEXPECTED));
56+
57+
assertThat(exception.getStatusCode(), is(INTERNAL_SERVER_ERROR));
58+
assertThat(exception.getStatusText(), is("500"));
59+
assertThat(exception.getVndErrors(), is(notNullValue()));
60+
VndErrors.VndError vndError = exception.getVndErrors().iterator().next();
61+
assertThat(vndError.getLogref(), is(LOGREF));
62+
assertThat(vndError.getMessage(), is(UNEXPECTED));
63+
}
64+
65+
@Test
66+
public void shouldCreateVndErrorWithStatusTextAndBody() throws Exception {
67+
68+
exception = new VndErrorException(INTERNAL_SERVER_ERROR, "500", UNEXPECTED.getBytes("UTF-8"),
69+
Charset.forName("UTF-8"), new VndErrors(LOGREF, UNEXPECTED));
70+
71+
assertThat(exception.getStatusCode(), is(INTERNAL_SERVER_ERROR));
72+
assertThat(exception.getStatusText(), is("500"));
73+
assertThat(exception.getResponseBodyAsString(), is(UNEXPECTED));
74+
assertThat(exception.getVndErrors(), is(notNullValue()));
75+
VndErrors.VndError vndError = exception.getVndErrors().iterator().next();
76+
assertThat(vndError.getLogref(), is(LOGREF));
77+
assertThat(vndError.getMessage(), is(UNEXPECTED));
78+
}
79+
}

0 commit comments

Comments
 (0)