Skip to content

Errors

gogenfilter uses branded errors with [gogenfilter:<code>] prefixes for structured error handling. Every error carries a machine-readable ErrorCode and a descriptive message. Use errors.Is for sentinel matching and .ErrorCode() for programmatic access.

Returned when the project root cannot be found or resolved.

type ProjectRootError struct {
Code ErrorCode
StartPath string
Markers []string
Err error
}

When it occurs: FindProjectRoot fails to locate a go.mod or other marker file, or cannot resolve the start path.

Returned when a filter configuration option is invalid.

type FilterConfigError struct {
Code ErrorCode
Option FilterOption
Err error
}

When it occurs: WithFilterOptions receives an unrecognized FilterOption value.

Returned when a sqlc configuration file cannot be found, read, parsed, or collected.

type SQLCConfigError struct {
Code ErrorCode
ConfigPath string
Operation string
Message string
Err error
}

When it occurs: FindSQLCConfigs, GetSQLOutputDirs, or internal sqlc parsing fails.

Locates the project root by searching upward for marker files.

FindProjectRoot(startPath string, markers []string) (string, *ProjectRootError)

Searches from startPath upward (up to 10 levels) for any of the markers files. If no marker is found, returns ErrProjectRootNotFound. If the start path is invalid, returns ErrProjectRootInvalidPath.

root, err := gogenfilter.FindProjectRoot(
"./internal/pkg",
[]string{"go.mod", "go.sum"},
)
if err != nil {
// handle error
}

ErrorCode is a string type for machine-readable error classification:

type ErrorCode string

All error codes are defined as ErrorCode constants:

ConstantCode
CodeProjectRootNotFoundproject_root_not_found
CodeProjectRootInvalidPathproject_root_invalid_path
CodeInvalidFilterOptioninvalid_filter_option
CodeSQLCConfigReadsqlc_config_read
CodeSQLCConfigParsesqlc_config_parse
CodeSQLCConfigWalksqlc_config_walk
CodeSQLCConfigCollectsqlc_config_collect
CodeSQLCConfigFindsqlc_config_find

Sentinel errors have zero-value domain fields — matching is by ErrorCode only. Use errors.Is for comparison.

SentinelError TypeCode
ErrProjectRootNotFound*ProjectRootErrorproject_root_not_found
ErrProjectRootInvalidPath*ProjectRootErrorproject_root_invalid_path
ErrInvalidFilterOption*FilterConfigErrorinvalid_filter_option
ErrSQLCConfigRead*SQLCConfigErrorsqlc_config_read
ErrSQLCConfigParse*SQLCConfigErrorsqlc_config_parse
ErrSQLCConfigWalk*SQLCConfigErrorsqlc_config_walk
ErrSQLCConfigCollect*SQLCConfigErrorsqlc_config_collect
ErrSQLCConfigFind*SQLCConfigErrorsqlc_config_find

All error types implement the ErrorCoder interface:

type ErrorCoder interface {
ErrorCode() ErrorCode
}
_, err := gogenfilter.FindProjectRoot(
"/nonexistent",
[]string{"go.mod"},
)
if errors.Is(err, gogenfilter.ErrProjectRootNotFound) {
// handle project root not found
}

errors.AsType — Type Extraction (Go 1.26+)

Section titled “errors.AsType — Type Extraction (Go 1.26+)”

Extracts a branded error from a generic error variable:

var err error = maybeReturnProjectRootError()
projErr, ok := errors.AsType[*gogenfilter.ProjectRootError](err)
if ok {
fmt.Println(projErr.ErrorCode())
fmt.Println(projErr.StartPath)
}

All error types implement Unwrap() for error chain traversal with errors.Is and errors.As. The inner error is accessible via the Err field.

All errors produce branded messages with the [gogenfilter:<code>] prefix:

[gogenfilter:project_root_not_found] project root not found from "/some/path" (searched for: go.mod)
[gogenfilter:sqlc_config_read] sqlc config read "sqlc.yaml": reading sqlc config: permission denied

This format enables easy grep and log filtering.