ErrUnsupported Flags Unimplemented Backends

Wrap the stdlib ErrUnsupported sentinel in your own error so callers detect missing capabilities with errors.Is, not ad-hoc per-backend message strings.

GoResilience

Code

gosnippet
29 lines
package main

import (
	"errors"
	"fmt"
)

// 按标准库建议不要裸返回哨兵:包一层自己的上下文,errors.Is 仍能穿透匹配
func buildIndex(driver string) error {
	if driver != "sqlite" {
		return fmt.Errorf("driver %q: %w", driver, errors.ErrUnsupported)
	}
	fmt.Println("index built with", driver)
	return nil
}

func main() {
	for _, driver := range []string{"sqlite", "csv"} {
		err := buildIndex(driver)
		switch {
		case err == nil:
			fmt.Println(driver, "ok")
		case errors.Is(err, errors.ErrUnsupported):
			fmt.Println(driver, "unsupported, keeping linear scan")
		default:
			fmt.Println(driver, "failed:", err)
		}
	}
}

What the code does

Count the functions buildIndex and main as the entire surface area of ErrUnsupported Flags Unimplemented Backends.

The cycling part of this snippet is for _, driver := range []string{"sqlite", "csv"}.

Using it

Signature: func buildIndex(driver string) error

Running the file prints labeled results such as index built with — each operation’s effect is visible in the output.

Launch the file; main takes over and steps through buildIndex.

Key statements

return fmt.Errorf("driver %q: %w", driver, errors.ErrUnsupported)
for _, driver := range []string{"sqlite", "csv"} {
err := buildIndex(driver)
switch {
case err == nil:
fmt.Println(driver, "ok")
case errors.Is(err, errors.ErrUnsupported):
fmt.Println(driver, "unsupported, keeping linear scan")
default:
fmt.Println(driver, "failed:", err)
}
}

Interactive Tools for This Topic

FAQ

What does ErrUnsupported Flags Unimplemented Backends print when run?
Invoke the file as-is — the `index built with` prefixes identify which step printed each line.