moltzaum opened a new pull request #3157: Added error context helper to help 
with tests. Also wrote unit tests for it.
URL: https://github.com/apache/trafficcontrol/pull/3157
 
 
   #### What does this PR do?
   
   I plan on using this PR in some tests I am writing. It creates a new type of 
error that has an error code associated with it. There is also an 
'ErrorContext', which makes sure you don't accidentally create errors with 
codes that have no meaning. The ErrorContext also keeps statistics, which gives 
a nice metric for negative test coverage.
   
   #### Which TC components are affected by this PR?
   
   This affects nothing yet. Later it will affect new tests.
   I have no plans to include this with traffic ops or other components.
   
   #### What is the best way to verify this PR?
   
   Run the unit test and read the code. I am hoping it is somewhat 
understandable from reading the code, however it might be a little more obscure 
since I'm only running negative tests and not showing how I'd use it. Below are 
a few snippets of snippets + pseudocode showing how I might use it.
   
   ```golang
   package cache_config
   
   const (
       COMMON_BASE            = 10
       NOT_ENOUGH_ASSIGNMENTS = iota + COMMON_BASE
       BAD_ASSIGNMENT_MATCH   = iota + COMMON_BASE
   )
   
   const (
       A_BASE                  = 40
       INVALID_ACTION_LABEL    = iota + A_BASE
       INVALID_ACTION          = iota + A_BASE
       INVALID_RESP_TO_COOKIES = iota + A_BASE
       INVALID_PIN_IN_CACHE    = iota + A_BASE
       INVALID_REVALIDATE      = iota + A_BASE
       INVALID_TTL_IN_CACHE    = iota + A_BASE
   )
   
   // scoped to the package name
   var ErrorContext *test.ErrorContext
   
   func init() {
       // *_BASE constants are not error codes in themselves, but can be used to
       // determine if an error code exists within a certain 'class' of errors
       errorCodes := []uint{
           INVALID_ACTION_LABEL,
           INVALID_ACTION,
           INVALID_RESP_TO_COOKIES,
           INVALID_PIN_IN_CACHE,
           INVALID_REVALIDATE,
           INVALID_TTL_IN_CACHE,
           NOT_ENOUGH_ASSIGNMENTS,
           BAD_ASSIGNMENT_MATCH,
       }
       // Not many mappings are made since most error messages have additional 
information
       ErrorContext = test.NewErrorContext("cache config", iterableErrorCodes)
       ErrorContext.AddMapping(NOT_ENOUGH_ASSIGNMENTS, "not enough assignments 
in rule")
       ErrorContext.TurnPanicOn()
   }
   
   ```
   
   ```golang
   package cache_config
   
   func matchAssignmentThenDoXXX(token string) {
       if token does not contain '=' {
           return ErrorContext.NewError(BAD_ASSIGNMENT_MATCH, "could not match 
yyy assignment")
       }
       // do XXX with match result
   }
   
   func matchRuleThenDoXXX(config string) {
   
       if tokens in config is < 2 {
           return ErrorContext.NewError(NOT_ENOUGH_ASSIGNMENTS)
       }
       matchAssignmentThenDoXXX( config_tokens[0] )
       // continue...
   }
   
   ```
   
   Currently the only documentation I have is in the comments. To whoever is 
reading this, do you think it needs a README?
   
   #### Check all that apply
   
   - [x] This PR includes tests
   - [ ] This PR includes documentation updates
   - [ ] This PR includes an update to CHANGELOG.md
   - [x] This PR includes all required license headers
   - [ ] This PR includes a database migration (ensure that migration sequence 
is correct)
   - [ ] This PR fixes a serious security flaw. Read more: 
[www.apache.org/security](http://www.apache.org/security/)
   
   <!--
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at
   
         http://www.apache.org/licenses/LICENSE-2.0
   
       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
   -->
   
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to