Skip to content

Error type that provides the file, line number, and function where an error occurred

License

Notifications You must be signed in to change notification settings

mwat56/sourceerror

Repository files navigation

SourceError

golang GoDoc Go Report Issues Size Tag View examples License


Purpose

This module offers the ErrSource error type that wraps another error instance, along with the file name, line number, and function name where the error occurred.

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.

The ErrSource methods Error() and String() mention another field

- `Error`: The string representation of the wrapped error.

The ErrSource can be used especially during development to help finding problems in the source code. In case the error call-stacks are not needed just set the NOSTACK flag to true (which will save some time an memory). Once the source code is free of avoidable errors, just set the NODEBUG flag to true without any need to change the source code otherwise.

Installation

You can use Go to install this package for you:

go get -u github.com/mwat56/sourceerror

Usage

It can be used by calling the provided constructor function:

import (
	"github.com/mwat56/sourceerror"
)

// ...

// if the call-stacks are not needed:
sourceerror.NOSTACK = true

// uncomment the next line when your code is production ready:
// sourceerror.NODEBUG = true

// ...

// here some error occurs:
err := someFunction()
if nil != err {
	err = sourceerror.Wrap(err, 2)
	// `err` now wraps the original `err` and points
	// two lines up i.e. to the line where the error
	// was encountered.

	return err
	// ... or perform some proper error handling here
}

// ...

Libraries

No external libraries were used building sourceerror.

Licence

    Copyright © 2024 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 for details.


GFDL

About

Error type that provides the file, line number, and function where an error occurred

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages