Skip to content

Commit b32a90f

Browse files
committed
First attempt at adding EXPath HTTP Client Error Codes
Closes #22
1 parent ddb0758 commit b32a90f

22 files changed

+281
-146
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/****************************************************************************/
2+
/* File: org.expath.ExpathConstants.java */
3+
/* Author: A. Retter - adamretter.org.uk */
4+
/* Date: 2024-06-18 */
5+
/* Tags: */
6+
/* Copyright (c) 2024 Adam Retter (see end of file.) */
7+
/* ------------------------------------------------------------------------ */
8+
9+
10+
package org.expath;
11+
12+
/**
13+
* Centralize some constants.
14+
*
15+
* @author Adam Retter
16+
*/
17+
public class ExpathConstants {
18+
public final static String ERR_NS_PREFIX = "experr";
19+
public final static String ERR_NS_URI = "http://expath.org/ns/error";
20+
}
21+
22+
23+
/* ------------------------------------------------------------------------ */
24+
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
25+
/* */
26+
/* The contents of this file are subject to the Mozilla Public License */
27+
/* Version 1.0 (the "License"); you may not use this file except in */
28+
/* compliance with the License. You may obtain a copy of the License at */
29+
/* http://www.mozilla.org/MPL/. */
30+
/* */
31+
/* Software distributed under the License is distributed on an "AS IS" */
32+
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
33+
/* the License for the specific language governing rights and limitations */
34+
/* under the License. */
35+
/* */
36+
/* The Original Code is: all this file. */
37+
/* */
38+
/* The Initial Developer of the Original Code is Adam Retter. */
39+
/* */
40+
/* Contributor(s): none. */
41+
/* ------------------------------------------------------------------------ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/****************************************************************************/
2+
/* File: org.expath.ExpathError.java */
3+
/* Author: A. Retter - adamretter.org.uk */
4+
/* Date: 2024-06-18 */
5+
/* Tags: */
6+
/* Copyright (c) 2024 Adam Retter (see end of file.) */
7+
/* ------------------------------------------------------------------------ */
8+
9+
10+
package org.expath;
11+
12+
import javax.xml.namespace.QName;
13+
14+
/**
15+
* An EXPath Error.
16+
*/
17+
public interface ExpathError {
18+
19+
/**
20+
* Get the qualified name of the EXPath error.
21+
*
22+
* @return the name.
23+
*/
24+
QName getName();
25+
26+
/**
27+
* Get the description of the EXPath error.
28+
*
29+
* @return the description.
30+
*/
31+
String getDescription();
32+
}
33+
34+
35+
/* ------------------------------------------------------------------------ */
36+
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
37+
/* */
38+
/* The contents of this file are subject to the Mozilla Public License */
39+
/* Version 1.0 (the "License"); you may not use this file except in */
40+
/* compliance with the License. You may obtain a copy of the License at */
41+
/* http://www.mozilla.org/MPL/. */
42+
/* */
43+
/* Software distributed under the License is distributed on an "AS IS" */
44+
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
45+
/* the License for the specific language governing rights and limitations */
46+
/* under the License. */
47+
/* */
48+
/* The Original Code is: all this file. */
49+
/* */
50+
/* The Initial Developer of the Original Code is Adam Retter. */
51+
/* */
52+
/* Contributor(s): none. */
53+
/* ------------------------------------------------------------------------ */

