Skip to content

zlobste/baserr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

The baserr package in Go provides a simple and effective way to create inherited errors, similar to the inheritance of exceptions in object-oriented programming languages. This approach allows for more specific error handling in Go applications.

Installation

go get github.com/zlobste/baserr

Usage

Import the baserr package into your Go project:

import (
    "github.com/zlobste/baserr"
)

Define your own error types:

// OSError -> IOError -> WriteError
type (
    // OSError represents a first level error. It is used as parent for other errors.
    OSError struct {
        baserr.Base[baserr.BaseError]
    }
    // IOError represents an error inherited from OSError.
    IOError struct {
        baserr.Base[OSError]
    }
    // WriteError represents an error inherited from IOError.
    WriteError struct {
        baserr.Base[IOError]
    }
)

Create typed errors:

func execute() error {
    return baserr.NewError[WriteError]("EOF")
}

Check if an error is inherited from a specific error type:

if err := execute(); err != nil {
    switch {
    case baserr.InheritedFrom[CustomError](err):
        fmt.Println("custom")
    case baserr.InheritedFrom[OSError](err):
        fmt.Println("write error")
    }
} else {
    fmt.Println("no error occurred")
}

Output:

write error

About

Advanced Error Handling in Go

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages