mirror of
https://github.com/SigNoz/signoz.git
synced 2026-03-24 05:10:27 +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.
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package errors
|
|
|
|
import (
|
|
"regexp"
|
|
)
|
|
|
|
var (
|
|
CodeInvalidInput Code = Code{"invalid_input"}
|
|
CodeInternal = Code{"internal"}
|
|
CodeUnsupported = Code{"unsupported"}
|
|
CodeNotFound = Code{"not_found"}
|
|
CodeMethodNotAllowed = Code{"method_not_allowed"}
|
|
CodeAlreadyExists = Code{"already_exists"}
|
|
CodeUnauthenticated = Code{"unauthenticated"}
|
|
CodeForbidden = Code{"forbidden"}
|
|
CodeCanceled = Code{"canceled"}
|
|
CodeTimeout = Code{"timeout"}
|
|
CodeUnknown = Code{"unknown"}
|
|
CodeFatal = Code{"fatal"}
|
|
CodeLicenseUnavailable = Code{"license_unavailable"}
|
|
)
|
|
|
|
var (
|
|
codeRegex = regexp.MustCompile(`^[a-z_]+$`)
|
|
)
|
|
|
|
type Code struct{ s string }
|
|
|
|
func NewCode(s string) (Code, error) {
|
|
if !codeRegex.MatchString(s) {
|
|
return Code{}, NewInvalidInputf(CodeInvalidInput, "invalid code: %v", s)
|
|
}
|
|
|
|
return Code{s: s}, nil
|
|
}
|
|
|
|
func MustNewCode(s string) Code {
|
|
code, err := NewCode(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return code
|
|
}
|
|
|
|
func (c Code) String() string {
|
|
return c.s
|
|
}
|