You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/12/28 18:15:50 UTC

[GitHub] dangogh commented on a change in pull request #3157: Added error context helper to help with tests. Also wrote unit tests for it.

dangogh commented on a change in 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#discussion_r244377319
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/test/error_context.go
 ##########
 @@ -0,0 +1,214 @@
+/*
+
+   Licensed 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.
+*/
+
+package test
+
+import "fmt"
+
+type errorCode struct {
+	error
+	cause error
+	code  int
+}
+
+type ErrorCoder interface {
+	Error() string
+	ErrorCode() int
+	Cause() error
+	Prepend(string, ...interface{}) ErrorCoder
+}
+
+func NewError(code int, fmtStr string, fmtArgs ...interface{}) ErrorCoder {
+	err := fmt.Errorf(fmtStr, fmtArgs...)
+	return &errorCode{err, err, code}
+}
+
+func (e errorCode) ErrorCode() int {
+	return e.code
+}
+
+// not a pointer receiver, does not modify itself
+func (e errorCode) Prepend(fmtStr string, fmtArgs ...interface{}) ErrorCoder {
+	err := fmt.Errorf(fmtStr, fmtArgs...)
+	e.error = fmt.Errorf("%v %v", err, e.error)
+	return e
+}
+
+func (e errorCode) Cause() error {
+	return e.cause
+}
+
+func AddErrorCode(code int, err error) ErrorCoder {
+	return NewError(code, "%v", err)
+}
+
+// ErrorContext
+// contains a list of all error codes (a whitelist)
+//		- allows user to make sure they are creating the correct errors
+//		- actually a map
+//			lookup can be done without linear search
+//			we can use the map to keep count of which errors are made
+// contains mapping from error code to name (either for testing metainfo or used in case no args are given)
+//		not required for all error codes, or for any
+//
+// context.NewError was made to improve upon test.NewError
+type ErrorContext struct {
+	calledNewError  bool
+	createdNewError bool
+	doPanic         bool
+	name            string
+	codes           map[uint]uint
 
 Review comment:
   had to trace thru the code to figure out where the whitelist came from, then saw the comment above.  Should this be called "whitelist" to make that apparent?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services