Skip to content

Commit

Permalink
initial commit errorx
Browse files Browse the repository at this point in the history
  • Loading branch information
ymohl-cl committed May 31, 2019
1 parent 412b945 commit 0fc46d5
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 0 deletions.
59 changes: 59 additions & 0 deletions errorx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Errorx

[![Source](https://img.shields.io/badge/git-source-orange.svg?style=flat-square)](https://github.com/ymohl-cl/gopkg/tree/errorx-release/errorx)
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/ymohl-cl/gopkg/errorx)
[![Build Status](https://travis-ci.org/ymohl-cl/gopkg.svg?branch=errorx-release&style=flat-square)](https://travis-ci.org/ymohl-cl/gopkg)
[![codecov](https://codecov.io/gh/ymohl-cl/gopkg/branch/errorx-release/graph/badge.svg?style=flat-square)](https://codecov.io/gh/ymohl-cl/gopkg)
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/ymohl-cl/gopkg/errorx-release/LICENSE)

The errorx package implement a manager error which implement the standar error interface.

Provide a dictionnary and wrapper errors.

## Requirements

Golang 1.12.4 or higher

download the package

``` bash
go get -u github.com/ymohl-cl/gopkg/errorx
```

## Usage

``` Golang
import "github.com/ymohl-cl/gopkg/errorx"

func main() {
// build a dictionnary error
d := NewDico()
d.SetEntry(uint64(0), "error example 1")
d.SetEntry(uint64(1), "error example 2")

// then catch an error
err := func() errorx.Errorx {
return d.Error(uint64(0))
}()
err.Wrap("context error example")

// finally print error to see the result
fmt.Println(err.Error())
}
```

Output:

``` bash
> "context error example -> error example 1"
```

## Changelog

### v1.0.0

Initial commit

- dictionnary management
- implement standar error interface
- tests and documentation
37 changes: 37 additions & 0 deletions errorx/dico.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package errorx

import (
"math"
)

// ErrNotDefine is a reserved error on the MaxUint64 value
// catch an undefine case
const (
indexReserved uint64 = math.MaxUint64
ErrNotDefine = "error not recorded in dictionnary errors"
)

// Dico describe a combo type to report an error message
type Dico map[uint64]string

// NewDico return a fresh and empty Dico
func NewDico() Dico {
d := make(map[uint64]string)
d[indexReserved] = ErrNotDefine
return d
}

// SetEntry record a new message to a given code
// You can erase the reserved index to personalize the relative message
func (d *Dico) SetEntry(code uint64, message string) {
map[uint64]string(*d)[code] = message
}

// Error create a new error from the dictionnary code
func (d Dico) Error(code uint64) *Errorx {
message, ok := d[code]
if !ok {
return New(indexReserved, d[indexReserved])
}
return New(code, message)
}
64 changes: 64 additions & 0 deletions errorx/dico_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package errorx

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewDico(t *testing.T) {
// default: should be ok
func() {
// init
d := NewDico()

// assert
if assert.NotNil(t, d) {
assert.Len(t, d, 1)
assert.Equal(t, ErrNotDefine, d[indexReserved])
}
}()
}

func TestSetEntry(t *testing.T) {
code := uint64(42)
message := "error example"

// default: should be ok
func() {
// init
d := NewDico()
d.SetEntry(code, message)

// assert
assert.Len(t, d, 2)
assert.Equal(t, message, d[code])
assert.Equal(t, ErrNotDefine, d[indexReserved])
}()
}

func TestDico_Error(t *testing.T) {
code0 := uint64(8)
code1 := uint64(42)
code2 := uint64(43)
code3 := uint64(44)
message1 := "error example_1"
message2 := "error example_2"
message3 := "error example_3"

// default: should be ok
func() {
// init
d := NewDico()
d.SetEntry(code1, message1)
d.SetEntry(code2, message2)
d.SetEntry(code3, message3)

// assert
assert.Len(t, d, 4)
assert.Equal(t, message1, d.Error(code1).Error())
assert.Equal(t, message2, d.Error(code2).Error())
assert.Equal(t, message3, d.Error(code3).Error())
assert.Equal(t, ErrNotDefine, d.Error(code0).Error())
}()
}
28 changes: 28 additions & 0 deletions errorx/errorx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errorx

const (
separator = " -> "
)

// Errorx take a status code to describe the message
// Implement a wrapper to embeded repport error
type Errorx struct {
code uint64
message string
}

// New return an errorx
func New(code uint64, message string) *Errorx {
return &Errorx{code: code, message: message}
}

// Error return the content and implement the error standard type
func (e Errorx) Error() string {
return e.message
}

// Wrap add a content error on the current error
func (e *Errorx) Wrap(message string) {
e.message = message + separator + e.message
return
}
57 changes: 57 additions & 0 deletions errorx/errorx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package errorx

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
code := uint64(42)
message := "error example"

// default: should be ok
func() {
// init
err := New(42, message)

// assert
if assert.NotNil(t, err) {
assert.Equal(t, message, err.message)
assert.Equal(t, code, err.code)
}
}()
}

func TestError(t *testing.T) {
message := "error example"

// default: should be ok
func() {
// init
var err error
err = New(uint64(0), message)

// assert
if assert.NotNil(t, err) {
assert.Equal(t, message, err.Error())
}
}()
}

func TestWrap(t *testing.T) {
code := uint64(0)
message1 := "error example"
message2 := "context error"

// default: should be ok
func() {
// init
err := New(code, message1)
err.Wrap(message2)

// assert
assert.Equal(t, code, err.code)
assert.Equal(t, message2+separator+message1, err.message)
}()
}
7 changes: 7 additions & 0 deletions errorx/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/ymohl-cl/gopkg/errorx

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
6 changes: 6 additions & 0 deletions errorx/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

0 comments on commit 0fc46d5

Please sign in to comment.