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.
78 lines
3.1 KiB
78 lines
3.1 KiB
|
2 years ago
|
// =============================================================================
|
||
|
|
// Active-GO Framework
|
||
|
|
// Copyright (c) 2024 ActiveThing (https://activething.com)
|
||
|
|
// Author: Juan V. Navarro juanvnl@activething.com
|
||
|
|
// =============================================================================
|
||
|
|
//
|
||
|
|
// 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 errs
|
||
|
|
|
||
|
|
// ErrWith is a struct that wraps an error and provides additional error handling functionality
|
||
|
|
// It embeds the error interface to maintain error compatibility
|
||
|
|
type ErrWith struct {
|
||
|
|
error
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewErrWith(err error) *ErrWith {
|
||
|
|
if err == nil {
|
||
|
|
panic(g.panicErrNil)
|
||
|
|
}
|
||
|
|
return &ErrWith{err}
|
||
|
|
}
|
||
|
|
|
||
|
|
func MakeErrWith(err error) ErrWith {
|
||
|
|
return *NewErrWith(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Unwrap returns the wrapped error
|
||
|
|
// This method complies with Go's error unwrapping convention
|
||
|
|
func (w ErrWith) Unwrap() error {
|
||
|
|
return w.error
|
||
|
|
}
|
||
|
|
|
||
|
|
// Cause creates a new error with cause by wrapping the current error
|
||
|
|
// and associating it with a cause error
|
||
|
|
// Returns a pointer to ErrWithCause containing both errors
|
||
|
|
func (w ErrWith) Cause(cause error) *ErrWithCause {
|
||
|
|
return NewErrWithCause(w.error, cause)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Message creates a new error with an additional Msg
|
||
|
|
// Returns a pointer to ErrWithMessage containing the original error and new Msg
|
||
|
|
func (w ErrWith) Message(msg string) *ErrWithMessage {
|
||
|
|
return NewErrWithMessage(w.error, msg)
|
||
|
|
}
|
||
|
|
|
||
|
|
// MessageF creates a new error with a formatted Msg using printf-style formatting
|
||
|
|
// params are used to format the Msg string
|
||
|
|
// Returns a pointer to ErrWithMessage containing the original error and formatted Msg
|
||
|
|
func (w ErrWith) MessageF(format string, params ...any) *ErrWithMessage {
|
||
|
|
return NewErrWithMessageF(w.error, format, params...)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Stack creates a new error with stack trace information
|
||
|
|
// offset parameter determines how many stack frames to skip
|
||
|
|
// Returns a pointer to ErrWithStack containing the original error and stack trace
|
||
|
|
func (w ErrWith) Stack(offset int) *ErrWithStack {
|
||
|
|
return NewErrWithStack(w.error, offset)
|
||
|
|
}
|