errors

package module
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 6 Imported by: 2

README

errors

Sentinel Errors

Creating or Extending Errors
var ErrInvalidInput := New("Invalid Input")
var ErrMissingIdParameter := Extend(ErrInvalidInput, "Missing Id Parameter")

New creates a new error. And Extend creates a new error which satisfies Is relationship with the error it extends.

errors.Is(ErrMissingIdParameter, ErrInvalidInput) // will return true
Returning errors
func Return(err error, cause error, msg string) error

Return returns an error of type err. You pass the cause of the error and override the error message of the error.

Return internally captures the call stack which lets you print the stack trace.

For eg,

import (
	"fmt"

	"github.com/kumarishan/errors"
)

var ErrInternalError = errors.New("internal error")
var ErrInvalidInput = errors.New("invalid input")
var ErrMissingIdParameter = errors.Extend(ErrInvalidInput, "missing id parameter")

func A() error {
	return errors.Return(ErrInvalidInput, nil, "")
}

func B() error {
	err := A()

	if errors.Is(err, ErrInvalidInput) {
		return errors.Return(ErrMissingIdParameter, err, "overriden error message")
	}

	return errors.Return(ErrInternalError, nil, "")

}

func main() {
	err := B()
	fmt.Printf("Got error: %+v\n", err)
}

the output is

Got error: overriden error message
	main.B(./errors/example/main.go:21) (0x112223b)
	main.main(./errors/example/main.go:29) (0x11222d8)
Caused by: invalid input
	main.A(./errors/example/main.go:14) (0x11221ff)
	main.B(./errors/example/main.go:18) (0x11221f8)

todo

Similarities from Java

Using New
Err := New("some error")

is equivalent to

class Err extends Exception {
    public Err() {
        super("some error")
    }

    public Err(String message, Throwable cause) {
        super("some error, " + message, cause)
    }

    public Err(Throwable cause) {
        super("some error", cause)
    }
}
Using Extend
ErrE := Extend(Err, "derieved error")

is equivalent to

class ErrE extends Err {
    private String message = "derieved error";

    public ErrE(Throwable cause) {
        super(cause)
    }

    public ErrE() {
        super()
    }

    public toString() String {
        return super.toString() + ":" + message;
    }

}
Using Struct Embedding

type EErr struct {
    errors.Error
    int a
    string b
}

func F() error {
    return &EErr({nil, nil, "some error"}, 10, "some value")
}

is equivalent to


class EErr extends Exception {
    private int a;
    private String b;

    public EErr(int a, String b, string msg) {
        super(msg);
        this.a = a;
        this.b = b;
    }

}

class Example {
    public static void F() throws Exception {
        throw new EErr(10, "some value", "some error")
    }
}

Returning Error

In golang the error is not thrown, but instead returned from the function and handled by the caller.


func F1() error {
    return errors.Return(Err, nil, "")
}

func F2() error {
    return errors.Return(Err, nil, "custom message")
}

func F3() error {
    cause := errors.New("some cuase")
    return errors.Return(Err, cause, "custom message")
}

is equivalent to


class Example {

    class SomeCause extends Exception {
        public SomeCause() {
            super("some cause")
        }
    }

    public static void F1() throws Exception {
        throw new Err();
    }

    public static void F2() throws Exception {
        throw new Err("custom message")
    }

    public static void F3() throws Exception {
        throw new Err("cu)
    }
}

Documentation

Index

Constants

View Source
const MaxStackDepth = 50

Variables

View Source
var (
	ErrInvalidInput            = New("InvalidInputError")
	ErrInternal                = New("InternalError")
	ErrUnexpectedInternalState = New("UnexpectedInternalStateError")
	ErrNotFound                = New("NotFoundError")
)

Functions

func Extend

func Extend(err error, msg string) error

Create a new error extended from another error error.Is will now work on both this and the from error

func Is

func Is(err, target error) bool

func New

func New(msg string) error

Define a new error

func NewHttpErr

func NewHttpErr(msg string, code int) error

func Return

func Return(err error, cause error, msg string) error

func Returnf

func Returnf(err error, cause error, format string, args ...any) error

func StackTrace

func StackTrace(err error) string

Types

type BaseError

type BaseError struct {
	// contains filtered or unexported fields
}

func (*BaseError) Error

func (b *BaseError) Error() string

type HttpErr

type HttpErr struct {
	BaseError
	// contains filtered or unexported fields
}

func (*HttpErr) Error

func (h *HttpErr) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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