The five sentinel errors returned during icon resolution, and the wrapping convention for matching them.
Overview
The package defines five sentinel errors. All are package-level variables that can be matched with errors.Is. Manager and provider functions wrap these sentinels with contextual information via fmt.Errorf.
For how errors interact with fallback resolution and IgnoreNotFound, see Aliases & Fallbacks.
Sentinel errors
ErrIconNotFound
var ErrIconNotFound = errors.New("icon not found")Returned by IconManager.Get when the icon name resolves to a valid provider but the provider does not have an icon with that name, and no fallback resolves.
import ( "errors" swarmicons "github.com/frostybee/go-swarm-icons")_, err := manager.Get("lucide:nonexistent")if errors.Is(err, swarmicons.ErrIconNotFound) { // handle missing icon}ErrProviderNotFound
var ErrProviderNotFound = errors.New("no provider registered for prefix")Returned by IconManager.Get when the resolved prefix does not match any registered provider.
_, err := manager.Get("unknown:home")if errors.Is(err, swarmicons.ErrProviderNotFound) { // no provider for prefix "unknown"}ErrInvalidIconName
var ErrInvalidIconName = errors.New("invalid icon name")Returned by IconManager.Get for malformed names: empty string, empty prefix (:home), empty name (lucide:), or bare name with no default prefix set.
_, err := manager.Get("")if errors.Is(err, swarmicons.ErrInvalidIconName) { // empty or malformed name}ErrInvalidSVG
var ErrInvalidSVG = errors.New("invalid SVG content")Returned by FromFile and FromString when the input is empty or contains no <svg> element. Also returned internally when an Iconify JSON entry has an empty body.
_, err := swarmicons.FromString("")if errors.Is(err, swarmicons.ErrInvalidSVG) { // empty or unparseable SVG}ErrProviderError
var ErrProviderError = errors.New("provider error")Returned by provider constructors for infrastructure-level failures: NewDirectoryProvider when the directory does not exist, NewJsonCollectionProvider when the file cannot be read, and FromFile on I/O errors.
_, err := swarmicons.NewDirectoryProvider("/nonexistent")if errors.Is(err, swarmicons.ErrProviderError) { // directory does not exist}Wrapping convention
Manager methods wrap sentinel errors with the original name for context:
fmt.Errorf("get %q: %w", name, ErrProviderNotFound)This preserves errors.Is matching while adding the icon name to the error message:
get "unknown:home": no provider registered for prefixAlways use errors.Is for matching, not string comparison:
import ( "errors" swarmicons "github.com/frostybee/go-swarm-icons")_, err := manager.Get("unknown:home")if errors.Is(err, swarmicons.ErrProviderNotFound) { // correct: matches wrapped errors}// WRONG: do not compare error stringsif err.Error() == "no provider registered for prefix" { // fails because the error is wrapped with context}See also
- Aliases & Fallbacks:
IgnoreNotFound()behavior and fallback resolution - IconManager API reference: the
Get()method that produces these errors
Summary table
| Error | Message | Returned by |
|---|---|---|
ErrIconNotFound |
"icon not found" |
IconManager.Get |
ErrProviderNotFound |
"no provider registered for prefix" |
IconManager.Get |
ErrInvalidIconName |
"invalid icon name" |
IconManager.Get |
ErrInvalidSVG |
"invalid SVG content" |
FromFile, FromString, Iconify JSON parsing |
ErrProviderError |
"provider error" |
NewDirectoryProvider, NewJsonCollectionProvider, FromFile |