sourceerror

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: GPL-3.0 Imports: 3 Imported by: 4

README

SourceError

golang GoDoc Go Report Issues Size Tag View examples License


Purpose

This small module offers the ErrCodeLocation error type that wraps another error instance, along with the file name, line number, and function name where the error occurred. The ErrCodeLocation error can be used especially during development to help finding program errors. Once the source code is free of avoidable errors, one just sets the NODEBUG flag to true without having to change error handling at all.

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 (
	se "github.com/mwat56/sourceerror"
)

// ...

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

// here some error occurs:
err := someFunction()
if nil != err {
	err = se.SourceError(err, 2)
	// `err` now wraps the original 'err` and points two
	// lines up i.e. the line where the error appeared.

	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

Documentation

Overview

Package sourceerror implements an error type that wraps another error instance. That error type includes the file name, line number, and function name where the error occurred, along with original error's text.

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](http://www.gnu.org/licenses/gpl.html) for details.

Copyright © 2024 M.Watermann, 10247 Berlin, Germany

    All rights reserved
EMail : <support@mwat.de>

Index

Constants

View Source
const (
	// The constant error message of the `ErrSourceLocation` error type.
	StrCodeLocation = "error in source"
)

Variables

View Source
var (
	// If set `true`, the `SourceError()` function basically becomes a NoOp.
	NODEBUG bool
)

Functions

func SourceError

func SourceError(aErr error, aLines int) error

`SourceError()` is a function that wraps an error with additional information about the location where the 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.

NOTE: If the global `NODEBUG` flag is `true`, this function simply returns he given `aErr`, skipping the error location investigation.

Parameters: - `aErr`: The error to be wrapped. - `aLines`: The number of lines to subtract from the caller's line number.

Returns: - `error`: A new `ErrSourceLocation` instance that contains the original error, file, function, and adjusted line number of the code causing `aErr`.

Types

type ErrSourceLocation

type ErrSourceLocation struct {
	File     string
	Function string
	Line     int
	Stack    string
	// contains filtered or unexported fields
}

func (ErrSourceLocation) Error

func (se ErrSourceLocation) Error() string

`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 text.

Returns: - `string`: a string representation of the error message and location.

func (ErrSourceLocation) String

func (se ErrSourceLocation) String() string

`String()` implements the `Stringer` interface and returns a string representation of the error location.

It includes the file name, line number, and function name where the error occurred.

Returns: - `string`: a string representation of the error location.

func (ErrSourceLocation) Unwrap

func (se ErrSourceLocation) Unwrap() error

`Unwrap()` returns the original error that was wrapped by `ErrSourceLocation`.

Returns: - `error`: the original error.

Jump to

Keyboard shortcuts

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