Skip to content

Commit

Permalink
Merge pull request #25 from blackducksoftware/ek-improve-exception
Browse files Browse the repository at this point in the history
Ek improve exception
  • Loading branch information
ekerwin authored Mar 26, 2021
2 parents 0796b53 + db176a0 commit 85a4f8c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ plugins { id 'groovy' }
project.ext.moduleName = 'com.synopsys.integration.integration-rest'
project.ext.junitShowStandardStreams = 'true'

version = '7.1.3-SNAPSHOT'
version = '8.0.0-SNAPSHOT'
description = 'A library wrapping http communication for integrations.'

apply plugin: 'com.synopsys.integration.library'

dependencies {
api 'com.synopsys.integration:integration-common:24.2.0'
api 'com.synopsys.integration:integration-common:24.2.1'

api 'org.apache.httpcomponents:httpclient:4.5.13'
api 'org.apache.httpcomponents:httpmime:4.5.13'

testImplementation 'org.codehaus.groovy:groovy-all:2.4.12'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.9.0'
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/synopsys/integration/rest/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
package com.synopsys.integration.rest;

import org.apache.commons.lang3.StringUtils;

public enum HttpMethod {
GET,
PUT,
Expand All @@ -32,4 +34,8 @@ public enum HttpMethod {
OPTIONS,
TRACE;

public static HttpMethod fromMethod(String method) {
return HttpMethod.valueOf(StringUtils.upperCase(method));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,63 @@
package com.synopsys.integration.rest.exception;

import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.rest.HttpMethod;
import com.synopsys.integration.rest.HttpUrl;

public class IntegrationRestException extends IntegrationException {
private static final long serialVersionUID = 1L;

private final HttpMethod httpMethod;
private final HttpUrl httpUrl;
private final int httpStatusCode;
private final String httpStatusMessage;
private final String httpResponseContent;

public IntegrationRestException(int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
public IntegrationRestException(HttpMethod httpMethod, HttpUrl httpUrl, int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
this.httpMethod = httpMethod;
this.httpUrl = httpUrl;
this.httpStatusCode = httpStatusCode;
this.httpStatusMessage = httpStatusMessage;
this.httpResponseContent = httpResponseContent;
}

public IntegrationRestException(int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message, Throwable cause) {
public IntegrationRestException(HttpMethod httpMethod, HttpUrl httpUrl, int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message, Throwable cause) {
super(message, cause);
this.httpMethod = httpMethod;
this.httpUrl = httpUrl;
this.httpStatusCode = httpStatusCode;
this.httpStatusMessage = httpStatusMessage;
this.httpResponseContent = httpResponseContent;
}

public IntegrationRestException(int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message) {
public IntegrationRestException(HttpMethod httpMethod, HttpUrl httpUrl, int httpStatusCode, String httpStatusMessage, String httpResponseContent, String message) {
super(message);
this.httpMethod = httpMethod;
this.httpUrl = httpUrl;
this.httpStatusCode = httpStatusCode;
this.httpStatusMessage = httpStatusMessage;
this.httpResponseContent = httpResponseContent;
}

public IntegrationRestException(int httpStatusCode, String httpStatusMessage, String httpResponseContent, Throwable cause) {
public IntegrationRestException(HttpMethod httpMethod, HttpUrl httpUrl, int httpStatusCode, String httpStatusMessage, String httpResponseContent, Throwable cause) {
super(cause);
this.httpMethod = httpMethod;
this.httpUrl = httpUrl;
this.httpStatusCode = httpStatusCode;
this.httpStatusMessage = httpStatusMessage;
this.httpResponseContent = httpResponseContent;
}

public HttpMethod getHttpMethod() {
return httpMethod;
}

public HttpUrl getHttpUrl() {
return httpUrl;
}

public int getHttpStatusCode() {
return httpStatusCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.apache.http.impl.client.CloseableHttpClient;

import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.rest.HttpMethod;
import com.synopsys.integration.rest.HttpUrl;
import com.synopsys.integration.rest.RestConstants;
import com.synopsys.integration.rest.exception.IntegrationRestException;

Expand Down Expand Up @@ -210,8 +212,17 @@ public void throwExceptionForError() throws IntegrationRestException {
}

String messageFormat = "There was a problem trying to %s %s, response was %s %s%s.";
String message = String.format(messageFormat, request.getMethod(), request.getURI().toString(), statusCode, statusCodeDescription, reasonPhraseDescription);
throw new IntegrationRestException(statusCode, statusMessage, httpResponseContent, message);
HttpMethod httpMethod = HttpMethod.fromMethod(request.getMethod());
//ejk - seems unlikey that we'd get here without a valid url...
HttpUrl httpUrl = null;
try {
httpUrl = new HttpUrl(request.getURI());
} catch (IntegrationException ignored) {
//ignored
}

String message = String.format(messageFormat, httpMethod, httpUrl.string(), statusCode, statusCodeDescription, reasonPhraseDescription);
throw new IntegrationRestException(httpMethod, httpUrl, statusCode, statusMessage, httpResponseContent, message);
}
}

Expand Down

0 comments on commit 85a4f8c

Please sign in to comment.