Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
PLF-110 Rename Class and XyError
Browse files Browse the repository at this point in the history
  • Loading branch information
huykingsofm committed Sep 2, 2022
1 parent 159bb44 commit 214cac7
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 185 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Developing

- Make it simple by removing Generator.
- Rename some structs to make the package more popular.

# v0.0.1 (Sep 02, 2022)

Expand Down
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,34 @@

# Introduction

Package xyerror supports to define error types conveniently.
Package xyerror supports to define and compare errors conveniently.

The error is inspired by the idea of Python `Exception`.
The error is inspired by Python `Exception`.

# Features

## Python Exception Idea

Package xyerror defined an error type used to create other errors called
`Class`, it is equivalent to `Exception` class in Python.
`Exception`, it is equivalent to `Exception` class in Python.

`Class` creates `XyError` objects by using `New` method. It looks like
`Exception` class creates `Exception` instances. Example:
`Exception` creates `Error` objects by using `New` or `Newf` method. It looks
like `Exception` class creates `Exception` instances. Example:

```golang
// xyerror
var xerr xyerror.XyError
xerr = xyerror.ValueError.New("value is invalid")
fmt.Println(xerr)
var err xyerror.Error = xyerror.ValueError.New("value is invalid")
fmt.Println(err)
```

```python
# python
print(ValueError("value is invalid"))
```

A `Class` can "inherits" from another `Class`. Example:
An `Exception` can "inherits" from another `Exception`. Example:

```golang
// xyerror
var ZeroDivisionError = xyerror.ValueError.NewClass("ZeroDivisionError")
var ZeroDivisionError = xyerror.ValueError.NewException("ZeroDivisionError")
fmt.Println(ZeroDivisionError.New("do not divide by zero"))
```

Expand All @@ -51,13 +48,13 @@ class ZeroDivisionError(ValueError):
print(ZeroDivisionError("do not divide by zero"))
```

A `Class` also "inherits" from some `Class` instances. Example:
An `Exception` also "inherits" from many `Exception` instances. Example:

```golang
// xyerror
var ValueTypeError = xyerror.
Combine(xyerror.ValueError, xyerror.TypeError).
NewClass("ValueTypeError")
NewException("ValueTypeError")
```

```python
Expand All @@ -66,7 +63,7 @@ class ValueTypeError(ValueError, TypeError):
...
```

A `XyError` created by `Class` can compare to this `Class`.
An `Error` created by an `Exception` can compare to that `Exception`.

```golang
// xyerror
Expand All @@ -75,10 +72,10 @@ func foo() error {
}

