Skip to content

Commit 98ce7ea

Browse files
author
Caitlin Bales
committed
Update error logging to use ILogger's settings
Logic was included to include the full HTTP response from the API if the verbose flag was set, but there was no way to set this flag by default. Now, the response handler will use the client's logger level to determine what level of response to include. In this instance, full HTTP responses are included in the log if the logger level is set to DEBUG.
1 parent ad4f746 commit 98ce7ea

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public ChunkedUploadResult generateResult(
108108

109109
return new ChunkedUploadResult(
110110
GraphServiceException.createFromConnection(request, null, serializer,
111-
connection));
111+
connection, logger));
112112
}
113113
} finally {
114114
if (in != null) {

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
331331
}
332332
} catch (final GraphServiceException ex) {
333333
final boolean shouldLogVerbosely = logger.getLoggingLevel() == LoggerLevel.DEBUG;
334-
logger.logError("OneDrive Service exception " + ex.getMessage(shouldLogVerbosely), ex);
334+
logger.logError("Graph ervice exception " + ex.getMessage(shouldLogVerbosely), ex);
335335
throw ex;
336336
} catch (final Exception ex) {
337337
final ClientException clientException = new ClientException("Error during http request",
@@ -355,7 +355,7 @@ private <Body> void handleErrorResponse(final IHttpRequest request,
355355
final IConnection connection)
356356
throws IOException {
357357
throw GraphServiceException.createFromConnection(request, serializable, serializer,
358-
connection);
358+
connection, logger);
359359
}
360360

361361
/**

src/main/java/com/microsoft/graph/http/GraphFatalServiceException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ protected GraphFatalServiceException(final String method,
4747
final int responseCode,
4848
final String responseMessage,
4949
final List<String> responseHeaders,
50-
final GraphErrorResponse error) {
51-
super(method, url, requestHeaders, requestBody, responseCode, responseMessage, responseHeaders, error);
50+
final GraphErrorResponse error,
51+
final boolean verbose) {
52+
super(method, url, requestHeaders, requestBody, responseCode, responseMessage, responseHeaders, error, verbose);
5253
}
5354

5455
@Override
5556
public String getMessage(final boolean verbose) {
5657
//no inspection StringBufferReplaceableByString
5758
final StringBuilder sb = new StringBuilder();
5859
sb.append("Unexpected exception returned from the service.")
59-
.append(super.getMessage(true));
60+
.append(super.getMessage(verbose));
6061
return sb.toString();
6162
}
6263
}

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.google.gson.GsonBuilder;
3333
import com.microsoft.graph.core.ClientException;
3434
import com.microsoft.graph.core.GraphErrorCodes;
35+
import com.microsoft.graph.logger.ILogger;
36+
import com.microsoft.graph.logger.LoggerLevel;
3537
import com.microsoft.graph.options.HeaderOption;
3638
import com.microsoft.graph.serializer.ISerializer;
3739

@@ -104,6 +106,11 @@ public class GraphServiceException extends ClientException {
104106
* The response headers.
105107
*/
106108
private final List<String> responseHeaders;
109+
110+
/**
111+
* Whether to log the full error response.
112+
*/
113+
private final boolean verbose;
107114

108115
/**
109116
* Create a Graph service exception.
@@ -116,6 +123,7 @@ public class GraphServiceException extends ClientException {
116123
* @param responseMessage The response message.
117124
* @param responseHeaders The response headers.
118125
* @param error The error response if available.
126+
* @param verbose The error response log level.
119127
*/
120128
protected GraphServiceException(final String method,
121129
final String url,
@@ -124,7 +132,8 @@ protected GraphServiceException(final String method,
124132
final int responseCode,
125133
final String responseMessage,
126134
final List<String> responseHeaders,
127-
final GraphErrorResponse error) {
135+
final GraphErrorResponse error,
136+
final boolean verbose) {
128137
super(responseMessage, null);
129138
this.method = method;
130139
this.url = url;
@@ -134,11 +143,12 @@ protected GraphServiceException(final String method,
134143
this.responseMessage = responseMessage;
135144
this.responseHeaders = responseHeaders;
136145
this.error = error;
146+
this.verbose = verbose;
137147
}
138148

139149
@Override
140150
public String getMessage() {
141-
return getMessage(false);
151+
return getMessage(verbose);
142152
}
143153

144154
/**
@@ -232,26 +242,32 @@ public GraphError getServiceError() {
232242
public static <T> GraphServiceException createFromConnection(final IHttpRequest request,
233243
final T serializable,
234244
final ISerializer serializer,
235-
final IConnection connection)
245+
final IConnection connection,
246+
final ILogger logger)
236247
throws IOException {
237248
final String method = connection.getRequestMethod();
238249
final String url = request.getRequestUrl().toString();
239250
final List<String> requestHeaders = new LinkedList<>();
240251
for (final HeaderOption option : request.getHeaders()) {
241252
requestHeaders.add(option.getName() + " : " + option.getValue());
242253
}
254+
boolean isVerbose = logger.getLoggingLevel() == LoggerLevel.DEBUG;
243255
final String requestBody;
244256
if (serializable instanceof byte[]) {
245257
final byte[] bytes = (byte[]) serializable;
246258
StringBuilder sb = new StringBuilder();
247259
sb.append("byte[").append(bytes.length).append("]");
248260

249261
sb.append(" {");
250-
for (int i = 0; i < MAX_BYTE_COUNT_BEFORE_TRUNCATION && i < bytes.length; i++) {
251-
sb.append(bytes[i]).append(", ");
252-
}
253-
if (bytes.length > MAX_BYTE_COUNT_BEFORE_TRUNCATION) {
254-
sb.append(TRUNCATION_MARKER).append("}");
262+
if (isVerbose) {
263+
sb.append(bytes);
264+
} else {
265+
for (int i = 0; i < MAX_BYTE_COUNT_BEFORE_TRUNCATION && i < bytes.length; i++) {
266+
sb.append(bytes[i]).append(", ");
267+
}
268+
if (bytes.length > MAX_BYTE_COUNT_BEFORE_TRUNCATION) {
269+
sb.append(TRUNCATION_MARKER).append("}");
270+
}
255271
}
256272
requestBody = sb.toString();
257273
} else if (serializable != null) {
@@ -295,7 +311,8 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
295311
responseCode,
296312
responseMessage,
297313
responseHeaders,
298-
error);
314+
error,
315+
isVerbose);
299316
}
300317

301318
return new GraphServiceException(method,
@@ -305,6 +322,7 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
305322
responseCode,
306323
responseMessage,
307324
responseHeaders,
308-
error);
325+
error,
326+
isVerbose);
309327
}
310328
}

src/test/java/com/microsoft/graph/http/GraphFatalServiceExceptionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class GraphFatalServiceExceptionTests {
1212

1313
@Test
1414
public void testGraphFatalServiceException() {
15-
GraphFatalServiceException exception = new GraphFatalServiceException(null,null,new ArrayList<String>(),null,401,"Unauthenticated",new ArrayList<String>(),null);
15+
GraphFatalServiceException exception = new GraphFatalServiceException(null,null,new ArrayList<String>(),null,401,"Unauthenticated",new ArrayList<String>(),null, false);
1616
String expectedMessage = "Unexpected exception returned from the service." +
1717
"null null\n" +
1818
"\n" +

src/test/java/com/microsoft/graph/http/GraphServiceExceptionTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.Test;
1313

1414
import com.microsoft.graph.core.GraphErrorCodes;
15+
import com.microsoft.graph.logger.DefaultLogger;
1516

1617
public class GraphServiceExceptionTests {
1718

@@ -21,7 +22,7 @@ public void testError() {
2122
GraphError error = new GraphError();
2223
error.code = GraphErrorCodes.UNAUTHENTICATED.toString();
2324
errorResponse.error = error;
24-
GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),null,401,"Unauthorized",new ArrayList<String>(),errorResponse);
25+
GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),null,401,"Unauthorized",new ArrayList<String>(),errorResponse, false);
2526
String message = exception.getMessage();
2627
assertTrue(message.indexOf("Error code: UNAUTHENTICATED") == 0);
2728
assertTrue(message.indexOf("401 : Unauthorized") > 0);
@@ -45,6 +46,7 @@ public void testVerboseError() {
4546

4647
@Test
4748
public void testCreateFromConnection() {
49+
DefaultLogger logger = new DefaultLogger();
4850
GraphServiceException exception = null;
4951
Boolean success = false;
5052
Boolean failure = false;
@@ -65,7 +67,7 @@ public Map<String, String> getHeaders() {
6567
}
6668
};
6769
try{
68-
exception = GraphServiceException.createFromConnection(new MockHttpRequest(),null,null,new MockConnection(data){});
70+
exception = GraphServiceException.createFromConnection(new MockHttpRequest(),null,null,new MockConnection(data){},logger);
6971
success = true;
7072
}catch (IOException ex){
7173
failure = true;

0 commit comments

Comments
 (0)