github.com/greenpau/go-authcrunch@v1.1.4/pkg/errors/errors.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package errors 16 17 import ( 18 "errors" 19 "fmt" 20 ) 21 22 // StandardError is a standard error. 23 type StandardError string 24 25 func (e StandardError) Error() string { 26 return string(e) 27 } 28 29 // WithArgs accepts errors with parameters. 30 func (e StandardError) WithArgs(v ...interface{}) error { 31 var hasErr, hasNil bool 32 for _, vv := range v { 33 switch err := vv.(type) { 34 case error: 35 if err == nil { 36 return nil 37 } 38 hasErr = true 39 case nil: 40 hasNil = true 41 } 42 } 43 44 if hasNil && !hasErr { 45 return nil 46 } 47 48 err := advErr{ 49 err: fmt.Errorf("%w", e), 50 v: v, 51 } 52 53 return err 54 } 55 56 // advErr is an error with parameters. 57 type advErr struct { 58 err error 59 v []interface{} 60 } 61 62 // Error returns error string. 63 func (e advErr) Error() string { 64 return fmt.Sprintf(e.err.Error(), e.v...) 65 } 66 67 // Unwrap returns unwrapped error. 68 func (e advErr) Unwrap() error { 69 return errors.Unwrap(e.err) 70 }