customError

package module
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: GPL-3.0 Imports: 4 Imported by: 21

README

customError


A simple extension on the Error() interface. That provides output in yellow (warning) or red (error), according to the error type.

IMPORTANT NOTE


This github repo is a read-only copy of the actual source of truth repo. The source repo is outside of github, and is the one where all Pull Requests and merges happen.

If you need changes, please fill an issue in the issues tab of the repo with what needs to be corrected, or better yet, include a patch to whichever files need changes.

My main repo is on gitea (hosted somewhere else), and compability issues between gitea and github prevent me to git pull the github repo into the gitea repo, but I can push from gitea to github, thanks to git remote add githubRepo ghRepoURL

In a good day, in a good mood, I could give access to some users to my gitea repo, but... well.. most likely not. Accept it, or not: by no means this package is a perfect package, we're all humans with different levels of understanding of GO or whatnot, so it does not suit you ? Please do find another more suitable solution to your needs.

Structure

We have this very simple (... for now) structure and its dependencies. I expect to expand this type down the road.

type ErrortypeIota int

const (
	Fatal  ErrortypeIota = iota
	Warning
	Continuable
	Undefined
)

type CustomError struct {
    Title    string         // optional
    Message  string
    Code     int           // optional
    Fatality ErrortypeIota // if omitted, we will use "Undefined, and will throw a panic() right away, so... define it :)
}

Having Fatal defined as the first iota (thus value == 0) means that every time we define a CustomError struct and omit Fatality, we'll deal with a fatal error.

If you wish to customize an error message that would otherwise be plain, or just meh, you just need to populate a variable of the above struct type with the proper members, and use $YOUR_VAR_NAME.Error(). More on that later on.

Struct members
  • Of the above structure, what is important here is the Fatality member from the CustomError struct. It is from there that we decide of the colors used in the output, and whether this is enough to completely halt the software. As it is, a Fatal error halts the software, and un Undefined error throws a panic().

When this member is not explicitely initialized, it assumes the value of "Undefined", and will throw, eventually a panic() and stops the software. Otherwise, this is the value that determines which colors are used to display the error message.

  • The Title struct member is self-explanatory. It prepends the actual error message with a title, such as (example): "FATAL: ", or "WARNING", or whatever.

The color of Title is assumed from Fatality, which is red. If omitted, the actual error message (member Message) will be of the proper color.

  • The Message member is the "real" error message. A safe bet is to have it using err.Error() Actually, if you omit to define this member, it will be set to "Unspecified error" as Message.

  • Code is totally optional, and is there for totally real customized error messages where you define your own errors.

It will not show up in the output if omitted.

Usage

Assume you have the following code that returns a plain error:

func myFunc() error {
    if err := os.MkDirAll(var1, var2); err != nil {
        return err
    }
}

This would return an error the usual way to whichever function called myFunc().

With the CustomError package, it gets a bit more colourful, and explicit:

  1. You first need to download the package: go get "github.com/jeanfrancoisgratton/customErrors"
  2. In the import section of your file, you need to add the above package in that section
  3. Then, you will need to change your previous code a bit:
func myFunc() error {
    if err := os.MkDirAll(var1, var2); err != nil {
        ce := CustomError {Fatality: ErrortypeIota.Fatal, Message: err.Error(), Title: "Error creating directory", Code: -8}
        return ce.Error()		
    }
}

Remember :

You can omit the Title, Message and Code members of the struct without any hitch;

  • if you omit Fatality, your software will exit with a panic() call.
  • if you omit Message, it will use the error message from err.Error() (as it would have done before using CustomError)
  • if you omit Title, the coloured output will display Message, without any title
  • if you omit Code, it just gets discarded from the coloured output

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blue

func Blue(sentence string) string

func ClearTerminal added in v1.7.1

func ClearTerminal()

func Green

func Green(sentence string) string

func Red

func Red(sentence string) string

COLOR FUNCTIONS ===============

func White

func White(sentence string) string

func Yellow

func Yellow(sentence string) string

Types

type CustomError

type CustomError struct {
	Fatality       ErrortypeIota // optional, if omitted, we will use "Fatal"; "Undefined" will throw a panic right away
	Title          string        // optional
	Message        string        // optional, if omitted, "Unspecified error" will be used
	Code           int           // optional
	ColouredOutput bool          // unused for now
}

This is the main data type.

func NewCustomError added in v1.7.1

func NewCustomError(colouredOutput bool) CustomError

func (CustomError) Continuable

func (e CustomError) Continuable() string

func (CustomError) Error

func (e CustomError) Error() string

This is the main function, really

func (CustomError) Fatal

func (e CustomError) Fatal()

This is how a FATAL error message is displayed, with various scenarios: - with or without a Title - with or without an error code

func (CustomError) Unknown

func (e CustomError) Unknown() string

We do not care for Title, here...

func (CustomError) Warning

func (e CustomError) Warning() string

type ErrortypeIota

type ErrortypeIota int

The ErrortypeIota and the following constant is used to determine what kind of error we deal with

const (
	Fatal ErrortypeIota = iota
	Warning
	Continuable
	Undefined // Title is disregarded, we only use Message here
)

Jump to

Keyboard shortcuts

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