errors

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v1.1.3

func Contains(err, target error) bool

Contains is a function that checks if an error instance or its string representation contains the target error. If the input error is an instance of ErrorDetail, it extracts the error message and updates the input error to a new instance of errors.New(). Finally, it checks if both the input error and target error are not nil and if the string representation of the input error contains the string representation of the target error. It returns a boolean value indicating whether the input error contains the target error.

func Is

func Is(err, target error) bool

Is a function that checks if the given `err` matches the given `target` error. If both `err` and `target` are instances of ErrorDetail, it extracts the error message from each and creates new errors with the extracted messages. This is to ensure that the error messages are comparable. It then compares the modified `err` and `target` using the helper functions `helper.IsNotNil` and `helper.Equals`. Returns true if `err` is not nil and is equal to `target`, false otherwise.

func IsErrorDetail

func IsErrorDetail(err error) bool

IsErrorDetail is a function that checks if the given `err` is an instance of ErrorDetail. It does this by using a regular expression pattern to match the string representation of `err`. Returns true if `err` is not nil and matches the pattern, false otherwise.

func IsNot

func IsNot(err, target error) bool

IsNot is a function that checks if the given `err` is not equal to the given `target` error. It negates the result of the function `Is(err, target)`. Returns true if `err` is not equal to `target`, false otherwise.

func New

func New(args ...any) error

New is a function that creates a new error with additional error details. It takes in variadic arguments `args` of any type and builds a message using `buildMessage`. It then obtains the caller information using `helper.GetCallerInfo` and the current stack trace using `debug.Stack()`. It returns an instance of `ErrorDetail` which contains the file, line number, function name, message, and debug stack. The caller information and debug stack are used for printing the stack trace.

Example usage:

err := New("test error detail")
fmt.Println(err.Error()) // Output: [CAUSE]: (filename:line) function: test error detail [STACK]: stack trace

Example usage:

err := Newf("%s", "test error detail")
fmt.Println(err.Error()) // Output: [CAUSE]: (filename:line) function: test error detail [STACK]: stack trace

func NewSkipCaller

func NewSkipCaller(skipCaller int, args ...any) error

NewSkipCaller is a function that creates a new error with additional error details, skipping a certain number of callers. It takes in an integer argument `skipCaller` to specify the number of callers to skip. It also takes in variadic arguments `args` of any type and builds a message using `buildMessage`. It then obtains the caller information using `helper.GetCallerInfo` and the current stack trace using `debug.Stack()`. It returns an instance of `ErrorDetail` which contains the file, line number, function name, message, and debug stack. The caller information and debug stack are used for printing the stack trace.

Usage:

err := NewSkipCaller(1, "test error detail")
logger.Info("err:", err)
err = NewSkipCaller(1, nil)
logger.Info("err:", err)

Example:

// Output: [CAUSE]: (filename:line) function: test error detail [STACK]: stack trace

func NewSkipCallerf added in v1.2.1

func NewSkipCallerf(skipCaller int, format string, args ...any) error

NewSkipCallerf is a function that creates a new error with additional error details, skipping a certain number of callers and formatting the message using a format string. It takes in an integer argument `skipCaller` to specify the number of callers to skip. It also takes in a format string and variadic arguments `args` of any type to build the formatted message using `buildMessageByFormat`. It then obtains the caller information using `helper.GetCallerInfo` and the current stack trace using `debug.Stack()`. It returns an instance of `ErrorDetail` which contains the file, line number, function name, formatted message, and debug stack. The caller information and debug stack are used for printing the stack trace.

Usage:

err := NewSkipCallerf(1, "%s", "test error detail")
logger.Info("err:", err)
err = NewSkipCallerf(1, "%s")
logger.Info("err:", err)

Example:

// Output: [CAUSE]: (filename:line) function: test error detail [STACK]: stack trace

func Newf added in v1.2.1

func Newf(format string, args ...any) error

Newf is a function that creates a new error with additional error details. It takes in a format string and variadic arguments `args` of any type and builds a message using `buildMessageByFormat`. It then obtains the caller information using `helper.GetCallerInfo` and the current stack trace using `debug.Stack()`. It returns an instance of `ErrorDetail` which contains the file, line number, function name, message, and debug stack. The caller information and debug stack are used for printing the stack trace.

Usage:

err := Newf("%s", "test error detail")
fmt.Println(err.Error()) // Output: [CAUSE]: (filename:line) function: test error detail [STACK]: stack trace

func NotContains added in v1.1.3

func NotContains(err, target error) bool

NotContains is a function that checks if an error instance or its string representation does not contain the target error. It calls the Contains function to check if the error does contain the target error and negates the result. It returns a boolean value indicating whether the error does not contain the target error.

Types

type ErrorDetail added in v1.0.9

