- 
                Notifications
    
You must be signed in to change notification settings  - Fork 56
 
Clean up exception handling in StashApiClient #109
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
Conversation
| 
           Improved tests for loading PRs and PR comments from multiple pages to avoid some repetition.  | 
    
        
          
                src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClientTest.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/test/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClientTest.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      
          
 How about we use POJOs and then serialize them into Json with Jackson etc.? For simple objects (I don't expect anything else) the transformation should be obvious. Another idea would be to go for spring-cloud-contract, too bad Stash doesn't publish it's API contracts (or does it?)  | 
    
          
 I don't think is would be simpler than what we have now. 
 A quick Google search suggests that they exist, but no indication where they are. Again, I don't expect it to be a simplification. I'm going to test standalone JSON files first.  | 
    
| 
           Added support for all public methods except  It turns out WireMock has good support for configuring through JSON files, I'm going to try it.  | 
    
| 
           Converted multi-page tests to JSON files. Using dynamic port for HTTP. There default is port 8080, which can conflict with Jenkins. Using   | 
    
| 
           This PR has achieved 91.9% code coverage for  One problem is that only "easy HTTPS" is being tested, but not HTTP or real HTTPS. I believe JUnit has a way to run tests with multiple parameters, I'll look into that. In any case, unit testing real HTTPS can be tricky. The unit tests have discovered multiple issues. I documented them in the code. 
 Applying the first commit from #68 on top of this PR (i.e. upgrading the HTTP client) breaks several tests. On the positive side, some errors are not ignored anymore. On the negative side,  I think the best course of action would be to completely remove   | 
    
| 
           I've added a commit to clean up exception handling. It would be better to update the HTTP client if we have a complete picture which exceptions are thrown and what code is handling them. I didn't try to use perfect messages in the exceptions, I only wanted then to be better than what it is now. I'm trying to keep this PR short so it can be reviewed and merged quickly.  | 
    
        
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildListener.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java
          
            Show resolved
            Hide resolved
        
      | 
           It turns out I removed stack traces in many places, please don't merge this PR until I fix that issue.  | 
    
| 
           Should be good to merge now.  | 
    
| 
           I added comments everywhere   | 
    
        
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildListener.java
          
            Show resolved
            Hide resolved
        
              
          
                src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
           Added unit tests for   | 
    
| 
           Added unit tests for the changes to   | 
    
| 
           Added checks for authentication, we really don't want to break it.  | 
    
Document discovered issues.
Stop using RuntimeException, it's an unchecked exception. The callers may be unaware that they need to handle it. Stop using Exception. Always use most specific exception types. Always rethrow caught exceptions by wrapping them in StashApiClient. Let the callers handle them, as they are in a better position to chose what to do and emit meaningful logs. Don't print stack trace in StashApiClient. Print it when the exception is handled. Some callers log the stack trace to the build log, not to the global Jenkins log. Don't log error messages and throw StashApiException at the same time, unless the log message contains information that is not added to the exception. Adjust "throws" declarations. Catch errors where appropriate and log them. Document functions that throw StashApiException. Add comments explaining how StashApiException is handled. Adjust unit tests for the new behavior.
The first commit adds WireMock based unit tests for the entire
StashApiClient.java. In some cases, the tests indicate issues in the code under test. Those issues are documented.The second commit changed exception handling in StashApiClient. Only specific checked exceptions are used,
RuntimeExceptionandExceptionare banned. The callers are changes to handle the exceptions. That code was gettingRuntimeExceptionand ignored it. Now it's forces to deal with it properly.