Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/main/java/com/microsoft/graph/http/GraphError.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -25,6 +25,7 @@
import com.google.gson.annotations.SerializedName;

import com.microsoft.graph.core.GraphErrorCodes;
import com.google.common.base.CaseFormat;
import com.google.gson.annotations.Expose;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -54,21 +55,30 @@ public class GraphError {

/**
* Determine if the given error code is the one that is expected
*
*
* @param expectedCode the expected error code
* @return <b>true</b> if the error code matches, and <b>false</b> if there was no match
*/
public boolean isError(@Nonnull final GraphErrorCodes expectedCode) {
if (code.equalsIgnoreCase(expectedCode.toString())) {
if (transformErrorCodeCase(code).equalsIgnoreCase(expectedCode.toString())) {
return true;
}
GraphInnerError innerError = innererror;
while (null != innerError) {
if (innerError.code.equalsIgnoreCase(expectedCode.toString())) {
if (transformErrorCodeCase(innerError.code).equalsIgnoreCase(expectedCode.toString())) {
return true;
}
innerError = innerError.innererror;
}
return false;
}
/**
* Transforms the text error code into the format expected by the value of the enum
* @param original original lowser Camel cased error code
* @return the resulting upper undescore cased error code
*/
@Nonnull
protected String transformErrorCodeCase(@Nonnull final String original) {
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, original);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GraphErrorTests {
public void testIsError(){
String expectedMessage = "test error message";
GraphError error = new GraphError();
error.code = GraphErrorCodes.ACCESS_DENIED.toString();
error.code = "accessDenied"; // the code prop is lower camel cased https://docs.microsoft.com/en-us/graph/errors#code-property
error.message = expectedMessage;
assertTrue(error.isError(GraphErrorCodes.ACCESS_DENIED));
assertEquals(expectedMessage, error.message);
Expand Down