mirror of
https://github.com/SigNoz/signoz.git
synced 2026-03-25 13:40:34 +00:00
* feat(middleware): add panic recovery middleware with TypeFatal error type Add a global HTTP recovery middleware that catches panics, logs them with OTel exception semantic conventions via errors.Attr, and returns a safe user-facing error response. Introduce TypeFatal/CodeFatal for unrecoverable failures and WithStacktrace to attach pre-formatted stack traces to errors. Remove redundant per-handler panic recovery blocks in querier APIs. * style(errors): keep WithStacktrace call on same line in test * fix(middleware): replace fmt.Errorf with errors.New in recovery test * feat(middleware): add request context to panic recovery logs Capture request body before handler runs and include method, path, and body in panic recovery logs using OTel semconv attributes. Improve error message to direct users to GitHub issues or support.
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package errors
|
|
|
|
import (
|
|
"errors" //nolint:depguard
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
typ := typ{"test-error"}
|
|
err := New(typ, MustNewCode("code"), "test error info")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestNewf(t *testing.T) {
|
|
typ := typ{"test-error"}
|
|
err := Newf(typ, MustNewCode("test_code"), "test error info with %s", "string")
|
|
assert.NotNil(t, err)
|
|
assert.Equal(t, "test error info with string", err.Error())
|
|
}
|
|
|
|
func TestWrapf(t *testing.T) {
|
|
typ := typ{"test-error"}
|
|
err := Wrapf(errors.New("original error"), typ, MustNewCode("test_code"), "info for err %d", 2)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
typ := typ{"test-error"}
|
|
err1 := New(typ, MustNewCode("test_code"), "info for err1")
|
|
assert.Equal(t, "info for err1", err1.Error())
|
|
|
|
err2 := Wrapf(err1, typ, MustNewCode("test_code"), "info for err2")
|
|
assert.Equal(t, "info for err1", err2.Error())
|
|
}
|
|
|
|
func TestUnwrapb(t *testing.T) {
|
|
typ := typ{"test-error"}
|
|
oerr := errors.New("original error")
|
|
berr := Wrapf(oerr, typ, MustNewCode("test_code"), "this is a base err").WithUrl("https://docs").WithAdditional("additional err")
|
|
|
|
atyp, acode, amessage, aerr, au, aa := Unwrapb(berr)
|
|
assert.Equal(t, typ, atyp)
|
|
assert.Equal(t, "test_code", acode.String())
|
|
assert.Equal(t, "this is a base err", amessage)
|
|
assert.Equal(t, oerr, aerr)
|
|
assert.Equal(t, "https://docs", au)
|
|
assert.Equal(t, []string{"additional err"}, aa)
|
|
|
|
atyp, _, _, _, _, _ = Unwrapb(oerr)
|
|
assert.Equal(t, TypeInternal, atyp)
|
|
}
|
|
|
|
func TestAttr(t *testing.T) {
|
|
err := New(TypeInternal, MustNewCode("test_code"), "test error")
|
|
attr := Attr(err)
|
|
assert.Equal(t, "exception", attr.Key)
|
|
assert.Equal(t, err, attr.Value.Any())
|
|
}
|
|
|
|
func TestWithStacktrace(t *testing.T) {
|
|
err := New(TypeInternal, MustNewCode("test_code"), "panic").WithStacktrace("custom stack trace")
|
|
|
|
assert.Equal(t, "custom stack trace", err.Stacktrace())
|
|
assert.Equal(t, "panic", err.Error())
|
|
|
|
typ, code, message, _, _, _ := Unwrapb(err)
|
|
assert.Equal(t, TypeInternal, typ)
|
|
assert.Equal(t, "test_code", code.String())
|
|
assert.Equal(t, "panic", message)
|
|
}
|