Skip to content

Commit

Permalink
fix: remove the custom stacktraces from the example
Browse files Browse the repository at this point in the history
To avoid exposing the "internal" part of the stacktraces and to keep
the example simpler, we'll just use the default %+v format output.
  • Loading branch information
matdurand authored and Southclaws committed Sep 15, 2023
1 parent e6d656d commit 84c038e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
20 changes: 12 additions & 8 deletions examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ curl http://localhost:3333/users/123 # will return a user in JSON
Running each curl should produce the following logs
```
2023/09/09 11:05:06 ERROR
db error: connection lost
examples/api/main.go:53
Could not get user
examples/api/main.go:54
db error: connection lost
examples/api/main.go:53
Could not get user
examples/api/main.go:54
<ftag>
examples/api/main.go:54
http_method=GET user_id=999 request_id=It73FDo3WC-000005 request_path=/users/999 remote_ip=127.0.0.1:52246 protocol=HTTP/1.1 error="Could not get user: db error: connection lost"
2023/09/09 11:05:06 INFO API Request request_id=It73FDo3WC-000005 request_path=/users/999 remote_ip=127.0.0.1:52246 protocol=HTTP/1.1 http_method=GET status=500 latency=96.25µs
2023/09/09 11:05:25 ERROR
db error: user id[321] not found
examples/api/main.go:62
User not found
examples/api/main.go:63
db error: user id[321] not found
examples/api/main.go:62
User not found
examples/api/main.go:63
<ftag>
examples/api/main.go:63
http_method=GET user_id=321 request_path=/users/321 remote_ip=127.0.0.1:52246 request_id=It73FDo3WC-000006 protocol=HTTP/1.1 error="User not found: db error: user id[321] not found"
2023/09/09 11:05:25 INFO API Request protocol=HTTP/1.1 http_method=GET request_path=/users/321 remote_ip=127.0.0.1:52246 request_id=It73FDo3WC-000006 status=404 latency=65.306µs
Expand Down
25 changes: 1 addition & 24 deletions examples/api/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package http
import (
"context"
"fmt"
"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fctx"
"github.com/Southclaws/fault/fmsg"
"github.com/Southclaws/fault/ftag"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"log/slog"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -70,27 +68,6 @@ func LoggerRequest(logger *slog.Logger) func(next http.Handler) http.Handler {

}

func isInternalString(s string) bool {
return strings.HasPrefix(s, "<") && strings.HasSuffix(s, ">")
}

func toStackTrace(err error) string {
var sb strings.Builder
u := fault.Flatten(err)
for _, v := range u {
if isInternalString(v.Message) {
continue
}
if v.Message != "" {
sb.WriteString(fmt.Sprintf("\t%s\n", v.Message))
}
if v.Location != "" {
sb.WriteString(fmt.Sprintf("\t\t%s\n", v.Location))
}
}
return sb.String()
}

func RespondWithError(
logger *slog.Logger,
err error,
Expand All @@ -100,7 +77,7 @@ func RespondWithError(
tag := ftag.Get(err)

attrs := fctxToSlog(r.Context())
errStr := toStackTrace(err)
errStr := fmt.Sprintf("%+v", err)
attrs = append(attrs, slog.String("error", err.Error()))
logger.Error("\n"+errStr, attrs...)

Expand Down
1 change: 1 addition & 0 deletions examples/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ func main() {
r.Get("/", GetUser)
})

fmt.Printf("Listening on :3333 ...\n")
http.ListenAndServe(":3333", r)
}

0 comments on commit 84c038e

Please sign in to comment.