checker-dnsviz/checker/provider.go
Pierre-Olivier Mercier 77dfb82313 fix: return ErrNoCollector instead of panicking on nil CollectFn
Hosts that register the provider only for its definition (externalizable
checker) construct it with a nil CollectFn. If the local ObservationContext
ends up calling Collect, the previous code dereferenced a nil function
value and crashed the goroutine. Surface a typed error so rules degrade
to a clean StatusError state.
2026-04-30 09:23:26 +07:00

40 lines
1.3 KiB
Go

// SPDX-License-Identifier: MIT
package checker
import (
"context"
"errors"
sdk "git.happydns.org/checker-sdk-go/checker"
)
// ErrNoCollector is returned by Collect when the provider was constructed
// without a CollectFn (e.g. when registered host-side as an externalizable-only
// provider). Callers should route the observation to an external checker.
var ErrNoCollector = errors.New("dnsviz: provider has no local collector; use an external checker")
// CollectFn is the function signature for the DNSViz data collection step.
// The checker package is decoupled from the subprocess invocation so it can
// be imported without GPL obligations. Implementations live in the binary or
// plugin layer (see internal/collect).
type CollectFn func(ctx context.Context, opts sdk.CheckerOptions) (any, error)
// Provider returns a new DNSViz observation provider backed by the given
// collect function.
func Provider(collect CollectFn) sdk.ObservationProvider {
return &dnsvizProvider{collect: collect}
}
type dnsvizProvider struct{ collect CollectFn }
func (p *dnsvizProvider) Key() sdk.ObservationKey {
return ObservationKeyDNSViz
}
func (p *dnsvizProvider) Collect(ctx context.Context, opts sdk.CheckerOptions) (any, error) {
if p.collect == nil {
return nil, ErrNoCollector
}
return p.collect(ctx, opts)
}