Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(bunq/sdk_csharp#63) add response id to failed request #73

Merged
merged 18 commits into from
Jan 3, 2018

Conversation

OGKevin
Copy link
Contributor

@OGKevin OGKevin commented Dec 30, 2017

Closes #63

  • Tested

@OGKevin OGKevin added this to the 0.12.5 milestone Dec 30, 2017
@OGKevin OGKevin self-assigned this Dec 30, 2017
@bunq bunq deleted a comment Dec 30, 2017
@bunq bunq deleted a comment Dec 30, 2017
@bunq bunq deleted a comment Dec 30, 2017
caughtException = e;
}

Assert.NotNull(caughtException);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will not need this first assert @OGKevin , also it is a good practice to keep unit tests with a single assertion. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this first assertion and caughtException turns out to be null, the code will break on the next line due to NullReferenceException. Rather have the test fails correctly instead of due to an unhandled exception.

The good practice of one assertion per test is not always practical.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a valid point @OGKevin , plus one can say you are being more 'defensive' this way.

);
}

private static string GetResponsId(HttpHeaders allHeader)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in method name


private static string GetResponsId(HttpHeaders allHeader)
{
if (allHeader.Contains(HEADER_RESPONSE_ID_UPPER_CASE))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a method could be extracted here to avoid the conditional expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cant seem to picture how an extracted method will prevent these conditional lines 🤔. Could you please provide a snippet with what you mean ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OGKevin something like:

 private static string GetResponseId(HttpHeaders allHeader, IEnumerable<string> allHeaderResponse)
        {
            foreach (var headerResponse in allHeaderResponse)
            {
                if (allHeader.Contains(headerResponse))
                {
                    return allHeader.GetValues(headerResponse).First();
                }
            }

            throw new BunqException(ERROR_COULD_NOT_DETERMINE_RESPONSE_ID_HEADER);
        }

Actually... the loop brings needed extra complexity, in this case. Maybe it is better to stick with the conditional statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it is indeed better if we stick to the conditional statements! 👍

@@ -4,12 +4,16 @@ namespace Bunq.Sdk.Exception
public class ApiException : System.Exception
{
public int ResponseCode { get; private set; }
public string ResponsId { get; private set; }
Copy link

@kid-cavaquinho kid-cavaquinho Dec 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in property

@OGKevin
Copy link
Contributor Author

OGKevin commented Jan 2, 2018

@epels all yours please 👀

/// <summary>
/// API context to use for the test API calls.
/// </summary>
private static readonly ApiContext API_CONTEXT = GetApiContext();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpperCamelCase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be refactored in #58

/// <summary>
/// Invalid user id to trigger BadRequestException
/// </summary>
private const int INVALID_USER_PERSON_ID = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpperCamelCase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be refactored in #58

[Fact]
public void TestBadRequestWithResponseId()
{
ApiException caughtException = null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Assert.Throws()

@@ -2,7 +2,8 @@
{
public class BadRequestException : ApiException
{
public BadRequestException(int responseCode, string message) : base(responseCode, message)
public BadRequestException(int responseCode, string message, string responseId) :
base(responseCode, message, responseId)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd choose to move the : to the new line (like here)

@@ -18,37 +18,49 @@ public class ExceptionFactory
/// <summary>
/// Glue to concatenate the error messages.
/// </summary>
private const string GLUE_ERROR_MESSAGES = "\n";
private const string SEPARATOR_ERROR_MESSAGES = "\n";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpperCamelCase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be refactored in #58

responseCode,
responseBody,
GetResponseId(responseMessage.Headers)
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unindent the closing parenthese

);
}

private static string GetResponseId(HttpHeaders allHeader)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetResponseIdByAllHeader(/DetermineResponseIdByAllHeader)?

int responseCode,
string responseBody,
string responseId
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

responseCode,
FetchErrorDescriptions(responseBody),
responseId
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unindent closing parenthese

responseCode,
new List<string> {responseBody},
responseId
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unindent closing parenthese

@OGKevin OGKevin force-pushed the bunq/sdk_csharp#63-add-response-id-to-failed-request branch from b855651 to 7cb55ae Compare January 2, 2018 12:24

/// <returns>The exception that belongs to this status code.</returns>
public static ApiException CreateExceptionForResponse(
int responseCode,
IList<string> messages,
IEnumerable<string> messages,
string responseId
)
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this open brace go on the same line as the close parenthese?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is ? 😁

}
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline at EOF

if (allHeader.Contains(HEADER_RESPONSE_ID_UPPER_CASE))
{
return allHeader.GetValues(HEADER_RESPONSE_ID_UPPER_CASE).First();
} else if (allHeader.Contains(HEADER_RESPONSE_ID_LOWER_CASE))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c# code style is rusty, by why the } on same line.

} else if (allHeader.Contains(HEADER_RESPONSE_ID_LOWER_CASE))
{
return allHeader.GetValues(HEADER_RESPONSE_ID_LOWER_CASE).First();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing else

@OGKevin
Copy link
Contributor Author

OGKevin commented Jan 3, 2018

@andrederoos merging!

@OGKevin OGKevin merged commit fbbd9ad into develop Jan 3, 2018
@OGKevin OGKevin deleted the bunq/sdk_csharp#63-add-response-id-to-failed-request branch January 3, 2018 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants