mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00
Package Errors:
- fix bug with fmt.Errorf called withtou pattern, replace it by a errors.New - fix but of circular inclusion for method Add - remove deprecated const / code - reorganize code file to map to interface / model - update other modules following chnages Other: - bump dependencies
This commit is contained in:
@@ -52,8 +52,6 @@ func init() {
|
||||
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case liberr.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamEmpty:
|
||||
return "given parameters is empty"
|
||||
case ErrorFileStat:
|
||||
|
@@ -68,8 +68,6 @@ func init() {
|
||||
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case liberr.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamsEmpty:
|
||||
return "at least one given parameter is empty"
|
||||
case ErrorParamsMissing:
|
||||
@@ -112,5 +110,5 @@ func getMessage(code liberr.CodeError) (message string) {
|
||||
return "cluster engine config seems to be invalid"
|
||||
}
|
||||
|
||||
return ""
|
||||
return liberr.NullMessage
|
||||
}
|
||||
|
@@ -67,8 +67,6 @@ func init() {
|
||||
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case liberr.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamEmpty:
|
||||
return "given parameters is empty"
|
||||
case ErrorConfigMissingViper:
|
||||
|
@@ -39,11 +39,8 @@ var idMsgFct = make(map[CodeError]Message)
|
||||
type Message func(code CodeError) (message string)
|
||||
type CodeError uint16
|
||||
|
||||
const UNK_ERROR CodeError = 0
|
||||
const UnknownError CodeError = 0
|
||||
const UNK_MESSAGE = "unknown error"
|
||||
const UnknownMessage = "unknown error"
|
||||
const NUL_MESSAGE = ""
|
||||
const NullMessage = ""
|
||||
|
||||
func (c CodeError) GetUint16() uint16 {
|
||||
|
61
errors/compat.go
Normal file
61
errors/compat.go
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
var (
|
||||
defaultPattern = "[Error #%d] %s"
|
||||
defaultPatternTrace = "[Error #%d] %s (%s)"
|
||||
)
|
||||
|
||||
// GetDefaultPatternTrace define the pattern to be used for string of error with code.
|
||||
// The pattern is fmt pattern with 2 inputs in order : code, message.
|
||||
func SetDefaultPattern(pattern string) {
|
||||
defaultPattern = pattern
|
||||
}
|
||||
|
||||
// GetDefaultPattern return the current pattern used for string of error with code.
|
||||
// The pattern is fmt pattern with 2 inputs in order : code, message.
|
||||
func GetDefaultPattern() string {
|
||||
return defaultPattern
|
||||
}
|
||||
|
||||
// SetDefaultPatternTrace define the pattern to be used for string of error with code and trace.
|
||||
// The pattern is fmt pattern with 3 inputs in order : code, message, trace.
|
||||
func SetDefaultPatternTrace(patternTrace string) {
|
||||
defaultPatternTrace = patternTrace
|
||||
}
|
||||
|
||||
// GetDefaultPatternTrace return the current pattern used for string of error with code and trace.
|
||||
// The pattern is fmt pattern with 3 inputs in order : code, message, trace.
|
||||
func GetDefaultPatternTrace() string {
|
||||
return defaultPatternTrace
|
||||
}
|
||||
|
||||
// SetTracePathFilter customize the filter apply to filepath on trace.
|
||||
func SetTracePathFilter(path string) {
|
||||
filterPkg = path
|
||||
}
|
346
errors/errors.go
346
errors/errors.go
@@ -33,40 +33,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultPattern = "[Error #%d] %s"
|
||||
defaultPatternTrace = "[Error #%d] %s (%s)"
|
||||
)
|
||||
|
||||
// GetDefaultPatternTrace define the pattern to be used for string of error with code.
|
||||
// The pattern is fmt pattern with 2 inputs in order : code, message.
|
||||
func SetDefaultPattern(pattern string) {
|
||||
defaultPattern = pattern
|
||||
}
|
||||
|
||||
// GetDefaultPattern return the current pattern used for string of error with code.
|
||||
// The pattern is fmt pattern with 2 inputs in order : code, message.
|
||||
func GetDefaultPattern() string {
|
||||
return defaultPattern
|
||||
}
|
||||
|
||||
// SetDefaultPatternTrace define the pattern to be used for string of error with code and trace.
|
||||
// The pattern is fmt pattern with 3 inputs in order : code, message, trace.
|
||||
func SetDefaultPatternTrace(patternTrace string) {
|
||||
defaultPatternTrace = patternTrace
|
||||
}
|
||||
|
||||
// GetDefaultPatternTrace return the current pattern used for string of error with code and trace.
|
||||
// The pattern is fmt pattern with 3 inputs in order : code, message, trace.
|
||||
func GetDefaultPatternTrace() string {
|
||||
return defaultPatternTrace
|
||||
}
|
||||
|
||||
// SetTracePathFilter customize the filter apply to filepath on trace.
|
||||
func SetTracePathFilter(path string) {
|
||||
filterPkg = path
|
||||
}
|
||||
|
||||
type ers struct {
|
||||
c uint16
|
||||
e string
|
||||
@@ -74,273 +40,50 @@ type ers struct {
|
||||
t runtime.Frame
|
||||
}
|
||||
|
||||
type FuncMap func(e error) bool
|
||||
|
||||
type Error interface {
|
||||
//IsCode check if the given error code is matching with the current Error
|
||||
IsCode(code CodeError) bool
|
||||
//HasCode check if current error or parent has the given error code
|
||||
HasCode(code CodeError) bool
|
||||
//GetCode return the CodeError value of the current error
|
||||
GetCode() CodeError
|
||||
//GetParentCode return a slice of CodeError value of all parent Error and the code of the current Error
|
||||
GetParentCode() []CodeError
|
||||
|
||||
//IsError check if the given error params is a valid error and not a nil pointer
|
||||
IsError(e error) bool
|
||||
//HasError check if the given error in params is still in parent error
|
||||
HasError(err error) bool
|
||||
//HasParent check if the current Error has any valid parent
|
||||
HasParent() bool
|
||||
//GetParent return a slice of Error interface for each parent error with or without the first error.
|
||||
GetParent(withMainError bool) []error
|
||||
//Map run a function on each func and parent. If the function return false, the loop stop.
|
||||
Map(fct FuncMap) bool
|
||||
//ContainsString return true if any message into the main error or the parent message error contains the given part string
|
||||
ContainsString(s string) bool
|
||||
|
||||
//Add will add all no empty given error into parent of the current Error pointer
|
||||
Add(parent ...error)
|
||||
//SetParent will replace all parent with the given error list
|
||||
SetParent(parent ...error)
|
||||
|
||||
//Code is used to return the code of current Error, as string
|
||||
Code() uint16
|
||||
//CodeSlice is used to return a slice string of all code of current Error (main and parent)
|
||||
CodeSlice() []uint16
|
||||
|
||||
//CodeError is used to return a composed string of current Error code with message, for current Error and no parent
|
||||
CodeError(pattern string) string
|
||||
//CodeErrorSlice is used to return a composed string slice of couple error code with message, for current Error and all parent
|
||||
CodeErrorSlice(pattern string) []string
|
||||
|
||||
//CodeErrorTrace is used to return a composed string of current Error code with message and trace information, for current Error and no parent
|
||||
CodeErrorTrace(pattern string) string
|
||||
//CodeErrorTraceSlice is used to return a composed string slice of couple error code with message and trace information, for current Error and all parent
|
||||
CodeErrorTraceSlice(pattern string) []string
|
||||
|
||||
//Error is used to match with error interface
|
||||
//this function will return a mixed result depends of the configuration defined by calling SetModeReturnError
|
||||
Error() string
|
||||
|
||||
//StringError is used to return the error message, for current Error and no parent
|
||||
StringError() string
|
||||
//StringErrorSlice is used to return the error message, for current Error and all parent, as a slice of string
|
||||
StringErrorSlice() []string
|
||||
|
||||
//GetError is used to return a new error interface based of the current error (and no parent)
|
||||
GetError() error
|
||||
//GetErrorSlice is used to return a slice of new error interface, based of the current error and all parent
|
||||
GetErrorSlice() []error
|
||||
|
||||
//GetTrace will return a comped string for the trace of the current Error
|
||||
GetTrace() string
|
||||
//GetTrace will return a slice of comped string fpr the trace of the current Error and all parent
|
||||
GetTraceSlice() []string
|
||||
|
||||
//Return will transform the current Error into a given pointer that implement the Return interface
|
||||
Return(r Return)
|
||||
//ReturnError will send the current Error value to the given function ReturnError
|
||||
ReturnError(f ReturnError)
|
||||
//ReturnParent will send all parent information of the current Error value to the given function ReturnError
|
||||
ReturnParent(f ReturnError)
|
||||
}
|
||||
|
||||
type Errors interface {
|
||||
// ErrorsLast return the last registered error
|
||||
ErrorsLast() error
|
||||
|
||||
// ErrorsList return a slice of all registered errors
|
||||
ErrorsList() []error
|
||||
}
|
||||
|
||||
func Is(e error) bool {
|
||||
var err Error
|
||||
return errors.As(e, &err)
|
||||
}
|
||||
|
||||
func Get(e error) Error {
|
||||
var err Error
|
||||
|
||||
if errors.As(e, &err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Has(e error, code CodeError) bool {
|
||||
if err := Get(e); err == nil {
|
||||
func (e *ers) is(err *ers) bool {
|
||||
if e == nil || err == nil {
|
||||
return false
|
||||
} else {
|
||||
return err.HasCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
func ContainsString(e error, s string) bool {
|
||||
if e == nil {
|
||||
var (
|
||||
ss = e.GetTrace()
|
||||
sd = err.GetTrace()
|
||||
ts = len(ss) > 0
|
||||
td = len(sd) > 0
|
||||
)
|
||||
|
||||
if (ts || td) && !(ts && td) { // XOR Trace Source & Destination != 0
|
||||
return false
|
||||
} else if err := Get(e); err == nil {
|
||||
return strings.Contains(e.Error(), s)
|
||||
} else {
|
||||
return err.ContainsString(s)
|
||||
} else if ts && td {
|
||||
return strings.EqualFold(ss, sd)
|
||||
}
|
||||
}
|
||||
|
||||
func IsCode(e error, code CodeError) bool {
|
||||
if err := Get(e); err == nil {
|
||||
ss = e.Error()
|
||||
sd = err.Error()
|
||||
ts = len(ss) > 0
|
||||
td = len(sd) > 0
|
||||
|
||||
if (ts || td) && !(ts && td) { // XOR Message Source & Destination != 0
|
||||
return false
|
||||
} else {
|
||||
return err.IsCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
func Make(e error) Error {
|
||||
var err Error
|
||||
|
||||
if e == nil {
|
||||
return nil
|
||||
} else if errors.As(e, &err) {
|
||||
return err
|
||||
} else {
|
||||
return &ers{
|
||||
c: 0,
|
||||
e: e.Error(),
|
||||
p: nil,
|
||||
t: getNilFrame(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MakeIfError(err ...error) Error {
|
||||
var e Error = nil
|
||||
|
||||
for _, p := range err {
|
||||
if p == nil {
|
||||
continue
|
||||
} else if e == nil {
|
||||
e = Make(p)
|
||||
} else {
|
||||
e.Add(p)
|
||||
}
|
||||
} else if ts && td {
|
||||
return strings.EqualFold(ss, sd)
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
var (
|
||||
cs = e.Code()
|
||||
cd = err.Code()
|
||||
)
|
||||
|
||||
func AddOrNew(errMain, errSub error, parent ...error) Error {
|
||||
var e Error
|
||||
ts = cs > 0
|
||||
td = cd > 0
|
||||
|
||||
if errMain != nil {
|
||||
if e = Get(errMain); e == nil {
|
||||
e = New(0, errMain.Error())
|
||||
}
|
||||
e.Add(errSub)
|
||||
e.Add(parent...)
|
||||
return e
|
||||
} else if errSub != nil {
|
||||
return New(0, errSub.Error(), parent...)
|
||||
if (ts || td) && !(ts && td) { // XOR Message Source & Destination != 0
|
||||
return false
|
||||
} else if ts && td {
|
||||
return cs != cd
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(code uint16, message string, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: code,
|
||||
e: message,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorTrace(code int, msg string, file string, line int, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: uint16(code),
|
||||
e: msg,
|
||||
p: p,
|
||||
t: runtime.Frame{
|
||||
File: file,
|
||||
Line: line,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorRecovered(msg string, recovered string, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if recovered != "" {
|
||||
p = append(p, &ers{
|
||||
c: 0,
|
||||
e: recovered,
|
||||
p: nil,
|
||||
})
|
||||
}
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range getFrameVendor() {
|
||||
if t == getNilFrame() {
|
||||
continue
|
||||
}
|
||||
msg += "\n " + fmt.Sprintf("Fct: %s - File: %s - Line: %d", t.Function, t.File, t.Line)
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: 0,
|
||||
e: msg,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func IfError(code uint16, message string, parent ...error) Error {
|
||||
p := make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(p) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: code,
|
||||
e: message,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *ers) Add(parent ...error) {
|
||||
@@ -349,7 +92,20 @@ func (e *ers) Add(parent ...error) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err, ok := v.(Error); !ok {
|
||||
var (
|
||||
ok bool
|
||||
er *ers
|
||||
err Error
|
||||
)
|
||||
|
||||
if er, ok = v.(*ers); ok {
|
||||
// prevent circular addition
|
||||
if e.is(er) {
|
||||
continue
|
||||
} else {
|
||||
e.p = append(e.p, er)
|
||||
}
|
||||
} else if err, ok = v.(Error); !ok {
|
||||
e.p = append(e.p, &ers{
|
||||
c: 0,
|
||||
e: v.Error(),
|
||||
@@ -506,16 +262,22 @@ func (e *ers) StringErrorSlice() []string {
|
||||
|
||||
func (e *ers) GetError() error {
|
||||
//nolint goerr113
|
||||
return fmt.Errorf(e.e)
|
||||
return errors.New(e.e)
|
||||
}
|
||||
|
||||
func (e *ers) GetErrorSlice() []error {
|
||||
var r = []error{e.GetError()}
|
||||
|
||||
if len(e.p) < 1 {
|
||||
return r
|
||||
}
|
||||
|
||||
for _, v := range e.p {
|
||||
for _, s := range v.GetErrorSlice() {
|
||||
r = append(r, s)
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
r = append(r, v.GetErrorSlice()...)
|
||||
}
|
||||
|
||||
return r
|
||||
|
327
errors/interface.go
Normal file
327
errors/interface.go
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* 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 (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type FuncMap func(e error) bool
|
||||
|
||||
type ReturnError func(code int, msg string, file string, line int)
|
||||
|
||||
type Error interface {
|
||||
//IsCode check if the given error code is matching with the current Error
|
||||
IsCode(code CodeError) bool
|
||||
//HasCode check if current error or parent has the given error code
|
||||
HasCode(code CodeError) bool
|
||||
//GetCode return the CodeError value of the current error
|
||||
GetCode() CodeError
|
||||
//GetParentCode return a slice of CodeError value of all parent Error and the code of the current Error
|
||||
GetParentCode() []CodeError
|
||||
|
||||
//IsError check if the given error params is a valid error and not a nil pointer
|
||||
IsError(e error) bool
|
||||
//HasError check if the given error in params is still in parent error
|
||||
HasError(err error) bool
|
||||
//HasParent check if the current Error has any valid parent
|
||||
HasParent() bool
|
||||
//GetParent return a slice of Error interface for each parent error with or without the first error.
|
||||
GetParent(withMainError bool) []error
|
||||
//Map run a function on each func and parent. If the function return false, the loop stop.
|
||||
Map(fct FuncMap) bool
|
||||
//ContainsString return true if any message into the main error or the parent message error contains the given part string
|
||||
ContainsString(s string) bool
|
||||
|
||||
//Add will add all no empty given error into parent of the current Error pointer
|
||||
Add(parent ...error)
|
||||
//SetParent will replace all parent with the given error list
|
||||
SetParent(parent ...error)
|
||||
|
||||
//Code is used to return the code of current Error, as string
|
||||
Code() uint16
|
||||
//CodeSlice is used to return a slice string of all code of current Error (main and parent)
|
||||
CodeSlice() []uint16
|
||||
|
||||
//CodeError is used to return a composed string of current Error code with message, for current Error and no parent
|
||||
CodeError(pattern string) string
|
||||
//CodeErrorSlice is used to return a composed string slice of couple error code with message, for current Error and all parent
|
||||
CodeErrorSlice(pattern string) []string
|
||||
|
||||
//CodeErrorTrace is used to return a composed string of current Error code with message and trace information, for current Error and no parent
|
||||
CodeErrorTrace(pattern string) string
|
||||
//CodeErrorTraceSlice is used to return a composed string slice of couple error code with message and trace information, for current Error and all parent
|
||||
CodeErrorTraceSlice(pattern string) []string
|
||||
|
||||
//Error is used to match with error interface
|
||||
//this function will return a mixed result depends of the configuration defined by calling SetModeReturnError
|
||||
Error() string
|
||||
|
||||
//StringError is used to return the error message, for current Error and no parent
|
||||
StringError() string
|
||||
//StringErrorSlice is used to return the error message, for current Error and all parent, as a slice of string
|
||||
StringErrorSlice() []string
|
||||
|
||||
//GetError is used to return a new error interface based of the current error (and no parent)
|
||||
GetError() error
|
||||
//GetErrorSlice is used to return a slice of new error interface, based of the current error and all parent
|
||||
GetErrorSlice() []error
|
||||
|
||||
//GetTrace will return a comped string for the trace of the current Error
|
||||
GetTrace() string
|
||||
//GetTrace will return a slice of comped string fpr the trace of the current Error and all parent
|
||||
GetTraceSlice() []string
|
||||
|
||||
//Return will transform the current Error into a given pointer that implement the Return interface
|
||||
Return(r Return)
|
||||
//ReturnError will send the current Error value to the given function ReturnError
|
||||
ReturnError(f ReturnError)
|
||||
//ReturnParent will send all parent information of the current Error value to the given function ReturnError
|
||||
ReturnParent(f ReturnError)
|
||||
}
|
||||
|
||||
type Errors interface {
|
||||
// ErrorsLast return the last registered error
|
||||
ErrorsLast() error
|
||||
|
||||
// ErrorsList return a slice of all registered errors
|
||||
ErrorsList() []error
|
||||
}
|
||||
|
||||
type Return interface {
|
||||
SetError(code int, msg string, file string, line int)
|
||||
AddParent(code int, msg string, file string, line int)
|
||||
JSON() []byte
|
||||
}
|
||||
|
||||
type ReturnGin interface {
|
||||
Return
|
||||
|
||||
GinTonicAbort(ctx *gin.Context, httpCode int)
|
||||
GinTonicErrorAbort(ctx *gin.Context, httpCode int)
|
||||
}
|
||||
|
||||
func Is(e error) bool {
|
||||
var err Error
|
||||
return errors.As(e, &err)
|
||||
}
|
||||
|
||||
func Get(e error) Error {
|
||||
var err Error
|
||||
|
||||
if errors.As(e, &err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Has(e error, code CodeError) bool {
|
||||
if err := Get(e); err == nil {
|
||||
return false
|
||||
} else {
|
||||
return err.HasCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
func ContainsString(e error, s string) bool {
|
||||
if e == nil {
|
||||
return false
|
||||
} else if err := Get(e); err == nil {
|
||||
return strings.Contains(e.Error(), s)
|
||||
} else {
|
||||
return err.ContainsString(s)
|
||||
}
|
||||
}
|
||||
|
||||
func IsCode(e error, code CodeError) bool {
|
||||
if err := Get(e); err == nil {
|
||||
return false
|
||||
} else {
|
||||
return err.IsCode(code)
|
||||
}
|
||||
}
|
||||
|
||||
func Make(e error) Error {
|
||||
var err Error
|
||||
|
||||
if e == nil {
|
||||
return nil
|
||||
} else if errors.As(e, &err) {
|
||||
return err
|
||||
} else {
|
||||
return &ers{
|
||||
c: 0,
|
||||
e: e.Error(),
|
||||
p: nil,
|
||||
t: getNilFrame(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MakeIfError(err ...error) Error {
|
||||
var e Error = nil
|
||||
|
||||
for _, p := range err {
|
||||
if p == nil {
|
||||
continue
|
||||
} else if e == nil {
|
||||
e = Make(p)
|
||||
} else {
|
||||
e.Add(p)
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func AddOrNew(errMain, errSub error, parent ...error) Error {
|
||||
var e Error
|
||||
|
||||
if errMain != nil {
|
||||
if e = Get(errMain); e == nil {
|
||||
e = New(0, errMain.Error())
|
||||
}
|
||||
e.Add(errSub)
|
||||
e.Add(parent...)
|
||||
return e
|
||||
} else if errSub != nil {
|
||||
return New(0, errSub.Error(), parent...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(code uint16, message string, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: code,
|
||||
e: message,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorTrace(code int, msg string, file string, line int, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: uint16(code),
|
||||
e: msg,
|
||||
p: p,
|
||||
t: runtime.Frame{
|
||||
File: file,
|
||||
Line: line,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorRecovered(msg string, recovered string, parent ...error) Error {
|
||||
var p = make([]Error, 0)
|
||||
|
||||
if recovered != "" {
|
||||
p = append(p, &ers{
|
||||
c: 0,
|
||||
e: recovered,
|
||||
p: nil,
|
||||
})
|
||||
}
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range getFrameVendor() {
|
||||
if t == getNilFrame() {
|
||||
continue
|
||||
}
|
||||
msg += "\n " + fmt.Sprintf("Fct: %s - File: %s - Line: %d", t.Function, t.File, t.Line)
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: 0,
|
||||
e: msg,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func IfError(code uint16, message string, parent ...error) Error {
|
||||
p := make([]Error, 0)
|
||||
|
||||
if len(parent) > 0 {
|
||||
for _, e := range parent {
|
||||
if er := Make(e); er != nil {
|
||||
p = append(p, er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(p) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ers{
|
||||
c: code,
|
||||
e: message,
|
||||
p: p,
|
||||
t: getFrame(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewDefaultReturn() *DefaultReturn {
|
||||
return &DefaultReturn{
|
||||
Code: "",
|
||||
Message: "",
|
||||
}
|
||||
}
|
@@ -35,33 +35,12 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ReturnError func(code int, msg string, file string, line int)
|
||||
|
||||
type Return interface {
|
||||
SetError(code int, msg string, file string, line int)
|
||||
AddParent(code int, msg string, file string, line int)
|
||||
}
|
||||
|
||||
type ReturnGin interface {
|
||||
Return
|
||||
|
||||
GinTonicAbort(ctx *gin.Context, httpCode int)
|
||||
GinTonicErrorAbort(ctx *gin.Context, httpCode int)
|
||||
}
|
||||
|
||||
type DefaultReturn struct {
|
||||
Code string
|
||||
Message string
|
||||
err []error
|
||||
}
|
||||
|
||||
func NewDefaultReturn() *DefaultReturn {
|
||||
return &DefaultReturn{
|
||||
Code: "",
|
||||
Message: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (r *DefaultReturn) SetError(code int, msg string, file string, line int) {
|
||||
r.Code = fmt.Sprintf("%d", code)
|
||||
r.Message = msg
|
||||
|
@@ -43,7 +43,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
filterPkg = path.Clean(ConvPathFromLocal(reflect.TypeOf(UNK_ERROR).PkgPath()))
|
||||
filterPkg = path.Clean(ConvPathFromLocal(reflect.TypeOf(UnknownError).PkgPath()))
|
||||
currPkgs = path.Base(ConvPathFromLocal(filterPkg))
|
||||
)
|
||||
|
||||
|
@@ -26,10 +26,14 @@
|
||||
|
||||
package ftpclient
|
||||
|
||||
import "github.com/nabbar/golib/errors"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorParamsEmpty errors.CodeError = iota + errors.MinPkgFTPClient
|
||||
ErrorParamsEmpty liberr.CodeError = iota + liberr.MinPkgFTPClient
|
||||
ErrorValidatorError
|
||||
ErrorEndpointParser
|
||||
ErrorNotInitialized
|
||||
@@ -39,21 +43,15 @@ const (
|
||||
ErrorFTPCommand
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
|
||||
func IsCodeError() bool {
|
||||
return isCodeError
|
||||
}
|
||||
|
||||
func init() {
|
||||
isCodeError = errors.ExistInMapMessage(ErrorParamsEmpty)
|
||||
errors.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
|
||||
if liberr.ExistInMapMessage(ErrorParamsEmpty) {
|
||||
panic(fmt.Errorf("error code collision with package golib/ftpclient"))
|
||||
}
|
||||
liberr.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case errors.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamsEmpty:
|
||||
return "given parameters is empty"
|
||||
case ErrorValidatorError:
|
||||
@@ -70,5 +68,5 @@ func getMessage(code errors.CodeError) (message string) {
|
||||
return "ftp client : command to server trigger an error"
|
||||
}
|
||||
|
||||
return ""
|
||||
return liberr.NullMessage
|
||||
}
|
||||
|
34
go.mod
34
go.mod
@@ -2,15 +2,15 @@ module github.com/nabbar/golib
|
||||
|
||||
go 1.21
|
||||
|
||||
toolchain go1.21.4
|
||||
toolchain go1.21.5
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go v1.49.15
|
||||
github.com/aws/aws-sdk-go v1.49.21
|
||||
github.com/aws/aws-sdk-go-v2 v1.24.1
|
||||
github.com/aws/aws-sdk-go-v2/config v1.26.3
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.16.14
|
||||
github.com/aws/aws-sdk-go-v2/service/iam v1.28.7
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.47.8
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0
|
||||
github.com/aws/smithy-go v1.19.0
|
||||
github.com/bits-and-blooms/bitset v1.13.0
|
||||
github.com/c-bata/go-prompt v0.2.6
|
||||
@@ -19,7 +19,7 @@ require (
|
||||
github.com/fxamacker/cbor/v2 v2.5.0
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-ldap/ldap/v3 v3.4.6
|
||||
github.com/go-playground/validator/v10 v10.16.0
|
||||
github.com/go-playground/validator/v10 v10.17.0
|
||||
github.com/google/go-github/v33 v33.0.0
|
||||
github.com/hashicorp/go-hclog v1.6.2
|
||||
github.com/hashicorp/go-retryablehttp v0.7.5
|
||||
@@ -32,10 +32,10 @@ require (
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/nats-io/jwt/v2 v2.5.3
|
||||
github.com/nats-io/nats-server/v2 v2.10.7
|
||||
github.com/nats-io/nats.go v1.31.0
|
||||
github.com/nats-io/nats-server/v2 v2.10.9
|
||||
github.com/nats-io/nats.go v1.32.0
|
||||
github.com/nutsdb/nutsdb v0.14.3
|
||||
github.com/onsi/ginkgo/v2 v2.13.2
|
||||
github.com/onsi/ginkgo/v2 v2.14.0
|
||||
github.com/onsi/gomega v1.30.0
|
||||
github.com/pelletier/go-toml v1.9.5
|
||||
github.com/prometheus/client_golang v1.18.0
|
||||
@@ -49,9 +49,9 @@ require (
|
||||
github.com/xanzy/go-gitlab v0.95.2
|
||||
github.com/xhit/go-simple-mail v2.2.2+incompatible
|
||||
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
|
||||
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
|
||||
golang.org/x/net v0.19.0
|
||||
golang.org/x/oauth2 v0.15.0
|
||||
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
|
||||
golang.org/x/net v0.20.0
|
||||
golang.org/x/oauth2 v0.16.0
|
||||
golang.org/x/sync v0.6.0
|
||||
golang.org/x/sys v0.16.0
|
||||
golang.org/x/term v0.16.0
|
||||
@@ -66,7 +66,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||
github.com/ClickHouse/ch-go v0.61.0 // indirect
|
||||
github.com/ClickHouse/ch-go v0.61.1 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.17.1 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver v1.5.0 // indirect
|
||||
@@ -75,7 +75,7 @@ require (
|
||||
github.com/VictoriaMetrics/metrics v1.6.2 // indirect
|
||||
github.com/VividCortex/ewma v1.2.0 // indirect
|
||||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
|
||||
github.com/andybalholm/brotli v1.0.6 // indirect
|
||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.2 // indirect
|
||||
github.com/antlabs/stl v0.0.1 // indirect
|
||||
github.com/antlabs/timer v0.0.12 // indirect
|
||||
@@ -142,7 +142,7 @@ require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
|
||||
github.com/jackc/pgx/v5 v5.5.1 // indirect
|
||||
github.com/jackc/pgx/v5 v5.5.2 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
@@ -172,9 +172,9 @@ require (
|
||||
github.com/nats-io/nkeys v0.4.7 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/paulmach/orb v0.10.0 // indirect
|
||||
github.com/paulmach/orb v0.11.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.19 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.21 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pkg/term v1.2.0-beta.2 // indirect
|
||||
github.com/prometheus/client_model v0.5.0 // indirect
|
||||
@@ -207,10 +207,10 @@ require (
|
||||
go.opentelemetry.io/otel/trace v1.21.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/arch v0.7.0 // indirect
|
||||
golang.org/x/crypto v0.17.0 // indirect
|
||||
golang.org/x/crypto v0.18.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
golang.org/x/tools v0.16.1 // indirect
|
||||
golang.org/x/tools v0.17.0 // indirect
|
||||
google.golang.org/appengine v1.6.8 // indirect
|
||||
google.golang.org/protobuf v1.32.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
|
@@ -27,7 +27,11 @@
|
||||
|
||||
package nats
|
||||
|
||||
import liberr "github.com/nabbar/golib/errors"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorParamsEmpty liberr.CodeError = iota + liberr.MinPkgNutsDB
|
||||
@@ -47,21 +51,15 @@ const (
|
||||
ErrorServerStart
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
|
||||
func IsCodeError() bool {
|
||||
return isCodeError
|
||||
}
|
||||
|
||||
func init() {
|
||||
isCodeError = liberr.ExistInMapMessage(ErrorParamsEmpty)
|
||||
if liberr.ExistInMapMessage(ErrorParamsEmpty) {
|
||||
panic(fmt.Errorf("error code collision with package golib/nats"))
|
||||
}
|
||||
liberr.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case liberr.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamsEmpty:
|
||||
return "at least one given parameter is empty"
|
||||
case ErrorParamsMissing:
|
||||
@@ -80,5 +78,5 @@ func getMessage(code liberr.CodeError) (message string) {
|
||||
return "cannot start new client connection to cluster"
|
||||
}
|
||||
|
||||
return ""
|
||||
return liberr.NullMessage
|
||||
}
|
||||
|
@@ -26,31 +26,29 @@
|
||||
|
||||
package network
|
||||
|
||||
import "github.com/nabbar/golib/errors"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorParamsEmpty errors.CodeError = iota + errors.MinPkgNetwork
|
||||
ErrorParamsEmpty liberr.CodeError = iota + liberr.MinPkgNetwork
|
||||
ErrorNetCounter
|
||||
ErrorNetInterface
|
||||
ErrorNetNotFound
|
||||
ErrorNetReload
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
|
||||
func IsCodeError() bool {
|
||||
return isCodeError
|
||||
}
|
||||
|
||||
func init() {
|
||||
isCodeError = errors.ExistInMapMessage(ErrorParamsEmpty)
|
||||
errors.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
|
||||
if liberr.ExistInMapMessage(ErrorParamsEmpty) {
|
||||
panic(fmt.Errorf("error code collision with package golib/network"))
|
||||
}
|
||||
liberr.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case errors.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorParamsEmpty:
|
||||
return "given parameters is empty"
|
||||
case ErrorNetCounter:
|
||||
@@ -63,5 +61,5 @@ func getMessage(code errors.CodeError) (message string) {
|
||||
return "cannot reload interface"
|
||||
}
|
||||
|
||||
return ""
|
||||
return liberr.NullMessage
|
||||
}
|
||||
|
@@ -27,34 +27,30 @@
|
||||
package oauth
|
||||
|
||||
import (
|
||||
"github.com/nabbar/golib/errors"
|
||||
"fmt"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorEmptyParams errors.CodeError = iota + errors.MinPkgOAuth
|
||||
ErrorEmptyParams liberr.CodeError = iota + liberr.MinPkgOAuth
|
||||
ErrorOAuthExchange
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
|
||||
func IsCodeError() bool {
|
||||
return isCodeError
|
||||
}
|
||||
|
||||
func init() {
|
||||
isCodeError = errors.ExistInMapMessage(ErrorEmptyParams)
|
||||
errors.RegisterIdFctMessage(ErrorEmptyParams, getMessage)
|
||||
if liberr.ExistInMapMessage(ErrorEmptyParams) {
|
||||
panic(fmt.Errorf("error code collision with package golib/config"))
|
||||
}
|
||||
liberr.RegisterIdFctMessage(ErrorEmptyParams, getMessage)
|
||||
}
|
||||
|
||||
func getMessage(code errors.CodeError) (message string) {
|
||||
func getMessage(code liberr.CodeError) (message string) {
|
||||
switch code {
|
||||
case errors.UNK_ERROR:
|
||||
return ""
|
||||
case ErrorEmptyParams:
|
||||
return "given parameters is empty"
|
||||
case ErrorOAuthExchange:
|
||||
return "code seems to be invalid when trying to get token from it"
|
||||
}
|
||||
|
||||
return ""
|
||||
return liberr.NullMessage
|
||||
}
|
||||
|
Reference in New Issue
Block a user