You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name errMeta.go
// Created by DEV
// Modified 24/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// 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 errs
import (
"time"
)
// ErrWithInfo is an error type that enriches errors with additional context and metadata
// It wraps an existing error and adds fields for documentation, timing, and descriptions
type (
ErrWithInfo struct {
error // Embedded error interface
Msg string // Custom error Msg
Info string // Additional information about the error
Desc string // Detailed description of the error
Help string // Help text for resolving the error
Link string // URL to documentation or additional resources
Date time.Time // Timestamp when the error was created
}
// ErrWithInfoFnc is a function type used for the functional options pattern
// It allows flexible configuration of ErrWithInfo instances
ErrWithInfoFnc func(*ErrWithInfo)
)
// NewErrWithInfo creates a new ErrWithInfo instance with the given error and options
// Panics if the input error is nil
// The options parameter allows customizing the error information using functional options
func NewErrWithInfo(err error, options ...ErrWithInfoFnc) *ErrWithInfo {
if err == nil {
panic(G.ErrInvalidNilError.With().MessageF(errNilWrapMsg, "ErrWithInfo"))
}
ei := &ErrWithInfo{
error: err,
Link: G.InfoLink,
Date: time.Now(),
}
for _, o := range options {
o(ei)
}
return ei
}
// Message returns the custom Msg associated with the error
// Implements the Message() string interface
func (e ErrWithInfo) Message() string {
return e.Msg
}
// Unwrap returns the underlying error
// Implements the Unwrapper interface
func (e ErrWithInfo) Unwrap() error {
return e.error
}
// With returns a WrapErrWith instance containing this error
// Enables using the wrapper's additional error handling functionality
func (e ErrWithInfo) With() ErrWith {
return MakeErrWith(e)
}

Powered by TurnKey Linux.