diff --git a/assets/README.md b/assets/README.md deleted file mode 100644 index af7df3b..0000000 --- a/assets/README.md +++ /dev/null @@ -1,40 +0,0 @@ - -# Assets - -Generated on https://carbon.now.sh - -### motivation.png - -![](motivation.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=application%2Fjson&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=%257B%250A%2520%2520%2522time%2522%253A%2520%25222023-05-02%252005%253A26%253A48.570837Z%2522%252C%250A%2520%2520%2522level%2522%253A%2520%2522ERROR%2522%252C%250A%2520%2520%2522msg%2522%253A%2520%2522something%2520failed%253A%2520permission%2520denied%2522%252C%250A%2520%2520%2522error%2522%253A%2520%257B%250A%2520%2520%2520%2520%2522code%2522%253A%2520%2522iam_missing_permission%2522%252C%250A%2520%2520%2520%2520%2522context%2522%253A%2520%257B%250A%2520%2520%2520%2520%2520%2520%2522hello%2522%253A%2520%2522world%2522%252C%250A%2520%2520%2520%2520%2520%2520%2522permission%2522%253A%2520%2522post.create%2522%252C%250A%2520%2520%2520%2520%2520%2520%2522user_id%2522%253A%25201234%250A%2520%2520%2520%2520%257D%252C%250A%2520%2520%2520%2520%2522domain%2522%253A%2520%2522authz%2522%252C%250A%2520%2520%2520%2520%2522tags%2522%253A%2520%255B%2522iam%2522%252C%2520%2522authz%2522%255D%252C%250A%2520%2520%2520%2520%2522error%2522%253A%2520%2522something%2520failed%253A%2520permission%2520denied%2522%252C%250A%2520%2520%2520%2520%2522hint%2522%253A%2520%2522Runbook%253A%2520https%253A%252F%252Fdoc.acme.org%252Fdoc%252Fabcd.md%2522%252C%250A%2520%2520%2520%2520%2522stacktrace%2522%253A%250A%2520%2520%2520%2520%2520%2520%2520%2520%2522Oops%253A%2520permission%2520denied%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A20%2520%28d%29%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A24%2520%28c%29%250A%2520%2520%2520%2520%2520%2520%2520%2520Thrown%253A%2520something%2520failed%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A32%2520%28b%29%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A36%2520%28a%29%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A42%2520%28main%29%2522%252C%250A%2520%2520%2520%2520%2522time%2522%253A%2520%25222023-05-02%252005%253A26%253A48.570837Z%2522%252C%250A%2520%2520%2520%2520%2522trace%2522%253A%2520%2522e76031ee-a0c4-4a80-88cb-17086fdd19c0%2522%252C%250A%2520%2520%2520%2520%2522user%2522%253A%2520%257B%250A%2520%2520%2520%2520%2520%2520%2522firstname%2522%253A%2520%2522john%2522%252C%250A%2520%2520%2520%2520%2520%2520%2522id%2522%253A%2520%2522user-123%2522%252C%250A%2520%2520%2520%2520%2520%2520%2522lastname%2522%253A%2520%2522doe%2522%250A%2520%2520%2520%2520%257D%250A%2520%2520%257D%250A%257D - -### Stack trace 1 - -![](stacktrace1.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=text&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=Oops%253A%2520permission%2520denied%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A20%2520%28d%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A24%2520%28c%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A32%2520%28b%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A36%2520%28a%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A42%2520%28main%29 - -### Stack trace 2 - -![](stacktrace2.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=text&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=Oops%253A%2520permission%2520denied%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A20%2520%28d%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A24%2520%28c%29%250AThrown%253A%2520something%2520failed%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A32%2520%28b%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A36%2520%28a%29%250A%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A42%2520%28main%29 - -### Sources 1 - -![](sources1.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=auto&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=Oops%253A%2520permission%2520denied%250Agithub.com%252Fsamber%252Foops%252Fexamples%252Fsources%252Fexample.go%253A22%2520d%28%29%250A17%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Time%28time.Now%28%29%29.%250A18%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520With%28%2522user_id%2522%252C%25201234%29.%250A19%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520With%28%2522permission%2522%252C%2520%2522post.create%2522%29.%250A20%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Hint%28%2522Runbook%253A%2520https%253A%252F%252Fdoc.acme.org%252Fdoc%252Fabcd.md%2522%29.%250A21%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520User%28%2522user-123%2522%252C%2520%2522firstname%2522%252C%2520%2522john%2522%252C%2520%2522lastname%2522%252C%2520%2522doe%2522%29.%250A22%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Errorf%28%2522permission%2520denied%2522%29%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%250A23%2520%2520%2520%2520%2520%2520%257D%250A24%250A25%2520%2520%2520%2520%2520%2520func%2520c%28%29%2520error%2520%257B%250A26%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520return%2520d%28%29%250A27%2520%2520%2520%2520%2520%2520%257D%250A%250AThrown%253A%2520something%2520failed%250Agithub.com%252Fsamber%252Foops%252Fexamples%252Fsources%252Fexample.go%253A34%2520b%28%29%250A29%2520%2520%2520%2520%2520%2520func%2520b%28%29%2520error%2520%257B%250A30%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520return%2520oops.%250A31%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520In%28%2522iam%2522%29.%250A32%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Trace%28%25226710668a-2b2a-4de6-b8cf-3272a476a1c9%2522%29.%250A33%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520With%28%2522hello%2522%252C%2520%2522world%2522%29.%250A34%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520Wrapf%28c%28%29%252C%2520%2522something%2520failed%2522%29%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%255E%250A35%2520%2520%2520%2520%2520%2520%257D%250A36%250A37%2520%2520%2520%2520%2520%2520func%2520a%28%29%2520error%2520%257B%250A38%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520return%2520b%28%29%250A39%2520%2520%2520%2520%2520%2520%257D - -### Output 1 - -![](output-printf-plusv.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=text&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=Oops%253A%2520permission%2520denied%250ACode%253A%2520%2522iam_missing_permission%2522%250ATime%253A%25202023-05-02%252005%253A26%253A48.570837%2520%252B0000%2520UTC%250ADuration%253A%252042ms%250ADomain%253A%2520authz%250ATags%253A%2520iam%252C%2520authz%250ATrace%253A%2520092abdf7-a0ad-40cd-bfdc-d25a9435a87d%250AHint%253A%2520Runbook%253A%2520https%253A%252F%252Fdoc.acme.org%252Fdoc%252Fabcd.md%250AOwner%253A%2520authz-team%2540acme.org%250AContext%253A%250A%2520%2520*%2520user_id%253A%25201234%250AUser%253A%250A%2520%2520*%2520id%253A%2520user-123%250A%2520%2520*%2520firstname%253A%2520john%250A%2520%2520*%2520lastname%253A%2520doe%250AStacktrace%253A%250A%2520%2520Oops%253A%2520permission%2520denied%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A20%2520%28d%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A24%2520%28c%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A32%2520%28b%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A36%2520%28a%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A42%2520%28main%29 - -### Output 2 - -![](output-json.png) - -https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=a11y-dark&wt=none&l=application%2Fjson&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=13px&ph=15px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code=%257B%250A%2520%2520%2522code%2522%253A%2520%2522iam_missing_permission%2522%252C%250A%2520%2520%2522context%2522%253A%2520%257B%250A%2520%2520%2520%2520%2522user_id%2522%253A%25201234%250A%2520%2520%257D%252C%250A%2520%2520%2522domain%2522%253A%2520%2522authz%2522%252C%250A%2520%2520%2522tags%2522%253A%2520%255B%250A%2520%2520%2520%2520%2522iam%2522%252C%250A%2520%2520%2520%2520%2522authz%2522%250A%2520%2520%255D%252C%250A%2520%2520%2522error%2522%253A%2520%2522Permission%2520denied%2522%252C%250A%2520%2520%2522hint%2522%253A%2520%2522Runbook%253A%2520https%253A%252F%252Fdoc.acme.org%252Fdoc%252Fabcd.md%2522%252C%250A%2520%2520%2522time%2522%253A%2520%25222023-05-02T05%253A26%253A48.570837Z%2522%252C%250A%2520%2520%2522duration%2522%253A%2520%252242ms%2522%252C%250A%2520%2520%2522stacktrace%2522%253A%2520%2522Oops%253A%2520permission%2520denied%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A20%2520%28d%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A24%2520%28c%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A32%2520%28b%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A36%2520%28a%29%250A%2520%2520%2520%2520---%2520at%2520github.com%252Fsamber%252Foops%252Floggers%252Fslog%252Fexample.go%253A42%2520%28main%29%2522%252C%250A%2520%2520%2522trace%2522%253A%2520%25224ab0e35e-8414-4d76-b09e-cba80c983e4b%2522%252C%250A%2520%2520%2522user%2522%253A%2520%257B%250A%2520%2520%2520%2520%2522firstname%2522%253A%2520%2522john%2522%252C%250A%2520%2520%2520%2520%2522id%2522%253A%2520%2522user-123%2522%252C%250A%2520%2520%2520%2520%2522lastname%2522%253A%2520%2522doe%2522%250A%2520%2520%257D%250A%257D \ No newline at end of file diff --git a/assets/logo.png b/assets/logo.png deleted file mode 100644 index bd5a7fe..0000000 Binary files a/assets/logo.png and /dev/null differ diff --git a/assets/logo.xcf b/assets/logo.xcf deleted file mode 100644 index 54681ac..0000000 Binary files a/assets/logo.xcf and /dev/null differ diff --git a/assets/motivation.png b/assets/motivation.png deleted file mode 100644 index 5694110..0000000 Binary files a/assets/motivation.png and /dev/null differ diff --git a/assets/output-json.png b/assets/output-json.png deleted file mode 100644 index 0452769..0000000 Binary files a/assets/output-json.png and /dev/null differ diff --git a/assets/output-printf-plusv.png b/assets/output-printf-plusv.png deleted file mode 100644 index 5153cf5..0000000 Binary files a/assets/output-printf-plusv.png and /dev/null differ diff --git a/assets/sources1.png b/assets/sources1.png deleted file mode 100644 index 177084b..0000000 Binary files a/assets/sources1.png and /dev/null differ diff --git a/assets/stacktrace1.png b/assets/stacktrace1.png deleted file mode 100644 index e4541ee..0000000 Binary files a/assets/stacktrace1.png and /dev/null differ diff --git a/assets/stacktrace2.png b/assets/stacktrace2.png deleted file mode 100644 index b769345..0000000 Binary files a/assets/stacktrace2.png and /dev/null differ diff --git a/error_test.go b/error_test.go deleted file mode 100644 index 81e5195..0000000 --- a/error_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package oops - -import ( - "errors" - "io/fs" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestErrorsIs(t *testing.T) { - is := assert.New(t) - - err := Errorf("Error: %w", fs.ErrExist) - is.True(errors.Is(err, fs.ErrExist)) - - err = Wrap(fs.ErrExist) - is.True(errors.Is(err, fs.ErrExist)) - - err = Wrapf(fs.ErrExist, "Error: %w", assert.AnError) - is.True(errors.Is(err, fs.ErrExist)) - - err = Join(fs.ErrExist, assert.AnError) - is.True(errors.Is(err, fs.ErrExist)) - err = Join(assert.AnError, fs.ErrExist) - is.True(errors.Is(err, fs.ErrExist)) - - err = Recover(func() { - panic(fs.ErrExist) - }) - is.True(errors.Is(err, fs.ErrExist)) - - err = Recoverf(func() { - panic(fs.ErrExist) - }, "Error: %w", assert.AnError) - is.True(errors.Is(err, fs.ErrExist)) -} - -func TestErrorsAs(t *testing.T) { - is := assert.New(t) - - var anError error = &fs.PathError{Err: fs.ErrExist} - var target *fs.PathError - - err := Errorf("error: %w", anError) - is.True(errors.As(err, &target)) - - err = Wrap(anError) - is.True(errors.As(err, &target)) - - err = Wrapf(anError, "Error: %w", assert.AnError) - is.True(errors.As(err, &target)) - - err = Join(anError, assert.AnError) - is.True(errors.As(err, &target)) - err = Join(assert.AnError, anError) - is.True(errors.As(err, &target)) - - err = Recover(func() { - panic(anError) - }) - is.True(errors.As(err, &target)) - - err = Recoverf(func() { - panic(anError) - }, "Error: %w", assert.AnError) - is.True(errors.As(err, &target)) -} diff --git a/examples/log/README.md b/examples/log/README.md deleted file mode 100644 index ce317c1..0000000 --- a/examples/log/README.md +++ /dev/null @@ -1,8 +0,0 @@ - -# Example of log logger from stdlib - -Playground: https://go.dev/play/p/uNx3CcT-X40 - -```sh -go run examples/log/example.go -``` diff --git a/examples/log/example.go b/examples/log/example.go deleted file mode 100644 index b966ffb..0000000 --- a/examples/log/example.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "log" - "time" - - "github.com/samber/oops" -) - -// go run examples/log/example.go - -func d() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - Time(time.Now()). - With("user_id", 1234). - With("permission", func() any { return "post.create" }). // lazy evaluation - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - User("user-123", "firstname", "john", "lastname", "doe"). - Errorf("permission denied") -} - -func c() error { - return d() -} - -func b() error { - return oops. - In("iam"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - With("hello", "world"). - Wrapf(c(), "something failed") -} - -func a() error { - return b() -} - -func main() { - err := a() - if err != nil { - log.Printf("%+v", err) - } -} diff --git a/examples/log/go.mod b/examples/log/go.mod deleted file mode 100644 index 33def0e..0000000 --- a/examples/log/go.mod +++ /dev/null @@ -1,14 +0,0 @@ -module github.com/samber/oops/examples/log - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/log/go.sum b/examples/log/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/log/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/logrus/README.md b/examples/logrus/README.md deleted file mode 100644 index 66dbad6..0000000 --- a/examples/logrus/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of Logrus logger - -Playground: https://go.dev/play/p/-_7EBnceJ_A - -```sh -go run examples/logrus/example.go 2>&1 | jq -go run examples/logrus/example.go 2>&1 | jq .stacktrace -r -``` diff --git a/examples/logrus/example.go b/examples/logrus/example.go deleted file mode 100644 index 16fddd6..0000000 --- a/examples/logrus/example.go +++ /dev/null @@ -1,51 +0,0 @@ -package main - -import ( - "time" - - "github.com/samber/oops" - oopslogrus "github.com/samber/oops/loggers/logrus" - "github.com/sirupsen/logrus" -) - -// go run examples/logrus/example.go 2>&1 | jq -// go run examples/logrus/example.go 2>&1 | jq .stacktrace -r - -func d() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - Time(time.Now()). - With("user_id", 1234). - With("permission", "post.create"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - User("user-123", "firstname", "john", "lastname", "doe"). - Errorf("permission denied") -} - -func c() error { - return d() -} - -func b() error { - return oops. - In("iam"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - With("hello", "world"). - Wrapf(c(), "something failed") -} - -func a() error { - return b() -} - -func main() { - logrus.SetFormatter(oopslogrus.NewOopsFormatter(&logrus.JSONFormatter{ - PrettyPrint: true, - })) - - err := a() - if err != nil { - logrus.WithError(err).Error(err) - } -} diff --git a/examples/logrus/go.mod b/examples/logrus/go.mod deleted file mode 100644 index 89fd229..0000000 --- a/examples/logrus/go.mod +++ /dev/null @@ -1,15 +0,0 @@ -module github.com/samber/oops/examples/logrus - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/logrus/go.sum b/examples/logrus/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/logrus/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/panic/README.md b/examples/panic/README.md deleted file mode 100644 index f09a341..0000000 --- a/examples/panic/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of panic handling - -Playground: https://go.dev/play/p/uGwrFj9mII8 - -```sh -go run examples/panic/example.go 2>&1 | jq -go run examples/panic/example.go 2>&1 | jq .stacktrace -r -``` diff --git a/examples/panic/example.go b/examples/panic/example.go deleted file mode 100644 index 3c858f8..0000000 --- a/examples/panic/example.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "github.com/samber/oops" - oopslogrus "github.com/samber/oops/loggers/logrus" - "github.com/sirupsen/logrus" -) - -// go run examples/panic/example.go 2>&1 | jq -// go run examples/panic/example.go 2>&1 | jq .stacktrace -r - -func mayPanic() { - panic("permission denied") -} - -func handlePanic() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - With("permission", "post.create"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Recoverf(func() { - // ... - mayPanic() - // ... - }, "unexpected error") -} - -func main() { - logrus.SetFormatter(oopslogrus.NewOopsFormatter(&logrus.JSONFormatter{ - PrettyPrint: true, - })) - - err := handlePanic() - if err != nil { - logrus.WithError(err).Error(err) - } -} diff --git a/examples/panic/go.mod b/examples/panic/go.mod deleted file mode 100644 index 6f8260c..0000000 --- a/examples/panic/go.mod +++ /dev/null @@ -1,15 +0,0 @@ -module github.com/samber/oops/examples/panic - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/panic/go.sum b/examples/panic/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/panic/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/segfault/README.md b/examples/segfault/README.md deleted file mode 100644 index cc716d3..0000000 --- a/examples/segfault/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of segfault handling - -Playground: https://go.dev/play/p/66wkzJ-Rem1 - -```sh -go run examples/segfault/example.go 2>&1 | jq -go run examples/segfault/example.go 2>&1 | jq .stacktrace -r -``` diff --git a/examples/segfault/example.go b/examples/segfault/example.go deleted file mode 100644 index a9c2a76..0000000 --- a/examples/segfault/example.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "github.com/samber/oops" - oopslogrus "github.com/samber/oops/loggers/logrus" - "github.com/sirupsen/logrus" -) - -// go run examples/segfault/example.go 2>&1 | jq -// go run examples/segfault/example.go 2>&1 | jq .stacktrace -r - -func nilPointerException() { - var a *int - *a = 42 -} - -func handlePanic() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - With("permission", "post.create"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Recoverf(func() { - // ... - nilPointerException() - // ... - }, "unexpected error") -} - -func main() { - logrus.SetFormatter(oopslogrus.NewOopsFormatter(&logrus.JSONFormatter{ - PrettyPrint: true, - })) - - err := handlePanic() - if err != nil { - logrus.WithError(err).Error(err) - } -} diff --git a/examples/segfault/go.mod b/examples/segfault/go.mod deleted file mode 100644 index 0dd3484..0000000 --- a/examples/segfault/go.mod +++ /dev/null @@ -1,15 +0,0 @@ -module github.com/samber/oops/examples/segfault - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/segfault/go.sum b/examples/segfault/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/segfault/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/slog/README.md b/examples/slog/README.md deleted file mode 100644 index b10895f..0000000 --- a/examples/slog/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of slog logger - -Playground: https://go.dev/play/p/-X2ZnqjyDLu - -```sh -go run examples/slog/example.go | jq -go run examples/slog/example.go | jq .error.stacktrace -r -``` diff --git a/examples/slog/example.go b/examples/slog/example.go deleted file mode 100644 index c820572..0000000 --- a/examples/slog/example.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "fmt" - "log/slog" - "net/http" - "os" - "strings" - "time" - - "github.com/samber/oops" -) - -// go run examples/slog/example.go | jq -// go run examples/slog/example.go | jq .error.stacktrace -r - -func d() error { - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - Time(time.Now()). - With("user_id", 1234). - With("permission", "post.create"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - User("user-123", "firstname", "john", "lastname", "doe"). - Request(req, true). - Errorf("permission denied") -} - -func c() error { - return d() -} - -func b() error { - return oops. - In("iam"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - With("hello", "world"). - Wrapf(c(), "something failed") -} - -func a() error { - return b() -} - -type myError1 struct { - err error -} - -func (e myError1) Error() string { - return fmt.Errorf("fuck %w", e.err).Error() -} - -func (c myError1) Unwrap() error { - if c.err != nil { - return c.err - } - return nil -} - -type myError2 struct { - err error -} - -func (e myError2) Error() string { - return fmt.Errorf("fuck %w", e.err).Error() -} - -func (c myError2) Unwrap() error { - if c.err != nil { - return c.err - } - return nil -} - -func main() { - logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) - - err := a() - if err != nil { - logger.Error( - err.Error(), - slog.Any("error", err), - ) - } -} diff --git a/examples/slog/go.mod b/examples/slog/go.mod deleted file mode 100644 index 3712078..0000000 --- a/examples/slog/go.mod +++ /dev/null @@ -1,14 +0,0 @@ -module github.com/samber/oops/examples/slog - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/slog/go.sum b/examples/slog/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/slog/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/sources/README.md b/examples/sources/README.md deleted file mode 100644 index 35f9624..0000000 --- a/examples/sources/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of print sources - -Playground: https://go.dev/play/p/6dEVisE1jfq - -```sh -go run examples/sources/example.go | jq -go run examples/sources/example.go | jq .error.sources -r -``` diff --git a/examples/sources/example.go b/examples/sources/example.go deleted file mode 100644 index 83fbe3e..0000000 --- a/examples/sources/example.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - "os" - "time" - - "log/slog" - - "github.com/samber/oops" -) - -// go run examples/sources/example.go | jq .error.sources -r - -func f() error { - return fmt.Errorf("permission denied") -} - -func e() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - Time(time.Now()). - With("user_id", 1234). - With("permission", "post.create"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - User("user-123", "firstname", "john", "lastname", "doe"). - Wrap(f()) -} - -func d() error { - return oops.Wrap(e()) -} - -func c() error { - return d() -} - -func b() error { - return oops. - In("iam"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - With("hello", "world"). - Wrapf(c(), "something failed") -} - -func a() error { - return b() -} - -func main() { - oops.SourceFragmentsHidden = false - - logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) - - err := a() - if err != nil { - logger.Error( - err.Error(), - slog.Any("error", err), - ) - } -} diff --git a/examples/sources/go.mod b/examples/sources/go.mod deleted file mode 100644 index d88163b..0000000 --- a/examples/sources/go.mod +++ /dev/null @@ -1,15 +0,0 @@ -module github.com/samber/oops/examples/sources - -go 1.21 - -require ( - github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/examples/sources/go.sum b/examples/sources/go.sum deleted file mode 100644 index 61a5bea..0000000 --- a/examples/sources/go.sum +++ /dev/null @@ -1,25 +0,0 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/slog-formatter v0.3.3 h1:VCoKANbPtXf00CtnvKn/BZ7gcH1dBBnm48PAH854ynQ= -github.com/samber/slog-formatter v0.3.3/go.mod h1:C8LO3jmgtpSAxw7pm9xLjPcJ/h4qzw3OfVIMASyEKQ0= -github.com/samber/slog-multi v0.4.0 h1:QTQAo+9AP295irccqKdNwJ/2XflRMuL/aHqk7RblOhE= -github.com/samber/slog-multi v0.4.0/go.mod h1:QDicB1R5oTcbSSqlYmskphC7fNcjHgdxqZdns1eAvDc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/zerolog/README.md b/examples/zerolog/README.md deleted file mode 100644 index 17b50c4..0000000 --- a/examples/zerolog/README.md +++ /dev/null @@ -1,9 +0,0 @@ - -# Example of Zerolog logger - -Playground: https://go.dev/play/p/DaHzR4Zc-jj - -```sh -go run examples/zerolog/example.go 2>&1 | jq -go run examples/zerolog/example.go 2>&1 | jq .stacktrace -r -``` diff --git a/examples/zerolog/example.go b/examples/zerolog/example.go deleted file mode 100644 index af32eeb..0000000 --- a/examples/zerolog/example.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "os" - "time" - - "github.com/rs/zerolog" - "github.com/samber/oops" -) - -func d() error { - return oops. - Code("iam_authz_missing_permission"). - In("authz"). - Time(time.Now()). - With("user_id", 1234). - With("permission", "post.create"). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - User("user-123", "firstname", "john", "lastname", "doe"). - Errorf("permission denied") -} - -func c() error { - return d() -} - -func b() error { - return oops. - In("iam"). - Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9"). - With("hello", "world"). - Wrapf(c(), "something failed") -} - -func a() error { - return b() -} - -func WithOops(l zerolog.Logger, err error) *zerolog.Logger { - if oopsErr, ok := oops.AsOops(err); ok { - ctx := l.With() - for k, v := range oopsErr.ToMap() { - ctx = ctx.Interface(k, v) - } - - logger := ctx.Err(oopsErr).Logger() - return &logger - } - - // Recursively call into ourself so we can at least get a stack trace for any error - return WithOops(l, oops.Wrap(err)) -} - -func main() { - logger := zerolog. - New(os.Stderr). - With(). - Timestamp(). - Logger() - - err := a() - - WithOops(logger, err).Error().Send() -} diff --git a/examples/zerolog/go.mod b/examples/zerolog/go.mod deleted file mode 100644 index 6c3f552..0000000 --- a/examples/zerolog/go.mod +++ /dev/null @@ -1,17 +0,0 @@ -module github.com/samber/oops/examples/zerolog - -go 1.21 - -require ( - github.com/rs/zerolog v1.31.0 - github.com/samber/oops v1.9.2 -) - -require ( - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect - github.com/oklog/ulid/v2 v2.1.0 // indirect - github.com/samber/lo v1.38.1 // indirect - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect - golang.org/x/sys v0.12.0 // indirect -) diff --git a/examples/zerolog/go.sum b/examples/zerolog/go.sum deleted file mode 100644 index 582376f..0000000 --- a/examples/zerolog/go.sum +++ /dev/null @@ -1,24 +0,0 @@ -github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= -github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= -github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= -github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= -github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= -github.com/samber/oops v1.9.2 h1:ph8WaOk5pEMetbtp+UTcdrdaggk8E8gKhpunShAKyIM= -github.com/samber/oops v1.9.2/go.mod h1:xEXk4BLyqajkvCxzpxBnqfzHzRLFk3+g4E+tOJtOPZY= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/go.mod b/go.mod index 1eab908..125e7a7 100644 --- a/go.mod +++ b/go.mod @@ -7,16 +7,11 @@ go 1.21 require ( github.com/oklog/ulid/v2 v2.1.0 github.com/samber/lo v1.47.0 - github.com/stretchr/testify v1.10.0 go.opentelemetry.io/otel/trace v1.29.0 - go.uber.org/goleak v1.3.0 ) require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.10.0 // indirect go.opentelemetry.io/otel v1.29.0 // indirect golang.org/x/text v0.16.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 8f9b951..8e4f50d 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,7 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= @@ -20,12 +15,7 @@ go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw= go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8= go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4= go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helper_test.go b/helper_test.go deleted file mode 100644 index c24441b..0000000 --- a/helper_test.go +++ /dev/null @@ -1,6 +0,0 @@ -package oops - -func withoutStacktrace(o OopsError) OopsError { - o.stacktrace = nil - return o -} diff --git a/kv_test.go b/kv_test.go deleted file mode 100644 index 513665e..0000000 --- a/kv_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package oops - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestDereferencePointers(t *testing.T) { - is := assert.New(t) - - ptr := func(v string) *string { return &v } - - err := With("hello", "world").Errorf(assert.AnError.Error()).(OopsError) //nolint:govet - is.EqualValues(map[string]any{"hello": "world"}, err.Context()) - - err = With("hello", ptr("world")).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet - is.EqualValues(map[string]any{"hello": "world"}, err.Context()) - - err = With("hello", nil).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet - is.EqualValues(map[string]any{"hello": nil}, err.Context()) -} diff --git a/main_test.go b/main_test.go deleted file mode 100644 index 7b007e1..0000000 --- a/main_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package oops - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/oops_test.go b/oops_test.go deleted file mode 100644 index df9ce54..0000000 --- a/oops_test.go +++ /dev/null @@ -1,649 +0,0 @@ -package oops - -import ( - "context" - "encoding/json" - "fmt" - "log/slog" - "net/http" - "strings" - "testing" - "time" - - "github.com/samber/lo" - "github.com/stretchr/testify/assert" - "go.opentelemetry.io/otel/trace" -) - -func TestOopsWrap(t *testing.T) { - is := assert.New(t) - - err := new().Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Empty(err.(OopsError).msg) - is.Equal("assert.AnError general error for testing", err.Error()) - - err = new().Wrap(nil) - is.Nil(err) -} - -func TestOopsWrapf(t *testing.T) { - is := assert.New(t) - - err := new().Wrapf(assert.AnError, "a message %d", 42) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("a message 42", err.(OopsError).msg) - is.Equal("a message 42: assert.AnError general error for testing", err.Error()) - - err = new().Wrapf(nil, "a message %d", 42) - is.Nil(err) -} - -func TestOopsFromContext(t *testing.T) { - is := assert.New(t) - - domain := "domain" - key, val := "foo", "bar" - builder := new().In(domain).With(key, val).WithContext(context.Background()) - ctx := WithBuilder(context.Background(), builder) - - err := FromContext(ctx).Errorf("a message %d", 42) - is.Error(err) - is.Equal(domain, err.(OopsError).domain) - is.Equal(val, err.(OopsError).context[key]) -} - -func TestOopsErrorf(t *testing.T) { - is := assert.New(t) - - err := new().Errorf("a message %d", 42) - is.Error(err) - is.Equal(fmt.Errorf("a message %d", 42), err.(OopsError).err) - is.Empty(err.(OopsError).msg) - is.Equal("a message 42", err.Error()) -} - -func TestOopsCode(t *testing.T) { - is := assert.New(t) - - err := new().Code("iam_missing_permission").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("iam_missing_permission", err.(OopsError).code) -} - -func TestOopsTime(t *testing.T) { - is := assert.New(t) - - now := time.Now() - - err := new().Time(now).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(now, err.(OopsError).time) -} - -func TestOopsSince(t *testing.T) { - is := assert.New(t) - - start := time.Now() - time.Sleep(10 * time.Millisecond) - - err := new().Since(start).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.True(err.(OopsError).duration.Milliseconds() >= 10) -} - -func TestOopsDuration(t *testing.T) { - is := assert.New(t) - - start := time.Now() - time.Sleep(10 * time.Millisecond) - - err := new().Duration(time.Since(start)).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.True(err.(OopsError).duration.Milliseconds() >= 10) -} - -func TestOopsIn(t *testing.T) { - is := assert.New(t) - - err := new().In("authz").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("authz", err.(OopsError).domain) -} - -func TestOopsTags(t *testing.T) { - is := assert.New(t) - - err := new().Tags("iam", "authz", "iam").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal([]string{"iam", "authz", "iam"}, err.(OopsError).tags) // not deduplicated - is.Equal([]string{"iam", "authz"}, err.(OopsError).Tags()) // deduplicated -} - -func TestOopsTx(t *testing.T) { - is := assert.New(t) - - err := new().Trace("1234").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("1234", err.(OopsError).trace) -} - -func TestOopsTxSpanFromOtel(t *testing.T) { - is := assert.New(t) - - traceId, terr := trace.TraceIDFromHex("12345678901234567890123456789012") - is.NoError(terr) - spanId, serr := trace.SpanIDFromHex("1234567890123456") - is.NoError(serr) - - ctx := trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{ - TraceID: traceId, - SpanID: spanId, - })) - - err := new().WithContext(ctx).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("12345678901234567890123456789012", err.(OopsError).trace) - is.Equal("1234567890123456", err.(OopsError).span) -} - -func TestOopsWith(t *testing.T) { - is := assert.New(t) - - err := new().With("user_id", 1234).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"user_id": 1234}, err.(OopsError).context) - - err = new().With("user_id", 1234, "foo").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"user_id": 1234}, err.(OopsError).context) - - err = new().With("user_id", 1234, "foo", "bar").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"user_id": 1234, "foo": "bar"}, err.(OopsError).context) -} - -func TestOopsWithContext(t *testing.T) { - is := assert.New(t) - - type test string - const fooo test = "fooo" - - ctx := context.WithValue(context.Background(), "foo", "bar") //nolint:staticcheck - ctx = context.WithValue(ctx, fooo, "baz") - - // string - err := new().WithContext(ctx, "foo").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"foo": "bar"}, err.(OopsError).context) - - // type alias - err = new().WithContext(ctx, fooo).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"fooo": "baz"}, err.(OopsError).context) - - // multiple - err = new().WithContext(ctx, "foo", fooo).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"foo": "bar", "fooo": "baz"}, err.(OopsError).context) - - // not found - err = new().WithContext(ctx, "bar").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{"bar": nil}, err.(OopsError).context) - - // none - err = new().WithContext(ctx).Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal(map[string]any{}, err.(OopsError).context) -} - -func TestOopsWithLazyEvaluation(t *testing.T) { - is := assert.New(t) - - // lazy evaluation - err := new().With("user_id", func() int { return 1234 }, "foo", map[string]any{"bar": func() string { return "baz" }}).Wrap(assert.AnError) - is.Error(err) - is.Equal(map[string]any{"user_id": 1234, "foo": map[string]any{"bar": "baz"}}, err.(OopsError).Context()) -} - -func TestOopsHint(t *testing.T) { - is := assert.New(t) - - err := new().Hint("Runbook: https://doc.acme.org/doc/abcd.md").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("Runbook: https://doc.acme.org/doc/abcd.md", err.(OopsError).hint) -} - -func TestOopsPublic(t *testing.T) { - is := assert.New(t) - - err := new().Public("a public facing message").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("a public facing message", err.(OopsError).public) -} - -func TestOopsOwner(t *testing.T) { - is := assert.New(t) - - err := new().Owner("iam-team@acme.org").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("iam-team@acme.org", err.(OopsError).owner) -} - -func TestOopsUser(t *testing.T) { - is := assert.New(t) - - err := new().User("user-123").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("user-123", err.(OopsError).userID) - is.Equal(map[string]any{}, err.(OopsError).userData) - - err = new().User("user-123", "firstname", "john").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("user-123", err.(OopsError).userID) - is.Equal(map[string]any{"firstname": "john"}, err.(OopsError).userData) - - err = new().User("user-123", "firstname", "john", "lastname").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("user-123", err.(OopsError).userID) - is.Equal(map[string]any{"firstname": "john"}, err.(OopsError).userData) - - err = new().User("user-123", "firstname", "john", "lastname", "doe").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("user-123", err.(OopsError).userID) - is.Equal(map[string]any{"firstname": "john", "lastname": "doe"}, err.(OopsError).userData) -} - -func TestOopsTenant(t *testing.T) { - is := assert.New(t) - - err := new().Tenant("workspace-123").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("workspace-123", err.(OopsError).tenantID) - is.Equal(map[string]any{}, err.(OopsError).tenantData) - - err = new().Tenant("workspace-123", "name", "My 'hello world' project").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("workspace-123", err.(OopsError).tenantID) - is.Equal(map[string]any{"name": "My 'hello world' project"}, err.(OopsError).tenantData) - - err = new().Tenant("workspace-123", "name", "My 'hello world' project", "date").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("workspace-123", err.(OopsError).tenantID) - is.Equal(map[string]any{"name": "My 'hello world' project"}, err.(OopsError).tenantData) - - err = new().Tenant("workspace-123", "name", "My 'hello world' project", "date", "2023-01-01").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("workspace-123", err.(OopsError).tenantID) - is.Equal(map[string]any{"name": "My 'hello world' project", "date": "2023-01-01"}, err.(OopsError).tenantData) -} - -func TestOopsRequest(t *testing.T) { - is := assert.New(t) - - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new().Request(req, false).Wrap(assert.AnError) - is.Error(err) - is.Equal(err.(OopsError).err, assert.AnError) - is.NotNil(err.(OopsError).req) - if err.(OopsError).req != nil { - is.Equal(req, err.(OopsError).req.A) - is.False(err.(OopsError).req.B) - } - is.NotNil(err.(OopsError).Request()) - if err.(OopsError).Request() != nil { - is.Equal(req, err.(OopsError).Request()) - } - - err = new().Request(req, true).Wrap(assert.AnError) - is.Error(err) - is.Equal(err.(OopsError).err, assert.AnError) - is.NotNil(err.(OopsError).req) - if err.(OopsError).req != nil { - is.Equal(req, err.(OopsError).req.A) - is.True(err.(OopsError).req.B) - } - is.NotNil(err.(OopsError).Request()) - if err.(OopsError).Request() != nil { - is.Equal(req, err.(OopsError).Request()) - } -} - -func TestOopsMixed(t *testing.T) { - is := assert.New(t) - - now := time.Now() - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new(). - Code("iam_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Trace("1234"). - With("user_id", 1234). - WithContext(context.WithValue(context.Background(), "foo", "bar"), "foo"). //nolint:staticcheck - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - Owner("authz-team@acme.org"). - User("user-123", "firstname", "john", "lastname", "doe"). - Tenant("workspace-123", "name", "little project"). - Request(req, false). - Wrapf(assert.AnError, "a message %d", 42) - is.Error(err) - is.Equal(err.(OopsError).code, "iam_missing_permission") - is.Equal(err.(OopsError).time, now) - is.Equal(err.(OopsError).duration, time.Second) - is.Equal(err.(OopsError).domain, "authz") - is.Equal(err.(OopsError).trace, "1234") - is.Equal(err.(OopsError).context, map[string]any{"user_id": 1234, "foo": "bar"}) - is.Equal(err.(OopsError).hint, "Runbook: https://doc.acme.org/doc/abcd.md") - is.Equal(err.(OopsError).public, "public facing message") - is.Equal(err.(OopsError).owner, "authz-team@acme.org") - is.Equal(err.(OopsError).userID, "user-123") - is.Equal(err.(OopsError).userData, map[string]any{"firstname": "john", "lastname": "doe"}) - is.Equal(err.(OopsError).tenantID, "workspace-123") - is.Equal(err.(OopsError).tenantData, map[string]any{"name": "little project"}) - is.Equal(err.(OopsError).req, lo.ToPtr(lo.T2(req, false))) - is.Equal(err.(OopsError).err, assert.AnError) - is.Equal(err.(OopsError).msg, "a message 42") -} - -func TestOopsMixedWithGetters(t *testing.T) { - is := assert.New(t) - - now := time.Now() - req1, _ := http.NewRequest("POST", "http://localhost:1337/foo", strings.NewReader("hello world")) - req2, _ := http.NewRequest("POST", "http://localhost:1337/bar", strings.NewReader("hello world")) - - err := new(). - Code("iam_authz_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Trace("1234"). - With("user_id", 1234). - Hint("Runbook: https://doc.acme.org/doc/1234.md"). - Public("public facing message"). - Owner("authz-team@acme.org"). - User("user-123", "firstname", "bob", "lastname", "martin"). - Tenant("workspace-123", "name", "little project"). - Request(req1, true). - Wrapf(assert.AnError, "a message %d", 42) - - err = new(). - Code("iam_unknown_error"). - Time(now.Add(time.Hour)). - Duration(2*time.Second). - In("iam"). - Trace("abcd"). - With("workspace_id", 5678). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - Owner("iam-team@acme.org"). - User("user-123", "firstname", "john", "lastname", "doe", "email", "john@doe.org"). - Tenant("workspace-123", "name", "little project", "deleted", false). - Request(req2, true). - Wrapf(err, "hello world") - - // current error - is.Error(err) - is.Equal(err.(OopsError).Code(), "iam_authz_missing_permission") - is.Equal(err.(OopsError).Time(), now) - is.Equal(err.(OopsError).Duration(), time.Second) - is.Equal(err.(OopsError).Domain(), "authz") - is.Equal(err.(OopsError).Trace(), "1234") - is.Equal(err.(OopsError).Context(), map[string]any{"user_id": 1234, "workspace_id": 5678}) - is.Equal(err.(OopsError).Hint(), "Runbook: https://doc.acme.org/doc/1234.md") - is.Equal(err.(OopsError).Public(), "public facing message") - is.Equal(err.(OopsError).Owner(), "authz-team@acme.org") - is.Equal(lo.T2(err.(OopsError).User()), lo.T2("user-123", map[string]any{"firstname": "bob", "lastname": "martin", "email": "john@doe.org"})) - is.Equal(lo.T2(err.(OopsError).Tenant()), lo.T2("workspace-123", map[string]any{"name": "little project", "deleted": false})) - is.Equal(err.(OopsError).Request(), req1) - is.Equal(err.(OopsError).Error(), "hello world: a message 42: assert.AnError general error for testing") - - // first-level error - is.Error(err) - is.Equal(err.(OopsError).code, "iam_unknown_error") - is.Equal(err.(OopsError).time, now.Add(time.Hour)) - is.Equal(err.(OopsError).duration, 2*time.Second) - is.Equal(err.(OopsError).domain, "iam") - is.Equal(err.(OopsError).trace, "abcd") - is.Equal(err.(OopsError).context, map[string]any{"workspace_id": 5678}) - is.Equal(err.(OopsError).hint, "Runbook: https://doc.acme.org/doc/abcd.md") - is.Equal(err.(OopsError).public, "public facing message") - is.Equal(err.(OopsError).owner, "iam-team@acme.org") - is.Equal(err.(OopsError).userID, "user-123") - is.Equal(err.(OopsError).userData, map[string]any{"email": "john@doe.org", "firstname": "john", "lastname": "doe"}) - is.Equal(err.(OopsError).tenantID, "workspace-123") - is.Equal(err.(OopsError).tenantData, map[string]any{"deleted": false, "name": "little project"}) - is.Equal(err.(OopsError).req, lo.ToPtr(lo.T2(req2, true))) - is.Equal(err.(OopsError).err.Error(), "a message 42: assert.AnError general error for testing") - is.Equal(err.(OopsError).msg, "hello world") - - // deepest error - is.Equal(err.(OopsError).Unwrap().(OopsError).code, "iam_authz_missing_permission") - is.Equal(err.(OopsError).Unwrap().(OopsError).time, now) - is.Equal(err.(OopsError).Unwrap().(OopsError).duration, time.Second) - is.Equal(err.(OopsError).Unwrap().(OopsError).domain, "authz") - is.Equal(err.(OopsError).Unwrap().(OopsError).trace, "1234") - is.Equal(err.(OopsError).Unwrap().(OopsError).context, map[string]any{"user_id": 1234}) - is.Equal(err.(OopsError).Unwrap().(OopsError).hint, "Runbook: https://doc.acme.org/doc/1234.md") - is.Equal(err.(OopsError).Unwrap().(OopsError).public, "public facing message") - is.Equal(err.(OopsError).Unwrap().(OopsError).owner, "authz-team@acme.org") - is.Equal(err.(OopsError).Unwrap().(OopsError).userID, "user-123") - is.Equal(err.(OopsError).Unwrap().(OopsError).userData, map[string]any{"firstname": "bob", "lastname": "martin"}) - is.Equal(err.(OopsError).Unwrap().(OopsError).tenantID, "workspace-123") - is.Equal(err.(OopsError).Unwrap().(OopsError).tenantData, map[string]any{"name": "little project"}) - is.Equal(err.(OopsError).Unwrap().(OopsError).req, lo.ToPtr(lo.T2(req1, true))) - is.Equal(err.(OopsError).Unwrap().(OopsError).err.Error(), assert.AnError.Error()) - is.Equal(err.(OopsError).Unwrap().(OopsError).msg, "a message 42") -} - -func TestOopsLogValuer(t *testing.T) { - is := assert.New(t) - - now := time.Now() - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new(). - Code("iam_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Tags("iam", "authz"). - Trace("1234"). - With("user_id", 1234). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - Owner("authz-team@acme.org"). - User("user-123", "firstname", "john"). - Tenant("workspace-123", "name", "little project"). - Request(req, true). - Wrapf(assert.AnError, "a message %d", 42) - - is.Error(err) - - got := err.(OopsError).LogValuer().Group() - expectedAttrs := []slog.Attr{ - slog.String("message", "a message 42"), - slog.String("err", "a message 42: assert.AnError general error for testing"), - slog.String("code", "iam_missing_permission"), - slog.Time("time", now.UTC()), - slog.Duration("duration", time.Second), - slog.String("domain", "authz"), - slog.Any("tags", []string{"iam", "authz"}), - slog.String("trace", "1234"), - slog.String("hint", "Runbook: https://doc.acme.org/doc/abcd.md"), - slog.String("public", "public facing message"), - slog.String("owner", "authz-team@acme.org"), - slog.Group( - "context", - slog.Int("user_id", 1234), - ), - slog.Group( - "user", - slog.String("id", "user-123"), - slog.String("firstname", "john"), - ), - slog.Group( - "tenant", - slog.String("id", "workspace-123"), - slog.String("name", "little project"), - ), - slog.String("request", "POST /foobar HTTP/1.1\r\nHost: localhost:1337\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 11\r\nAccept-Encoding: gzip\r\n\r\nhello world"), - slog.String("stacktrace", err.(OopsError).Stacktrace()), - } - - is.Len(got, len(expectedAttrs)) - for i := range got { - is.Equal(expectedAttrs[i].Key, got[i].Key) - is.Equal(expectedAttrs[i].Value.Kind(), got[i].Value.Kind()) - is.EqualValues(expectedAttrs[i].Value.Any(), got[i].Value.Any()) - } -} - -func TestOopsFormatSummary(t *testing.T) { - is := assert.New(t) - - now := time.Now() - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new(). - Code("iam_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Trace("1234"). - With("user_id", 1234). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - Owner("authz-team@acme.org"). - User("user-123", "firstname", "john", "lastname", "doe"). - Tenant("workspace-123", "name", "little project"). - Request(req, true). - Wrapf(assert.AnError, "a message %d", 42) - - expected := "a message 42: assert.AnError general error for testing" - is.Equal(expected, fmt.Sprintf("%v", err.(OopsError))) -} - -func TestOopsFormatVerbose(t *testing.T) { - is := assert.New(t) - - now, _ := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2023-05-02 05:26:48.570837 +0000 UTC") - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new(). - Code("iam_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Trace("1234"). - With("user_id", 1234). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - Owner("authz-team@acme.org"). - User("user-123", "firstname", "john"). - Tenant("workspace-123", "name", "little project"). - Request(req, true). - Wrapf(assert.AnError, "a message %d", 42) - - expected := `Oops: a message 42: assert.AnError general error for testing -Code: iam_missing_permission -Time: 2023-05-02 05:26:48.570837 +0000 UTC -Duration: 1s -Domain: authz -Trace: 1234 -Hint: Runbook: https://doc.acme.org/doc/abcd.md -Owner: authz-team@acme.org -Context: - * user_id: 1234 -User: - * id: user-123 - * firstname: john -Tenant: - * id: workspace-123 - * name: little project -Request: - * POST /foobar HTTP/1.1 - * Host: localhost:1337 - * User-Agent: Go-http-client/1.1 - * Content-Length: 11 - * Accept-Encoding: gzip - * - * hello world -` - - got := fmt.Sprintf("%+v", withoutStacktrace(err.(OopsError))) - got = strings.ReplaceAll(got, "\r", "") // remove \r from request - is.Equal(expected, got) -} - -func TestOopsMarshalJSON(t *testing.T) { - is := assert.New(t) - - now, _ := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2023-05-02 05:26:48.570837 +0200 UTC") - req, _ := http.NewRequest("POST", "http://localhost:1337/foobar", strings.NewReader("hello world")) - - err := new(). - Code("iam_missing_permission"). - Time(now). - Duration(time.Second). - In("authz"). - Trace("1234"). - With("user_id", 1234). - Hint("Runbook: https://doc.acme.org/doc/abcd.md"). - Public("public facing message"). - User("user-123", "firstname", "john", "lastname", "doe"). - Tenant("workspace-123", "name", "little project"). - Request(req, true). - Wrapf(assert.AnError, "a message %d", 42) - - expected := `{"code":"iam_missing_permission","context":{"user_id":1234},"domain":"authz","duration":"1s","error":"a message 42: assert.AnError general error for testing","hint":"Runbook: https://doc.acme.org/doc/abcd.md","public":"public facing message","request":"POST /foobar HTTP/1.1\r\nHost: localhost:1337\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 11\r\nAccept-Encoding: gzip\r\n\r\nhello world","tenant":{"id":"workspace-123","name":"little project"},"time":"2023-05-02T05:26:48.570837Z","trace":"1234","user":{"firstname":"john","id":"user-123","lastname":"doe"}}` - - got, err := json.Marshal(withoutStacktrace(err.(OopsError))) - is.NoError(err) - is.Equal(expected, string(got)) -} - -func TestOopsGetPublic(t *testing.T) { - is := assert.New(t) - - err := new().Public("public facing message").Wrap(assert.AnError) - is.Error(err) - is.Equal(assert.AnError, err.(OopsError).err) - is.Equal("public facing message", GetPublic(err, "default message")) - is.Equal("default message", GetPublic(assert.AnError, "default message")) -} diff --git a/stacktrace_cleanpath_test.go b/stacktrace_cleanpath_test.go deleted file mode 100644 index 7d77f71..0000000 --- a/stacktrace_cleanpath_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package oops - -/// -/// Stolen from palantir/stacktrace repo -/// -> https://github.com/palantir/stacktrace/blob/master/cleanpath/gopath_test.go -/// -> Apache 2.0 LICENSE -/// - -import ( - "os" - "path/filepath" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestRemoveGoPath(t *testing.T) { - for _, testcase := range []struct { - gopath []string - path string - expected string - }{ - { - // empty gopath - gopath: []string{}, - path: "/some/dir/src/pkg/prog.go", - expected: "/some/dir/src/pkg/prog.go", - }, - { - // single matching dir in gopath - gopath: []string{"/some/dir"}, - path: "/some/dir/src/pkg/prog.go", - expected: "pkg/prog.go", - }, - { - // nonmatching dir in gopath - gopath: []string{"/other/dir"}, - path: "/some/dir/src/pkg/prog.go", - expected: "/some/dir/src/pkg/prog.go", - }, - { - // multiple matching dirs in gopath, shorter first - gopath: []string{"/some", "/some/src/dir"}, - path: "/some/src/dir/src/pkg/prog.go", - expected: "pkg/prog.go", - }, - { - // multiple matching dirs in gopath, longer first - gopath: []string{"/some/src/dir", "/some"}, - path: "/some/src/dir/src/pkg/prog.go", - expected: "pkg/prog.go", - }, - } { - gopath := strings.Join(testcase.gopath, string(filepath.ListSeparator)) - err := os.Setenv("GOPATH", gopath) - assert.NoError(t, err, "error setting gopath") - - cleaned := removeGoPath(testcase.path) - assert.Equal(t, testcase.expected, cleaned, "testcase: %+v", testcase) - } -} diff --git a/stacktrace_test.go b/stacktrace_test.go deleted file mode 100644 index 1cb2373..0000000 --- a/stacktrace_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package oops - -import ( - "runtime/debug" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func a() *oopsStacktrace { - return b() -} - -func b() *oopsStacktrace { - return c() -} - -func c() *oopsStacktrace { - return d() -} - -func d() *oopsStacktrace { - return e() -} - -func e() *oopsStacktrace { - return f() -} - -func f() *oopsStacktrace { - return newStacktrace("1234") -} - -func TestStacktrace(t *testing.T) { - is := assert.New(t) - - st := a() - - is.NotNil(st) - is.Equal("1234", st.span) - - bi, ok := debug.ReadBuildInfo() - is.True(ok) - - if st.frames != nil { - for _, f := range st.frames { - is.Truef(strings.Contains(f.file, bi.Path), "frame file %s should contain %s", f.file, bi.Path) - } - - is.Len(st.frames, 7, "expected 7 frames") - - if len(st.frames) == 7 { - is.Equal("f", (st.frames)[0].function) - is.Equal("e", (st.frames)[1].function) - is.Equal("d", (st.frames)[2].function) - is.Equal("c", (st.frames)[3].function) - is.Equal("b", (st.frames)[4].function) - is.Equal("a", (st.frames)[5].function) - is.Equal("TestStacktrace", (st.frames)[6].function) - } - } -}