https://github.com/xybor-x/xyerror
Python-like error design in Golang
https://github.com/xybor-x/xyerror
error error-handling errors exception-handling golang golang-error golang-errors golang-library python-like
Last synced: 3 months ago
JSON representation
Python-like error design in Golang
- Host: GitHub
- URL: https://github.com/xybor-x/xyerror
- Owner: xybor-x
- License: mit
- Created: 2022-09-01T03:37:42.000Z (over 3 years ago)
- Default Branch: dev
- Last Pushed: 2023-01-07T16:06:43.000Z (over 3 years ago)
- Last Synced: 2024-09-23T06:07:27.002Z (over 1 year ago)
- Topics: error, error-handling, errors, exception-handling, golang, golang-error, golang-errors, golang-library, python-like
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://github.com/huykingsofm)
[](https://pkg.go.dev/github.com/xybor-x/xyerror)
[](https://github.com/xybor-x/xyerror)
[](https://go.dev/)
[](https://go.dev/blog/go1.18)
[](https://github.com/xybor-x/xyerror/releases/latest)
[](https://www.codacy.com/gh/xybor-x/xyerror/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xybor-x/xyerror&utm_campaign=Badge_Grade)
[](https://www.codacy.com/gh/xybor-x/xyerror/dashboard?utm_source=github.com&utm_medium=referral&utm_content=xybor-x/xyerror&utm_campaign=Badge_Coverage)
[](https://goreportcard.com/report/github.com/xybor-x/xyerror)
# Introduction
Package xyerror supports to define and compare errors conveniently.
The error is inspired by Python `Exception`.
# Features
Package xyerror defined an error type used to create other errors called
`Exception`, it is equivalent to `Exception` class in Python.
`Exception` creates `Error` objects by using `New` or `Newf` method. It looks
like `Exception` class creates `Exception` instances. Example:
```golang
// xyerror
var err xyerror.Error = xyerror.ValueError.New("value is invalid")
fmt.Println(err)
```
```python
# python
print(ValueError("value is invalid"))
```
An `Exception` can "inherits" from another `Exception`. Example:
```golang
// xyerror
var ZeroDivisionError = xyerror.ValueError.NewException("ZeroDivisionError")
fmt.Println(ZeroDivisionError.New("do not divide by zero"))
```
```python
# python
class ZeroDivisionError(ValueError):
...
print(ZeroDivisionError("do not divide by zero"))
```
An `Exception` also "inherits" from many `Exception` instances. Example:
```golang
// xyerror
var ValueTypeError = xyerror.
Combine(xyerror.ValueError, xyerror.TypeError).
NewException("ValueTypeError")
```
```python
# python
class ValueTypeError(ValueError, TypeError):
...
```
An `Error` created by an `Exception` can compare to that `Exception`.
```golang
// xyerror
func foo() error {
return xyerror.ValueError.New("value is invalid")
}
func bar() error {
if err := foo(); errors.Is(err, xyerror.ValueError) {
fmt.Println(err)
} else {
return err
}
return nil
}
```
```python
# python
def foo():
raise ValueError("value is invalid")
def bar():
try:
foo()
except ValueError as e:
print(e)
```
# Example
```golang
package xyerror_test
import (
"errors"
"fmt"
"github.com/xybor-x/xyerror"
)
func ExampleException() {
// To create a root Exception, call xyerror.NewException with the its name.
var RootError = xyerror.NewException("RootError")
// You can create an Exception by inheriting from another one.
var ChildError = RootError.NewException("ChildError")
fmt.Println(RootError)
fmt.Println(ChildError)
// Output:
// RootError
// ChildError
}
func ExampleError() {
// You can compare an Error with an Exception by using the built-in method
// errors.Is.
var NegativeIndexError = xyerror.IndexError.NewException("NegativeIndexError")
var err1 = xyerror.ValueError.New("some value error")
if errors.Is(err1, xyerror.ValueError) {
fmt.Println("err1 is a ValueError")
}
if !errors.Is(err1, NegativeIndexError) {
fmt.Println("err1 is not a NegativeIndexError")
}
var err2 = NegativeIndexError.Newf("some negative index error %d", -1)
if errors.Is(err2, NegativeIndexError) {
fmt.Println("err2 is a NegativeIndexError")
}
if errors.Is(err2, xyerror.IndexError) {
fmt.Println("err2 is a IndexError")
}
if !errors.Is(err2, xyerror.ValueError) {
fmt.Println("err2 is not a ValueError")
}
// Output:
// err1 is a ValueError
// err1 is not a NegativeIndexError
// err2 is a NegativeIndexError
// err2 is a IndexError
// err2 is not a ValueError
}
func ExampleCombinedException() {
// CombinedException allows you to create an Exception with multiparents.
var KeyValueError = xyerror.
Combine(xyerror.KeyError, xyerror.ValueError).
NewException("KeyValueError")
var err = KeyValueError.New("something is wrong")
if errors.Is(err, xyerror.KeyError) {
fmt.Println("err is a KeyError")
}
if errors.Is(err, xyerror.ValueError) {
fmt.Println("err is a ValueError")
}
// Output:
// err is a KeyError
// err is a ValueError
}
```