mirror of
https://github.com/nabbar/golib.git
synced 2025-10-05 15:56:50 +08:00
Refactor package errors + packages names :
- Refactor ErrorType, list errors managment, codeError - Add interface Error with error interface implement - Add type CodeError assign typiclly to const that represent code of error - Add func to registry func to retrieve message from an uint16 codeError (typicaly a switch of each codeError const) - Add default errorCode with default errorMessage if no one code or message is found - Add modeError to manage how to manage compatibility between Error interface and error interface - Add Error interface that allow parent link (parent as error or Error interface), code and trace management - Add trace finder to allow find func/file/line caller when Error is call - Add http 2 transport in httpcli - Add http 2 transport in httpserver - Add function to get client http with timeout management in httpcli - Add function to get Error if occurs of http client in httpcli - Add test for smtp package - Chg return error by returning Error in all packages - Chg package njs-archive by archive - Chg package njs-certif by certificates - Chg package njs-console by console - Chg package njs-crypt by crypt - Chg package njs-errors by errors - Chg package njs-httpcli by httpcli - Chg package njs-httpserver by httpserver - Chg package njs-ioutils by ioutils - Chg package njs-ldap by ldap - Chg package njs-logger by logger - Chg package njs-password by password - Chg package njs-progress by progress - Chg package njs-router by router - Chg package njs-semaphore by semaphore - Chg package njs-smtp by smtp - Chg package njs-static by static - Chg package njs-status by status - Chg package njs-version by version - Fix dependancies gopkg by github/go-ldap for go module compatibility - Fix gin Abort call by gin Abort with Error in static package - Fix issue #18 in status package : replace partner by component - Fix go vet error - Del deprecated function - Del useless function & files - Bump dependancies - Apply CHG in README.md
This commit is contained in:
397
errors/errors.go
Normal file
397
errors/errors.go
Normal file
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package errors
|
||||
|
||||
import (
|
||||
errs "errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultGlue = ", "
|
||||
defaultPattern = "[Error #%s] %s"
|
||||
defaultPatternTrace = "[Error #%s] %s (%s)"
|
||||
)
|
||||
|
||||
func SetDefaultGlue(glue string) {
|
||||
defaultGlue = glue
|
||||
}
|
||||
|
||||
func GetDefaultGlue() string {
|
||||
return defaultGlue
|
||||
}
|
||||
|
||||
func SetDefaultPattern(pattern string) {
|
||||
defaultPattern = pattern
|
||||
}
|
||||
|
||||
func GetDefaultPattern() string {
|
||||
return defaultPattern
|
||||
}
|
||||
|
||||
func SetDefaultPatternTrace(patternTrace string) {
|
||||
defaultPatternTrace = patternTrace
|
||||
}
|
||||
|
||||
func GetDefaultPatternTrace() string {
|
||||
return defaultPatternTrace
|
||||
}
|
||||
|
||||
type errors struct {
|
||||
c uint16
|
||||
e string
|
||||
p []Error
|
||||
t runtime.Frame
|
||||
}
|
||||
|
||||
type Error interface {
|
||||
IsCodeError(code CodeError) bool
|
||||
HasCodeError(code CodeError) bool
|
||||
|
||||
IsError(e error) bool
|
||||
HasError(err error) bool
|
||||
|
||||
AddParent(parent ...error)
|
||||
SetParent(parent ...error)
|
||||
AddParentError(parent ...Error)
|
||||
SetParentError(parent ...Error)
|
||||
|
||||
Code() string
|
||||
CodeFull(glue string) string
|
||||
CodeSlice() []string
|
||||
|
||||
CodeError(pattern string) string
|
||||
CodeErrorFull(pattern, glue string) string
|
||||
CodeErrorSlice(pattern string) []string
|
||||
|
||||
CodeErrorTrace(pattern string) string
|
||||
CodeErrorTraceFull(pattern, glue string) string
|
||||
CodeErrorTraceSlice(pattern string) []string
|
||||
|
||||
Error() string
|
||||
|
||||
StringError() string
|
||||
StringErrorFull(glue string) string
|
||||
StringErrorSlice() []string
|
||||
|
||||
GetError() error
|
||||
GetErrorFull(glue string) error
|
||||
GetErrorSlice() []error
|
||||
|
||||
GetIError() Error
|
||||
GetIErrorSlice() []Error
|
||||
|
||||
GetTrace() string
|
||||
GetTraceSlice() []string
|
||||
}
|
||||
|
||||
func MakeErrorIfError(err ...Error) Error {
|
||||
var e Error = nil
|
||||
|
||||
for _, p := range err {
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
if e == nil {
|
||||
e = p
|
||||
} else {
|
||||
e.AddParentError(p)
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func NewError(code uint16, message string, parent Error) Error {
|
||||
if parent == nil {
|
||||
parent = &errors{
|
||||
c: 0,
|
||||
e: "",
|
||||
p: make([]Error, 0),
|
||||
}
|
||||
}
|
||||
|
||||
return &errors{
|
||||
c: code,
|
||||
e: message,
|
||||
p: parent.GetIErrorSlice(),
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorIferror(code uint16, message string, parent error) Error {
|
||||
if parent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
p := make([]Error, 0)
|
||||
p = append(p, &errors{
|
||||
c: 0,
|
||||
e: parent.Error(),
|
||||
p: nil,
|
||||
})
|
||||
|
||||
return &errors{
|
||||
c: code,
|
||||
e: message,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorIfError(code uint16, message string, parent Error) Error {
|
||||
if parent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &errors{
|
||||
c: code,
|
||||
e: message,
|
||||
p: parent.GetIErrorSlice(),
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *errors) AddParent(parent ...error) {
|
||||
for _, v := range parent {
|
||||
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
e.p = append(e.p, &errors{
|
||||
c: 0,
|
||||
e: v.Error(),
|
||||
p: nil,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *errors) IsCodeError(code CodeError) bool {
|
||||
return e.c == code.GetUint16()
|
||||
}
|
||||
|
||||
func (e *errors) IsError(err error) bool {
|
||||
return e.e == err.Error()
|
||||
}
|
||||
|
||||
func (e *errors) HasCodeError(code CodeError) bool {
|
||||
if e.IsCodeError(code) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, p := range e.p {
|
||||
if p.IsCodeError(code) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *errors) HasError(err error) bool {
|
||||
if e.IsError(err) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, p := range e.p {
|
||||
if p.IsError(err) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *errors) SetParent(parent ...error) {
|
||||
e.p = make([]Error, 0)
|
||||
e.AddParent(parent...)
|
||||
}
|
||||
|
||||
func (e *errors) AddParentError(parent ...Error) {
|
||||
e.p = append(e.p, parent...)
|
||||
}
|
||||
|
||||
func (e *errors) SetParentError(parent ...Error) {
|
||||
e.p = parent
|
||||
}
|
||||
|
||||
func (e *errors) Code() string {
|
||||
return strconv.Itoa(int(e.c))
|
||||
}
|
||||
|
||||
func (e *errors) CodeFull(glue string) string {
|
||||
if glue == "" {
|
||||
glue = defaultGlue
|
||||
}
|
||||
|
||||
return strings.Join(e.CodeSlice(), glue)
|
||||
}
|
||||
|
||||
func (e *errors) CodeSlice() []string {
|
||||
var r = []string{e.Code()}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.Code())
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) Error() string {
|
||||
return modeError.error(e)
|
||||
}
|
||||
|
||||
func (e *errors) StringError() string {
|
||||
return e.e
|
||||
}
|
||||
|
||||
func (e *errors) StringErrorFull(glue string) string {
|
||||
if glue == "" {
|
||||
glue = defaultGlue
|
||||
}
|
||||
|
||||
return strings.Join(e.StringErrorSlice(), glue)
|
||||
}
|
||||
|
||||
func (e *errors) StringErrorSlice() []string {
|
||||
var r = []string{e.Error()}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.Error())
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) GetError() error {
|
||||
return errs.New(e.e)
|
||||
}
|
||||
|
||||
func (e *errors) GetErrorFull(glue string) error {
|
||||
return errs.New(e.StringErrorFull(glue))
|
||||
}
|
||||
|
||||
func (e *errors) GetErrorSlice() []error {
|
||||
var r = []error{e.GetError()}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.GetError())
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) GetIError() Error {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *errors) GetIErrorSlice() []Error {
|
||||
var r = []Error{e}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.GetIError())
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) GetTrace() string {
|
||||
if e.t.File != "" {
|
||||
return fmt.Sprintf("%s#%d", e.t.File, e.t.Line)
|
||||
} else if e.t.Function != "" {
|
||||
return fmt.Sprintf("%s#%d", e.t.Function, e.t.Line)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (e *errors) GetTraceSlice() []string {
|
||||
var r = []string{e.GetTrace()}
|
||||
|
||||
for _, v := range e.p {
|
||||
if t := v.GetTrace(); t != "" {
|
||||
r = append(r, v.GetTrace())
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) CodeError(pattern string) string {
|
||||
if pattern == "" {
|
||||
pattern = defaultPattern
|
||||
}
|
||||
return fmt.Sprintf(pattern, e.Code(), e.Error())
|
||||
}
|
||||
|
||||
func (e *errors) CodeErrorFull(pattern, glue string) string {
|
||||
if glue == "" {
|
||||
glue = defaultGlue
|
||||
}
|
||||
|
||||
return strings.Join(e.CodeErrorSlice(pattern), glue)
|
||||
}
|
||||
|
||||
func (e *errors) CodeErrorSlice(pattern string) []string {
|
||||
var r = []string{e.CodeError(pattern)}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.CodeError(pattern))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (e *errors) CodeErrorTrace(pattern string) string {
|
||||
if pattern == "" {
|
||||
pattern = defaultPatternTrace
|
||||
}
|
||||
|
||||
return fmt.Sprintf(pattern, e.Code(), e.GetTrace(), e.Error())
|
||||
}
|
||||
|
||||
func (e *errors) CodeErrorTraceFull(pattern, glue string) string {
|
||||
if glue == "" {
|
||||
glue = defaultGlue
|
||||
}
|
||||
|
||||
return strings.Join(e.CodeErrorTraceSlice(pattern), glue)
|
||||
}
|
||||
|
||||
func (e *errors) CodeErrorTraceSlice(pattern string) []string {
|
||||
var r = []string{e.CodeErrorTrace(pattern)}
|
||||
|
||||
for _, v := range e.p {
|
||||
r = append(r, v.CodeErrorTrace(pattern))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
Reference in New Issue
Block a user