Skip to content

Commit 1e9e652

Browse files
authored
Merge pull request #135 from microsoftgraph/bugfix/error-code-casing
- fixes an issue where the SDK would be expecting the wrong casing for the gaph error codes
2 parents 947d384 + 5b1a6db commit 1e9e652

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

‎src/main/java/com/microsoft/graph/http/GraphError.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// ------------------------------------------------------------------------------
22
// Copyright (c) 2017 Microsoft Corporation
3-
//
3+
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
88
// copies of the Software, and to permit persons to whom the Software is
99
// furnished to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in
1212
// all copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -25,6 +25,7 @@
2525
import com.google.gson.annotations.SerializedName;
2626

2727
import com.microsoft.graph.core.GraphErrorCodes;
28+
import com.google.common.base.CaseFormat;
2829
import com.google.gson.annotations.Expose;
2930

3031
import javax.annotation.Nullable;
@@ -54,21 +55,30 @@ public class GraphError {
5455

5556
/**
5657
* Determine if the given error code is the one that is expected
57-
*
58+
*
5859
* @param expectedCode the expected error code
5960
* @return <b>true</b> if the error code matches, and <b>false</b> if there was no match
6061
*/
6162
public boolean isError(@Nonnull final GraphErrorCodes expectedCode) {
62-
if (code.equalsIgnoreCase(expectedCode.toString())) {
63+
if (transformErrorCodeCase(code).equalsIgnoreCase(expectedCode.toString())) {
6364
return true;
6465
}
6566
GraphInnerError innerError = innererror;
6667
while (null != innerError) {
67-
if (innerError.code.equalsIgnoreCase(expectedCode.toString())) {
68+
if (transformErrorCodeCase(innerError.code).equalsIgnoreCase(expectedCode.toString())) {
6869
return true;
6970
}
7071
innerError = innerError.innererror;
7172
}
7273
return false;
7374
}
75+
/**
76+
* Transforms the text error code into the format expected by the value of the enum
77+
* @param original original lowser Camel cased error code
78+
* @return the resulting upper undescore cased error code
79+
*/
80+
@Nonnull
81+
protected String transformErrorCodeCase(@Nonnull final String original) {
82+
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, original);
83+
}
7484
}

‎src/test/java/com/microsoft/graph/http/GraphErrorTests.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class GraphErrorTests {
1515
public void testIsError(){
1616
String expectedMessage = "test error message";
1717
GraphError error = new GraphError();
18-
error.code = GraphErrorCodes.ACCESS_DENIED.toString();
18+
error.code = "accessDenied"; // the code prop is lower camel cased https://docs.microsoft.com/en-us/graph/errors#code-property
1919
error.message = expectedMessage;
2020
assertTrue(error.isError(GraphErrorCodes.ACCESS_DENIED));
2121
assertEquals(expectedMessage, error.message);

0 commit comments

Comments
 (0)