Documentation
¶
Overview ¶
Package `sourceerror` implements a new error type that wraps another error instance. That error type includes the file name, line number, and function name where the initial error occurred, along with original error's message text and a call stack.
Copyright © 2024, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.
Copyright © 2024, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Index ¶
Constants ¶
const ( // `StringErrSource` is the error message text of the // `ErrSource` error type. StringErrSource = "error in source" )
Variables ¶
var ( // `NODEBUG` is a toggle used by [New] to either skip the error's // location investigation or include it. NODEBUG bool // `NOSTACK` is a toggle used by [New] to either skip the error's // call-stack investigation or include it. NOSTACK bool )
Functions ¶
func New ¶ added in v0.3.0
`New()` returns a new `ErrSource` instance that wraps `aErr` with additional information about the location where the initial error occurred. It uses certain `runtime` functions to determine the file- and function-names, as well as the code line and the call stack.
The `aLines` parameter allows for adjusting the reported line number by subtracting the specified number of lines from the actual line number to point to the code line where the initial error actually occurred.
If the global `NODEBUG` flag is `true`, this function returns just the given `aErr`, without any memory overhead.
If the global `NOSTACK` flag is `true`, this function returns does not add the initial error's call stack.
Parameters:
- `aErr`: The error to be wrapped.
- `aLines`: The number of lines to subtract from the caller's line number.
Returns:
- `error`: A new `ErrSource` instance that contains `aErr`, as well as file, function, and adjusted line number of the code causing the error.
Types ¶
type ErrSource ¶ added in v0.2.0
type ErrSource struct {
File string // 16 bytes
Function string // dito
Line int // 8 bytes
Stack []byte // 24 bytes
// contains filtered or unexported fields
}
`ErrSource` is an error type that wraps another error with the location of where that other error was encountered. It includes the file name, line number, and function name where the initial error occurred, along with original error's message text and call stack.
All public fields should be considered R/O (there really isn't any reason to modify those fields apart from confusing yourself).
The fields are as follows:
- `File`: The source file where the error was encountered.
- `Function`: The function wherein the error was encountered.
- `Line`: The code line within the `File`.
- `Stack`: The call stack to where the error was created.
func (ErrSource) Error ¶ added in v0.2.0
`Error()` returns a string representation of the error message along with the error location.
It includes the file name, line number, and function name where the error occurred, along with original error's message text and call stack.
Returns:
- `string`: A string representation of the error message and location.
func (ErrSource) String ¶ added in v0.2.0
`String()` implements the `Stringer` interface and returns a string representation of the error instance.
It includes the file name, line number, and function name where the error occurred as well as a call stack.
Returns:
- `string`: A string representation of the `ErrSource` instance.
