ocket8888 opened a new pull request #5162:
URL: https://github.com/apache/trafficcontrol/pull/5162


   ## What does this PR (Pull Request) do?
   - [x] This PR is not related to any Issue
   
   This adds a new, fairly small package to Traffic Ops: `apierrors`. It 
exposes a structure for containing a user-facing error, a system-only error, 
and an HTTP response code, like so:
   
   ```go
   type Errors struct {
        Code        int
        SystemError error
        UserError   error
   }
   ```
   
   This is meant to replace the `(error, error, int)` and `(int, error, error)` 
call signatures common throughout TO code. In fact, this PR changes such 
signatures to unify them all to return just that `Errors` structure instead. As 
well as making the TO codebase more compliant with the convention that "error 
values should be last function return signatures" - which is followed 
throughout the Go standard library, but is not actually enforced within our 
project - this eliminates any confusion about which error in the return 
signature is meant for clients and which is only meant for logs. It also makes 
it harder to make mistakes regarding those associations by making the 
assignment and use of such errors more explicit.
   
   It also adds helper functions similar to those that already exist for the 
old, "exploded" format of passing in all three bits of information separately, 
which allows one to change comparatively complex error handling into something 
much simpler:
   ```go
   // The old way
   inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
   if userErr != nil || sysErr != nil {
        api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
   }
   
   // The new way
   inf, errs := api.NewInfo(r, nil, nil)
   if errs.Occurred() {
        inf.HandleErrs(w, r, errs)
        return
   }
   ```
   
   ## Which Traffic Control components are affected by this PR?
   - Traffic Ops
   
   ## What is the best way to verify this PR?
   All existing API and unit tests should pass. This PR adds a few tests 
related to the new `Errors` structure, as well.
   
   ## The following criteria are ALL met by this PR
   - [x] This PR includes tests
   - [x] This PR includes documentation in the form of GoDocs - nothing 
external has changed, so no other documentation is necessary
   - [x] An update to CHANGELOG.md is not necessary for refactors
   - [x] This PR includes any and all required license headers
   - [x] This PR does not include a database migration
   - [x] This PR **DOES NOT FIX A SERIOUS SECURITY VULNERABILITY**


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to