func bar() error {
if xerr := foo(); errors.Is(xerr, xyerror.ValueError) {
fmt.Println(xerr)
if err := foo(); errors.Is(err, xyerror.ValueError) {
fmt.Println(err)
} else {
return xerr
return err
}
return nil
}
Expand Down Expand Up @@ -108,12 +105,12 @@ import (
"github.com/xybor-x/xyerror"
)

func ExampleClass() {
// To create a root Class, call xyerror.NewClass with the its name.
var RootError = xyerror.NewClass("RootError")
func ExampleException() {
// To create a root Exception, call xyerror.NewException with the its name.
var RootError = xyerror.NewException("RootError")

// You can create a class by inheriting from another one.
var ChildError = RootError.NewClass("ChildError")
// You can create an Exception by inheriting from another one.
var ChildError = RootError.NewException("ChildError")

fmt.Println(RootError)
fmt.Println(ChildError)
Expand All @@ -123,14 +120,14 @@ func ExampleClass() {
// ChildError
}

func ExampleXyError() {
// You can compare a XyError with an Class by using the built-in method
func ExampleError() {
// You can compare an Error with an Exception by using the built-in method
// errors.Is.
var NegativeIndexError = xyerror.IndexError.NewClass("NegativeIndexError")
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")
fmt.Println("err1 is a ValueError")
}
if !errors.Is(err1, NegativeIndexError) {
fmt.Println("err1 is not a NegativeIndexError")
Expand All @@ -155,11 +152,11 @@ func ExampleXyError() {
// err2 is not a ValueError
}

func ExampleGroup() {
// Group allows you to create a class with multiparents.
func ExampleCombinedException() {
// CombinedException allows you to create an Exception with multiparents.
var KeyValueError = xyerror.
Combine(xyerror.KeyError, xyerror.ValueError).
NewClass("KeyValueError")
NewException("KeyValueError")

var err = KeyValueError.New("something is wrong")

Expand Down
81 changes: 0 additions & 81 deletions class.go

This file was deleted.

21 changes: 10 additions & 11 deletions default.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package xyerror

// Default predefined errors.
// Predefined Exceptions.
var (
Error = NewClass("Error")
IOError = NewClass("IOError")
FloatingPointError = NewClass("FloatingPointError")
IndexError = NewClass("IndexError")
KeyError = NewClass("KeyError")
NotImplementedError = NewClass("NotImplementedError")
ValueError = NewClass("ValueError")
ParameterError = NewClass("ParameterError")
TypeError = NewClass("TypeError")
AssertionError = NewClass("AssertionError")
BaseException = NewException("BaseException")
IOError = NewException("IOError")
FloatingPointError = NewException("FloatingPointError")
IndexError = NewException("IndexError")
KeyError = NewException("KeyError")
ValueError = NewException("ValueError")
ParameterError = NewException("ParameterError")
TypeError = NewException("TypeError")
AssertionError = NewException("AssertionError")
)
27 changes: 9 additions & 18 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,20 @@ import (
"fmt"
)

// XyError is a special error belongs to a generic error class.
type XyError struct {
// error class
c Class

// error message
// Error is a special error belongs to a generic error class.
type Error struct {
exc Exception
msg string
}

// Error is the method to treat XyError as an error.
func (xerr XyError) Error() string {
return fmt.Sprintf("%s: %s", xerr.c.name, xerr.msg)
// Error returns the Exception name along with the Error message.
func (err Error) Error() string {
return fmt.Sprintf("%s: %s", err.exc.name, err.msg)
}

// Is is the method used to customize errors.Is method.
func (xerr XyError) Is(target error) bool {
if !errors.As(target, &Class{}) {
return false
}

var tc = target.(Class)

return xerr.c.belongsTo(tc)
// Is returns true if Error is created by the target Exception.
func (err Error) Is(target error) bool {
return errors.Is(err.exc, target)
}

// Or returns the first not-nil error. If all errors are nil, return nil.
Expand Down
26 changes: 13 additions & 13 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"github.com/xybor-x/xyerror"
)

func TestXyError(t *testing.T) {
var c = xyerror.NewClass("XError")
var xerr1 = c.Newf("error-%d", 1)
var xerr2 = c.New("error-2")
func TestError(t *testing.T) {
var exc = xyerror.NewException("Error")
var err1 = exc.Newf("error-%d", 1)
var err2 = exc.New("error-2")

xycond.ExpectEqual(xerr1.Error(), "XError: error-1").Test(t)
xycond.ExpectEqual(xerr2.Error(), "XError: error-2").Test(t)
xycond.ExpectEqual(err1.Error(), "Error: error-1").Test(t)
xycond.ExpectEqual(err2.Error(), "Error: error-2").Test(t)
}

func TestXyErrorIs(t *testing.T) {
func TestErrorIs(t *testing.T) {
var err1 = xyerror.ValueError.New("err1")
var err2 = xyerror.TypeError.New("err2")

Expand All @@ -39,11 +39,11 @@ func TestOr(t *testing.T) {
}

func TestCombine(t *testing.T) {
var c = xyerror.Combine(xyerror.ValueError, xyerror.TypeError).
NewClass("ValueTypeError")
var xerr = c.New("foo")
var exc = xyerror.Combine(xyerror.ValueError, xyerror.TypeError).
NewException("ValueTypeError")
var err = exc.New("foo")

xycond.ExpectError(xerr, xyerror.ValueError).Test(t)
xycond.ExpectError(xerr, xyerror.TypeError).Test(t)
xycond.ExpectErrorNot(xerr, xyerror.IndexError).Test(t)
xycond.ExpectError(err, xyerror.ValueError).Test(t)
xycond.ExpectError(err, xyerror.TypeError).Test(t)
xycond.ExpectErrorNot(err, xyerror.IndexError).Test(t)
}
22 changes: 11 additions & 11 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/xybor-x/xyerror"
)

func ExampleClass() {
// To create a root Class, call xyerror.NewClass with the its name.
var RootError = xyerror.NewClass("RootError")
func ExampleException() {
// To create a root Exception, call xyerror.NewException with the its name.
var RootError = xyerror.NewException("RootError")

// You can create a class by inheriting from another one.
var ChildError = RootError.NewClass("ChildError")
// You can create an Exception by inheriting from another one.
var ChildError = RootError.NewException("ChildError")

fmt.Println(RootError)
fmt.Println(ChildError)
Expand All @@ -22,10 +22,10 @@ func ExampleClass() {
// ChildError
}

func ExampleXyError() {
// You can compare a XyError with an Class by using the built-in method
func ExampleError() {
// You can compare an Error with an Exception by using the built-in method
// errors.Is.
var NegativeIndexError = xyerror.IndexError.NewClass("NegativeIndexError")
var NegativeIndexError = xyerror.IndexError.NewException("NegativeIndexError")

var err1 = xyerror.ValueError.New("some value error")
if errors.Is(err1, xyerror.ValueError) {
Expand Down Expand Up @@ -54,11 +54,11 @@ func ExampleXyError() {
// err2 is not a ValueError
}

func ExampleGroup() {
// Group allows you to create a class with multiparents.
func ExampleCombinedException() {
// CombinedException allows you to create an Exception with multiparents.
var KeyValueError = xyerror.
Combine(xyerror.KeyError, xyerror.ValueError).
NewClass("KeyValueError")
NewException("KeyValueError")

var err = KeyValueError.New("something is wrong")

Expand Down
Loading

0 comments on commit 214cac7

Please sign in to comment.