type ErrorDetail struct {
	// contains filtered or unexported fields
}

func Details added in v1.1.0

func Details(err error) *ErrorDetail

Details is a function that takes in an error and returns an instance of *ErrorDetail. If the input error is nil, it returns nil. It initializes variables file, line, funcName, message, and debugStack to empty strings. It uses a regular expression to match the error message of the input error against the regexErrorDetail pattern. If there is a match, it extracts the file, line, funcName, message, and debugStack from the error message. Otherwise, it obtains the caller information using helper.GetCallerInfo(2) and the current stack trace using debug.Stack(). It builds the message using buildMessage(err.Error()). It returns a pointer to a newly created ErrorDetail struct, with the extracted/obtained information as its field values.

func (*ErrorDetail) Error added in v1.0.9

func (e *ErrorDetail) Error() string

Error is a method of the ErrorDetail struct that returns a formatted string representation of the error. It returns a string in the format "[CAUSE]: (filename:line) function: message [STACK]: stack trace", where filename represents the name of the file where the error occurred, line represents the line number in the file where the error occurred, function represents the name of the function where the error occurred, message represents the specific error message, and stack trace represents the stack trace at the time the error occurred. This method is used for printing the error message along with the stack trace.

func (*ErrorDetail) GetCause added in v1.1.0

func (e *ErrorDetail) GetCause() string

GetCause is a method of the ErrorDetail struct that returns a formatted string representation of the cause of the error. It returns a string in the format "(filename:line) function: message", where filename represents the name of the file where the error occurred, line represents the line number in the file where the error occurred, function represents the name of the function where the error occurred, and message represents the specific error message. This method is used for getting the cause of the error.

func (*ErrorDetail) GetDebugStack added in v1.0.9

func (e *ErrorDetail) GetDebugStack() string

GetDebugStack is a method of the ErrorDetail struct that returns the debug stack trace. It returns a string representing the debug stack trace stored in the ErrorDetail instance. This method is used for retrieving the debug stack trace. Example usage:

err := New("test error detail")
stack := Details(err).GetDebugStack()
fmt.Println(stack) // Output: goroutine 1 [running]:
stack_trace_test.(*ErrorDetail).GetDebugStack(0x1140d81e0, 0xc00003e1b0, 0x104126d, 0xc00000c080)
	/path/to/file.go:42 +0x136
stack_trace_test.TestErrorGetDebugStack(0xc00003e1b0)
	/path/to/file_test.go:11 +0x38
testing.tRunner(0xc00003e1b0, 0x118b556)
	/path/to/testing/testing.go:1233 +0x162
created by testing.(*T).Run
	/path/to/testing/testing.go:1274 +0x298

Note: The actual stack trace content may vary depending on the environment and program execution.

func (*ErrorDetail) GetFile added in v1.0.9

func (e *ErrorDetail) GetFile() string

GetFile is a method of the ErrorDetail struct that returns the name of the file where the error occurred. It returns a string representing the name of the file. This method is used for retrieving the file name. Example usage:

err := New("test error detail")
file := Details(err).GetFile()
fmt.Println(file) // Output: test_error.go

func (*ErrorDetail) GetFuncName added in v1.0.9

func (e *ErrorDetail) GetFuncName() string

GetFuncName is a method of the ErrorDetail struct that returns the name of the function where the error occurred.

func (*ErrorDetail) GetLine added in v1.0.9

func (e *ErrorDetail) GetLine() int

GetLine is a method of the ErrorDetail struct that returns the line number where the error occurred as an integer. It uses the SimpleConvertToInt function from the helper package to convert the line number from a string to an integer. This method is used for retrieving the line number of the error. Example usage:

err := New("test error detail")
line := Details(err).GetLine()
fmt.Println(line) // Output: 42

func (*ErrorDetail) GetMessage added in v1.0.9

func (e *ErrorDetail) GetMessage() string

GetMessage is a method of the ErrorDetail struct that returns the error message. It returns a string representing the error message stored in the ErrorDetail instance. This method is used for retrieving the error message. Example usage:

err := New("test error detail")
message := Details(err).GetMessage()
fmt.Println(message) // Output: test error detail

func (*ErrorDetail) PrintCause added in v1.1.0

func (e *ErrorDetail) PrintCause()

PrintCause is a method of the ErrorDetail struct that logs the cause of the error using logger.ErrorSkipCaller. It takes no arguments and does not return anything. This method is used for logging the cause of the error.

func (*ErrorDetail) PrintStackTrace added in v1.1.0

func (e *ErrorDetail) PrintStackTrace()

PrintStackTrace is a method of the ErrorDetail struct that logs the debugStack using logger.ErrorSkipCaller. It takes no arguments and does not return anything. This method is used for printing the stack trace. Example usage:

err := New("test error detail")
Details(err).PrintStackTrace()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL