diff --git a/README.md b/README.md index 1460195..cbf67cc 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![](https://godoc.org/github.com/nathany/looper?status.svg)](https://pkg.go.dev/github.com/bnkamalesh/errors?tab=doc) [![](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#error-handling) -# Errors v0.9.3 +# Errors v0.9.4 Errors package is a drop-in replacement of the built-in Go errors package. It lets you create errors of 11 different types, which should handle most of the use cases. Some of them are a bit too specific for web applications, but useful nonetheless. @@ -138,8 +138,8 @@ import ( "time" "github.com/bnkamalesh/errors" - "github.com/bnkamalesh/webgo/v4" - "github.com/bnkamalesh/webgo/v4/middleware" + "github.com/bnkamalesh/webgo/v6" + "github.com/bnkamalesh/webgo/v6/middleware/accesslog" ) func bar() error { @@ -178,7 +178,7 @@ func handler(w http.ResponseWriter, r *http.Request) { func routes() []*webgo.Route { return []*webgo.Route{ - &webgo.Route{ + { Name: "home", Method: http.MethodGet, Pattern: "/", @@ -195,13 +195,12 @@ func main() { Port: "8080", ReadTimeout: 15 * time.Second, WriteTimeout: 60 * time.Second, - }, routes()) + }, routes()...) - router.UseOnSpecialHandlers(middleware.AccessLog) - router.Use(middleware.AccessLog) + router.UseOnSpecialHandlers(accesslog.AccessLog) + router.Use(accesslog.AccessLog) router.Start() } - ``` [webgo](https://github.com/bnkamalesh/webgo) was used to illustrate the usage of the function, `errors.HTTPStatusCodeMessage`. It returns the appropriate http status code, user friendly message stored within, and a boolean value. Boolean value is `true` if the returned error of type \*Error. diff --git a/errors.go b/errors.go index 8d1a04d..4a7327c 100644 --- a/errors.go +++ b/errors.go @@ -81,26 +81,27 @@ func (e *Error) fileLine() string { // Error is the implementation of error interface func (e *Error) Error() string { - fl := bytes.NewBuffer(make([]byte, 0, 128)) - fl.WriteString(e.fileLine()) - if fl.Len() != 0 { - fl.WriteString(": ") + str := bytes.NewBuffer(make([]byte, 0, 128)) + str.WriteString(e.fileLine()) + if str.Len() != 0 { + str.WriteString(": ") } if e.original != nil { - fl.WriteString(e.message) - fl.WriteString("\n") - fl.WriteString(e.original.Error()) + str.WriteString(e.message) + str.WriteString("\n") + str.WriteString(e.original.Error()) + return str.String() } if e.message != "" { - fl.WriteString(e.message) - return fl.String() + str.WriteString(e.message) + return str.String() } - fl.WriteString(DefaultMessage) + str.WriteString(DefaultMessage) - return fl.String() + return str.String() } // ErrorWithoutFileLine prints the final string without the stack trace / file+line number diff --git a/go.mod b/go.mod index f416eb9..6e03d08 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/bnkamalesh/errors + +go 1.19