Skip to content

Commit

Permalink
feat: add info from http response to client exception (OpenLineage#2486)
Browse files Browse the repository at this point in the history
Signed-off-by: David Goss <david.goss@matillion.com>
Signed-off-by: Fabio Manganiello <fabio@manganiello.tech>
  • Loading branch information
davidjgoss authored and blacklight committed Apr 4, 2024
1 parent 44553a6 commit 1c77d61
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,8 @@ private void setHeaders(HttpPost request) {
private void throwOnHttpError(@NonNull HttpResponse response) throws IOException {
final int code = response.getStatusLine().getStatusCode();
if (code >= 400 && code < 600) { // non-2xx
String message =
String.format(
"code: %d, response: %s", code, EntityUtils.toString(response.getEntity(), UTF_8));

throw new OpenLineageClientException(message);
throw new HttpTransportResponseException(
code, EntityUtils.toString(response.getEntity(), UTF_8));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
/* Copyright 2018-2024 contributors to the OpenLineage project
/* SPDX-License-Identifier: Apache-2.0
*/

package io.openlineage.client.transports;

import io.openlineage.client.*;
import lombok.*;

/** An exception thrown to indicate a client error relating to a http response. */
@Getter
public class HttpTransportResponseException extends OpenLineageClientException {
private static final long serialVersionUID = 1L;

private final int statusCode;
private final String body;

public HttpTransportResponseException(int statusCode, String body) {
super(String.format("code: %d, response: %s", statusCode, body), null);
this.statusCode = statusCode;
this.body = body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.*;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -149,11 +149,16 @@ void httpTransportRaisesOn500() throws IOException {

CloseableHttpResponse response = mock(CloseableHttpResponse.class, RETURNS_DEEP_STUBS);
when(response.getStatusLine().getStatusCode()).thenReturn(500);
when(response.getEntity()).thenReturn(mock(HttpEntity.class));
when(response.getEntity()).thenReturn(new StringEntity("whoops!", ContentType.TEXT_PLAIN));

when(http.execute(any(HttpUriRequest.class))).thenReturn(response);

assertThrows(OpenLineageClientException.class, () -> client.emit(runEvent()));
HttpTransportResponseException thrown =
assertThrows(HttpTransportResponseException.class, () -> client.emit(runEvent()));
assertThat(thrown.getStatusCode()).isEqualTo(500);
assertThat(thrown.getBody()).isEqualTo("whoops!");
assertThat(thrown.getMessage()).contains("500");
assertThat(thrown.getMessage()).contains("whoops!");

verify(http, times(1)).execute(any());
}
Expand Down

0 comments on commit 1c77d61

Please sign in to comment.