errt

package
v0.0.0-...-d841f61 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: AGPL-3.0 Imports: 0 Imported by: 0

Documentation

Overview

Package errt package defines some basic errors to use, see example.

The idea is to encapsulate any error in a hidden struct that implements a public interface.

That way, type assertion will give you the kind of error you encountered.

Also, you're free to redefine your custom errors in interfaces to avoid tight coupling between your program/library and this one.

Example
package main

import (
	"errors"
	"fmt"

	"git.canopsis.net/canopsis/go-engines/lib/errt"
)

type myError struct {
	errt.ErrT
	moreInfos bool
}

type MyError interface {
	errt.ErrT
	IsMyError()
	MoreInfos() bool
}

// IsMyError function does nothing because it's only here to implement the MyError interface.
// This makes type assertion possible.
func (e myError) IsMyError() {
}

func (e myError) MoreInfos() bool {
	return e.moreInfos
}

func NewMyError(err error, moreInfos bool) MyError {
	// Always return nil if the root error is nil
	if err == nil {
		return nil
	}
	return myError{
		ErrT:      errt.NewErrT(err),
		moreInfos: moreInfos,
	}
}

func IReturnAnError() error {
	oerr := errors.New("this is my error")
	return NewMyError(oerr, true)
}

func main() {
	err := IReturnAnError()

	if err != nil {
		switch referr := err.(type) {
		case MyError:
			fmt.Printf("i have more infos: %v", referr.MoreInfos())

		default:
			fmt.Printf("unknown error: %v", referr)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsFatal

func IsFatal(err error) bool

IsFatal returns true if the error is of type errt.Fatal, using type assertion. This is only a shortcut for type assertion since Fatal error might be used in multiple places.

func NewDuplicated

func NewDuplicated(err error) error

NewDuplicated returns a Duplicated Error

func NewFatal

func NewFatal(err error, rcode int) error

NewFatal returns a new Fatal Error

param rcode is the optional return code that can be used for os.Exit() for example.

func NewIOError

func NewIOError(err error) error

NewIOError returns an IO Error

func NewNotFound

func NewNotFound(err error) error

NewNotFound returns a NotFound Error

func NewUnknownError

func NewUnknownError(err error) error

NewUnknownError ...

func NewUnmanagedEventError

func NewUnmanagedEventError(err error) error

NewUnmanagedEventError ...

Types

type Duplicated

type Duplicated interface {
	IsDuplicated()
	ErrT
}

Duplicated can be used when something is a duplicate of something else.

type ErrT

type ErrT interface {
	// Returns the original error
	Err() error
	error
}

ErrT is the generic interface that all errt-compatible errors must implement.

func NewErrT

func NewErrT(err error) ErrT

NewErrT creates a RefErrT

type Fatal

type Fatal interface {
	IsFatal()
	RCode() int
	ErrT
}

Fatal error that should lead to program exit

type IOError

type IOError interface {
	IsIOError()
	ErrT
}

IOError of any kind: timeout, host unreachable, file permissions...

type NotFound

type NotFound interface {
	IsNotFound()
	ErrT
}

NotFound error

type UnknownError

type UnknownError interface {
	ErrT
	IsUnknownError()
}

UnknownError should be used to encapsulate any non fatal error with no consequences other than "it doesn't worked"

type UnmanagedEventError

type UnmanagedEventError interface {
	ErrT
	IsUnmanagedEventError()
}

UnmanagedEventError can be returned from any method that doesn't manage a given types.Event

Jump to

Keyboard shortcuts

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