http-client-java/src/main/java/org/expath/httpclient/ContentType.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public ContentType(final String type, final String charset, final String boundar
4848
} else {
4949
// get the charset from the header or the default
5050
if (header == null || !"Content-Type".equalsIgnoreCase(header.getName())) {
51-
throw new HttpClientException("Header is not content type");
51+
throw new HttpClientException(HttpClientError.HC001, "Header is not content type");
5252
}
5353
final HeaderElement[] headerElements = header.getElements();
5454
if (headerElements.length > 1) {
55-
throw new HttpClientException("Multiple Content-Type headers");
55+
throw new HttpClientException(HttpClientError.HC001, "Multiple Content-Type headers");
5656
}
5757

5858
final NameValuePair headerCharset = headerElements[0].getParameterByName("charset");
@@ -69,11 +69,11 @@ public ContentType(final String type, final String charset, final String boundar
6969
} else {
7070
// get the boundary from the header or null
7171
if (header == null || !"Content-Type".equalsIgnoreCase(header.getName())) {
72-
throw new HttpClientException("Header is not content type");
72+
throw new HttpClientException(HttpClientError.HC001, "Header is not content type");
7373
}
7474
final HeaderElement[] headerElements = header.getElements();
7575
if (headerElements.length > 1) {
76-
throw new HttpClientException("Multiple Content-Type headers");
76+
throw new HttpClientException(HttpClientError.HC001, "Multiple Content-Type headers");
7777
}
7878

7979
final NameValuePair headerBoundary = headerElements[0].getParameterByName("boundary");
@@ -87,12 +87,12 @@ public ContentType(final String type, final String charset, final String boundar
8787
}
8888

8989
if (!"Content-Type".equalsIgnoreCase(header.getName())) {
90-
throw new HttpClientException("Header is not Content-Type");
90+
throw new HttpClientException(HttpClientError.HC001, "Header is not Content-Type");
9191
}
9292

9393
final HeaderElement[] headerElements = header.getElements();
9494
if (headerElements.length > 1) {
95-
throw new HttpClientException("Multiple Content-Type headers");
95+
throw new HttpClientException(HttpClientError.HC001, "Multiple Content-Type headers");
9696
}
9797

9898
type = extractMediaTypeFromContentType(header.getValue());

http-client-java/src/main/java/org/expath/httpclient/HeaderSet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public HeaderSet(Header[] headers)
4747
throws HttpClientException
4848
{
4949
if ( headers == null ) {
50-
throw new HttpClientException("Headers array is null");
50+
throw new HttpClientException(HttpClientError.HC005, "Headers array is null");
5151
}
5252
myHeaders = new ArrayList<Header>(headers.length);
5353
myHeaders.addAll(Arrays.asList(headers));
@@ -63,7 +63,7 @@ public HeaderSet(Collection<Header> headers)
6363
throws HttpClientException
6464
{
6565
if ( headers == null ) {
66-
throw new HttpClientException("Headers list is null");
66+
throw new HttpClientException(HttpClientError.HC005, "Headers list is null");
6767
}
6868
myHeaders = new ArrayList<Header>(headers);
6969
}

http-client-java/src/main/java/org/expath/httpclient/HttpClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private Result doSendRequest(Result result, Element request, String href, Sequen
103103
return sendOnce(result, uri, req, parser);
104104
}
105105
catch ( URISyntaxException ex ) {
106-
throw new HttpClientException("Href is not valid: " + req.getHref(), ex);
106+
throw new HttpClientException(HttpClientError.HC002, "Href is not valid: " + req.getHref(), ex);
107107
}
108108
}
109109

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/****************************************************************************/
2+
/* File: org.expath.httpclient.HttpClientError.java */
3+
/* Author: A. Retter - adamretter.org.uk */
4+
/* Date: 2024-06-18 */
5+
/* Tags: */
6+
/* Copyright (c) 2024 Adam Retter (see end of file.) */
7+
/* ------------------------------------------------------------------------ */
8+
9+
10+
package org.expath.httpclient;
11+
12+
import org.expath.ExpathConstants;
13+
import org.expath.ExpathError;
14+
15+
import javax.xml.namespace.QName;
16+
17+
/**
18+
* An error as defined by the EXPath HTTP Client specification.
19+
*/
20+
public enum HttpClientError implements ExpathError {
21+
HC001("HC01", "An HTTP error occurred"),
22+
HC002("HC02", "Error parsing the entity content as XML or HTML."),
23+
HC003("HC03", "With a multipart response, the override-media-type must be either a multipart media type or application/octet-stream."),
24+
HC004("HC04", "The src attribute on the body element is mutually exclusive with all other attribute (except the media-type)."),
25+
HC005("HC05", "The request element is not valid."),
26+
HC006("HC06", "A timeout occurred waiting for the response.");
27+
28+
private final QName name;
29+
private final String description;
30+
31+
HttpClientError(final String code, final String description) {
32+
this.name = new QName(ExpathConstants.ERR_NS_URI, code, ExpathConstants.ERR_NS_PREFIX);
33+
this.description = description;
34+
}
35+
36+
@Override
37+
public QName getName() {
38+
return name;
39+
}
40+
41+
@Override
42+
public String getDescription() {
43+
return description;
44+
}
45+
}
46+
47+
48+
/* ------------------------------------------------------------------------ */
49+
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
50+
/* */
51+
/* The contents of this file are subject to the Mozilla Public License */
52+
/* Version 1.0 (the "License"); you may not use this file except in */
53+
/* compliance with the License. You may obtain a copy of the License at */
54+
/* http://www.mozilla.org/MPL/. */
55+
/* */
56+
/* Software distributed under the License is distributed on an "AS IS" */
57+
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
58+
/* the License for the specific language governing rights and limitations */
59+
/* under the License. */
60+
/* */
61+
/* The Original Code is: all this file. */
62+
/* */
63+
/* The Initial Developer of the Original Code is Adam Retter. */
64+
/* */
65+
/* Contributor(s): none. */
66+
/* ------------------------------------------------------------------------ */

http-client-java/src/main/java/org/expath/httpclient/HttpClientException.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.expath.httpclient;
1111

12+
1213
/**
1314
* Generic exception for the EXPath HTTP Client implementation in Java.
1415
*
@@ -17,14 +18,27 @@
1718
public class HttpClientException
1819
extends Exception
1920
{
20-
public HttpClientException(String msg)
21+
private final HttpClientError httpClientError;
22+
23+
public HttpClientException(final HttpClientError httpClientError, final String message)
2124
{
22-
super(msg);
25+
super(message);
26+
this.httpClientError = httpClientError;
2327
}
2428

25-
public HttpClientException(String msg, Throwable cause)
29+
public HttpClientException(final HttpClientError httpClientError, final String message, final Throwable cause)
2630
{
27-
super(msg, cause);
31+
super(message, cause);
32+
this.httpClientError = httpClientError;
33+
}
34+
35+
/**
36+
* Return the HTTP Client error.
37+
*
38+
* @return the HTTP Client error.
39+
*/
40+
public HttpClientError getHttpClientError() {
41+
return httpClientError;
2842
}
2943
}
3044

http-client-java/src/main/java/org/expath/httpclient/HttpRequestBody.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public HttpRequestBody(Element elem)
4040
myElem = elem;
4141
myContentType = myElem.getAttribute("media-type");
4242
if ( myContentType == null ) {
43-
throw new HttpClientException("@media-type is not on the body or multipart element");
43+
throw new HttpClientException(HttpClientError.HC005, "@media-type is not on the body or multipart element");
4444
}
4545
}
4646

http-client-java/src/main/java/org/expath/httpclient/HttpResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void outputResponseElement(TreeBuilder b)
6363
b.endElem();
6464
}
6565
catch ( ToolsException ex ) {
66-
throw new HttpClientException("Error building the response", ex);
66+
throw new HttpClientException(HttpClientError.HC002, "Error building the response", ex);
6767
}
6868
}
6969

0 commit comments

Comments
 (0)