gitlab.nesad.fit.vutbr.cz/blended/libblended@v0.0.0-20221202124402-2bee159339df/errors/README.md (about) 1 # BLENDED: Errors package 2 ### Main Goals 3 4 The main goals is to have the unified error handling with a good error stack track, possibility to create practical log system and provide a contextual error information to the user when necessary. 5 6 ### How to use 7 8 ###### Import 9 ToDo 10 11 ###### Dependencies 12 For the use of the package errors, you can need download two dependencies: 13 ```go 14 go get github.com/pkg/errors 15 go get github.com/stretchr/testify/assert 16 ``` 17 18 ###### Create Untyped Error 19 20 The untyped error is equal to classical error from standard "errors" package: 21 22 ```go 23 untypedError := errors.New("Untyped error message") 24 untypedError := errors.Newf("Untyped error with %s", "formating") 25 ``` 26 27 ###### Create Typed Error 28 29 The types represents additional information about errors. They are defined in `error.go` and the set can be also extended by simply adding lines: 30 31 ```go 32 const ( 33 // NoType error 34 NoType ErrorType = iota 35 // Ethereum error 36 EthereumError 37 // Collector error 38 CollectorError 39 // Runner error 40 RunnerError 41 // API interface error 42 APIError 43 ) 44 ``` 45 We can create a new typed error very simply 46 47 ```go 48 collectorError := errors.CollectorError.New("CollectorError") 49 collectorError := errors.CollectorError.Newf("CollectorError with %s", "formating") 50 51 // check the type 52 errors.GetType(err) == errors.CollectorError 53 ``` 54 ###### Wrap Error 55 56 To add information to error, `wrap` and `wrapf` can be used: 57 58 ```go 59 collectorError := errors.CollectorError.New("CollectorError") 60 err1 := errors.CollectorError.Wrap(collectorError, "Error from tests") 61 err2 := errors.CollectorError.Wrapf(collectorError, "Error from %s", "tests") 62 // err1 == err2: Error from tests: CollectorError 63 ``` 64 65 ###### Add context 66 67 To provide a contextual error information to the user when necessary. (e.g. the provided email is not in the right format): 68 69 ```go 70 errWithContext := errors.AddErrorContext(err, "id", "wrong id format, should be an integer") 71 // map[string]string{"field": "id", "message": "wrong id format, should be an integer"} 72 ``` 73 74 ###### Get cause of the error 75 Sometimes we want to get the original error message. For this purpose, the function `cause` can be used: 76 77 ```go 78 err := EthereumError.New("EthereumError") 79 ethereumError := Wrapf(err, "Error from %s", "tests") 80 fmt.Println(Cause(ethereumError)) 81 // prints EthereumError 82 ``` 83 84 ### Tests 85 - Unit tests in `errors_test.go` 86 87 ### Resources 88 - [Golang — handling errors gracefully](https://hackernoon.com/golang-handling-errors-gracefully-8e27f1db729f) 89 - [pkg/errors](https://github.com/pkg/errors) 90 - [Error Handling In Go](https://www.ardanlabs.com/blog/2014/10/error-handling-in-go-part-i.html) 91 - [Package errors](https://golang.org/pkg/errors/) 92 - [Working with Errors in Go 1.13](https://blog.golang.org/go1.13-errors) 93