Skip to content

Commit

Permalink
[patch] fix messages being concatenated in stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Jan 25, 2023
1 parent 182c597 commit 591544f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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: "/",
Expand All @@ -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.
Expand Down
23 changes: 12 additions & 11 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/bnkamalesh/errors

go 1.19

0 comments on commit 591544f

Please sign in to comment.