Inline documentation

This commit is contained in:
Magnus Åhall 2023-09-26 09:17:17 +02:00
parent a1602d21d9
commit b77f5b94c8

7
pkg.go
View File

@ -21,13 +21,15 @@ var (
baseDirLength int baseDirLength int
) )
// Init only works if called from the main package and sets the length of code base path
// to later be removed in file paths to receive relative paths.
func Init() { func Init() {
_, file, _, _ := runtime.Caller(1) _, file, _, _ := runtime.Caller(1)
dirBase := path.Dir(file) dirBase := path.Dir(file)
baseDirLength = len(dirBase) baseDirLength = len(dirBase)
fmt.Printf("--\nDIR: %s\nBASE: %s\nLENGTH: %d\n--\n", file, dirBase, baseDirLength)
} }
// SetLogCallback gives a possibility to automatically run code to handle any errors.
func SetLogCallback(cbk LogCallback) { func SetLogCallback(cbk LogCallback) {
logCallback = cbk logCallback = cbk
} }
@ -38,6 +40,7 @@ func callback(wrapped Error) {
} }
} }
// Error implements the error inteface and adds filename and line to the error.
func (wrapped Error) Error() string { func (wrapped Error) Error() string {
return fmt.Sprintf( return fmt.Sprintf(
"[%s:%d] %s", "[%s:%d] %s",
@ -62,10 +65,12 @@ func create(err error) error {
return wrapped return wrapped
} }
// Wrap wraps an existing error with file and line.
func Wrap(err error) error { func Wrap(err error) error {
return create(err) return create(err)
} }
// New creates a new wrapped error with file and line.
func New(msg string) error { func New(msg string) error {
wrapped := create(errors.New(msg)) wrapped := create(errors.New(msg))
return wrapped return wrapped