errors

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Registry = map[ErrorCode]ErrorInfo{
	ErrDynamicValue: {
		Code:    ErrDynamicValue,
		Title:   "schema values must use literal values for static analysis",
		HelpURL: "forge help E001",
	},
	ErrUnsupportedType: {
		Code:    ErrUnsupportedType,
		Title:   "unsupported field type",
		HelpURL: "forge help E002",
	},
	ErrInvalidFieldName: {
		Code:    ErrInvalidFieldName,
		Title:   "invalid field name",
		HelpURL: "forge help E003",
	},
	ErrMissingArgument: {
		Code:    ErrMissingArgument,
		Title:   "required argument missing",
		HelpURL: "forge help E004",
	},
	ErrInvalidModifierValue: {
		Code:    ErrInvalidModifierValue,
		Title:   "invalid modifier value",
		HelpURL: "forge help E005",
	},
	ErrDuplicateField: {
		Code:    ErrDuplicateField,
		Title:   "duplicate field name",
		HelpURL: "forge help E100",
	},
	ErrInvalidEnumDefault: {
		Code:    ErrInvalidEnumDefault,
		Title:   "enum default value not in allowed values",
		HelpURL: "forge help E101",
	},
	ErrMissingPrimaryKey: {
		Code:    ErrMissingPrimaryKey,
		Title:   "resource missing primary key",
		HelpURL: "forge help E102",
	},
}

Registry maps error codes to their metadata.

Functions

func Format

func Format(d Diagnostic) string

Format renders a single diagnostic in Rust-style format.

Output structure:

error[E001]: schema values must use literal values for static analysis
  --> resources/product/schema.go:12:34
   |
12 |     schema.String("Title").MaxLen(maxLen),
   |                                   ^^^^^^
   |
   = hint: Forge schemas must use literal values for static analysis.
           Found variable 'maxLen' — use a constant or literal instead.
   = help: Run `forge help E001` for more information

func FormatAll

func FormatAll(ds DiagnosticSet) string

FormatAll renders all diagnostics in a DiagnosticSet with separators.

Types

type Diagnostic

type Diagnostic struct {
	Code           ErrorCode // Error code (e.g., "E001")
	Message        string    // Human-readable error description
	Hint           string    // Suggestion for fixing the error
	File           string    // Source file path
	Line           int       // 1-based line number
	Column         int       // 1-based column number
	SourceLine     string    // The actual line of source code
	UnderlineStart int       // Column where underline begins (0-based within SourceLine)
	UnderlineLen   int       // Length of underline (number of chars to underline)
}

Diagnostic represents a rich error with source context and suggestions.

func (Diagnostic) Error

func (d Diagnostic) Error() string

Error implements the error interface.

type DiagnosticBuilder

type DiagnosticBuilder struct {
	// contains filtered or unexported fields
}

DiagnosticBuilder provides a fluent API for constructing diagnostics.

func NewDiagnostic

func NewDiagnostic(code ErrorCode, msg string) *DiagnosticBuilder

NewDiagnostic creates a new diagnostic builder.

func (*DiagnosticBuilder) Build

func (b *DiagnosticBuilder) Build() Diagnostic

Build returns the constructed Diagnostic.

func (*DiagnosticBuilder) Column

func (b *DiagnosticBuilder) Column(col int) *DiagnosticBuilder

Column sets the column number (1-based).

func (*DiagnosticBuilder) File

File sets the source file path.

func (*DiagnosticBuilder) Hint

Hint sets a suggestion for fixing the error.

func (*DiagnosticBuilder) Line

func (b *DiagnosticBuilder) Line(line int) *DiagnosticBuilder

Line sets the line number (1-based).

func (*DiagnosticBuilder) SourceLine

func (b *DiagnosticBuilder) SourceLine(line string) *DiagnosticBuilder

SourceLine sets the actual source code line.

func (*DiagnosticBuilder) Underline

func (b *DiagnosticBuilder) Underline(start, length int) *DiagnosticBuilder

Underline sets the underline position and length (0-based within source line).

type DiagnosticSet

type DiagnosticSet struct {
	Diagnostics []Diagnostic
}

DiagnosticSet is a collection of diagnostics. It implements the error interface so it can be returned as an error.

func (DiagnosticSet) Count

func (ds DiagnosticSet) Count() int

Count returns the number of diagnostics in the set.

func (DiagnosticSet) Error

func (ds DiagnosticSet) Error() string

Error implements the error interface. Returns a formatted string of all diagnostics.

func (DiagnosticSet) HasErrors

func (ds DiagnosticSet) HasErrors() bool

HasErrors returns true if the set contains any diagnostics.

type ErrorCode

type ErrorCode string

ErrorCode is a unique identifier for each error type.

const (
	ErrDynamicValue         ErrorCode = "E001" // Schema uses variable instead of literal
	ErrUnsupportedType      ErrorCode = "E002" // Field type not supported
	ErrInvalidFieldName     ErrorCode = "E003" // Field name invalid
	ErrMissingArgument      ErrorCode = "E004" // Required argument missing
	ErrInvalidModifierValue ErrorCode = "E005" // Modifier value invalid
)

Parser errors (E0xx range)

const (
	ErrDuplicateField     ErrorCode = "E100" // Field name appears twice
	ErrInvalidEnumDefault ErrorCode = "E101" // Enum default not in allowed values
	ErrMissingPrimaryKey  ErrorCode = "E102" // Resource missing primary key
)

Schema validation errors (E1xx range)

type ErrorInfo

type ErrorInfo struct {
	Code    ErrorCode // Error code
	Title   string    // Short description
	HelpURL string    // Help command or URL
}

ErrorInfo holds metadata about an error code.

func Lookup

func Lookup(code ErrorCode) ErrorInfo

Lookup returns the ErrorInfo for a given code. Returns an empty ErrorInfo if the code is not registered.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL