customErrors

package module
v0.0.0-...-aa5c16e Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

README

customErrors


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 aread-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 (
	Undefined ErrortypeIota = iota
	Fatal
	Warning
	Continuable
)

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 :)
}

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.

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 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 // if omitted, we will use "Undefined, and will throw a panic() right away, so... define it :)
	Title    string        // optional
	Message  string        // if omitted, "Unspecified error" will be used
	Code     int           // optional
}

This is the main data type.

func (CustomError) Continuable

func (e CustomError) Continuable() string

func (CustomError) Error

func (e CustomError) Error() string

func (CustomError) Fatal

func (e CustomError) Fatal() string

func (CustomError) Unknown

func (e CustomError) Unknown() string

func (CustomError) Warning

func (e CustomError) Warning() string

type ErrortypeIota

type ErrortypeIota int

The ErrortypeIota and the following constant are used to determine what kind of errors is CustomError able to manage

const (
	Undefined ErrortypeIota = iota
	Fatal
	Warning
	Continuable
)

Jump to

Keyboard shortcuts

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