snapshot of https://github.com/nats-io/nats-server.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
NATS Server Tests / Lint (push) Failing after 3s
NATS Server Tests / Build (Latest Go) (push) Has been skipped
NATS Server Tests / Build (Minimum Go) (push) Has been skipped
NATS Server Tests / Build (Minimum Go, windows-2022) (push) Has been skipped
NATS Server Tests / Build (Minimum Go, windows-2025) (push) Has been skipped
NATS Server Tests / Test Stores (push) Has been skipped
NATS Server Tests / Test JetStream (push) Has been skipped
NATS Server Tests / Test Raft (push) Has been skipped
NATS Server Tests / Test JetStream Consumers (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 1 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 2 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 3 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 4 (push) Has been skipped
NATS Server Tests / Test JetStream Supercluster (push) Has been skipped
NATS Server Tests / Test No-Race 1 (push) Has been skipped
NATS Server Tests / Test No-Race 2 (push) Has been skipped
NATS Server Tests / Test MQTT (push) Has been skipped
NATS Server Tests / Test Message Tracing (push) Has been skipped
NATS Server Tests / Test JWT (push) Has been skipped
NATS Server Tests / Test Remaining Server Tests (push) Has been skipped
NATS Server Tests / Test Other Packages (push) Has been skipped
Vulnerability Scan / govulncheck (push) Failing after 12m2s
Some checks failed
NATS Server Tests / Lint (push) Failing after 3s
NATS Server Tests / Build (Latest Go) (push) Has been skipped
NATS Server Tests / Build (Minimum Go) (push) Has been skipped
NATS Server Tests / Build (Minimum Go, windows-2022) (push) Has been skipped
NATS Server Tests / Build (Minimum Go, windows-2025) (push) Has been skipped
NATS Server Tests / Test Stores (push) Has been skipped
NATS Server Tests / Test JetStream (push) Has been skipped
NATS Server Tests / Test Raft (push) Has been skipped
NATS Server Tests / Test JetStream Consumers (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 1 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 2 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 3 (push) Has been skipped
NATS Server Tests / Test JetStream Cluster 4 (push) Has been skipped
NATS Server Tests / Test JetStream Supercluster (push) Has been skipped
NATS Server Tests / Test No-Race 1 (push) Has been skipped
NATS Server Tests / Test No-Race 2 (push) Has been skipped
NATS Server Tests / Test MQTT (push) Has been skipped
NATS Server Tests / Test Message Tracing (push) Has been skipped
NATS Server Tests / Test JWT (push) Has been skipped
NATS Server Tests / Test Remaining Server Tests (push) Has been skipped
NATS Server Tests / Test Other Packages (push) Has been skipped
Vulnerability Scan / govulncheck (push) Failing after 12m2s
This commit is contained in:
102
server/jetstream_errors.go
Normal file
102
server/jetstream_errors.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type errOpts struct {
|
||||
err error
|
||||
}
|
||||
|
||||
// ErrorOption configures a NATS Error helper
|
||||
type ErrorOption func(*errOpts)
|
||||
|
||||
// Unless ensures that if err is a ApiErr that err will be returned rather than the one being created via the helper
|
||||
func Unless(err error) ErrorOption {
|
||||
return func(opts *errOpts) {
|
||||
opts.err = err
|
||||
}
|
||||
}
|
||||
|
||||
func parseOpts(opts []ErrorOption) *errOpts {
|
||||
eopts := &errOpts{}
|
||||
for _, opt := range opts {
|
||||
opt(eopts)
|
||||
}
|
||||
return eopts
|
||||
}
|
||||
|
||||
type ErrorIdentifier uint16
|
||||
|
||||
// IsNatsErr determines if an error matches ID, if multiple IDs are given if the error matches any of these the function will be true
|
||||
func IsNatsErr(err error, ids ...ErrorIdentifier) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ce, ok := err.(*ApiError)
|
||||
if !ok || ce == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
ae, ok := ApiErrors[id]
|
||||
if !ok || ae == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if ce.ErrCode == ae.ErrCode {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ApiError is included in all responses if there was an error.
|
||||
type ApiError struct {
|
||||
Code int `json:"code"`
|
||||
ErrCode uint16 `json:"err_code,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorsData is the source data for generated errors as found in errors.json
|
||||
type ErrorsData struct {
|
||||
Constant string `json:"constant"`
|
||||
Code int `json:"code"`
|
||||
ErrCode uint16 `json:"error_code"`
|
||||
Description string `json:"description"`
|
||||
Comment string `json:"comment"`
|
||||
Help string `json:"help"`
|
||||
URL string `json:"url"`
|
||||
Deprecates string `json:"deprecates"`
|
||||
}
|
||||
|
||||
func (e *ApiError) Error() string {
|
||||
return fmt.Sprintf("%s (%d)", e.Description, e.ErrCode)
|
||||
}
|
||||
|
||||
func (e *ApiError) toReplacerArgs(replacements []any) []string {
|
||||
var (
|
||||
ra []string
|
||||
key string
|
||||
)
|
||||
|
||||
for i, replacement := range replacements {
|
||||
if i%2 == 0 {
|
||||
key = replacement.(string)
|
||||
continue
|
||||
}
|
||||
|
||||
switch v := replacement.(type) {
|
||||
case string:
|
||||
ra = append(ra, key, v)
|
||||
case error:
|
||||
ra = append(ra, key, v.Error())
|
||||
default:
|
||||
ra = append(ra, key, fmt.Sprintf("%v", v))
|
||||
}
|
||||
}
|
||||
|
||||
return ra
|
||||
}
|
||||
Reference in New Issue
Block a user