errors

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides structured, actionable error messages for Vango.

The errors package implements a comprehensive error system that:

  • Shows exact source locations (file, line, column)
  • Explains what went wrong in plain language
  • Suggests how to fix issues with code examples
  • Links to documentation for deeper understanding

Error Categories

Errors are organized into categories:

  • compile: Build-time errors (type mismatches, missing imports)
  • runtime: Execution errors (signal read outside component, nil pointer)
  • hydration: Server/client mismatch errors
  • protocol: Wire protocol errors (invalid messages, connection issues)
  • validation: User input errors (form validation, route parameters)

Error Codes

Each error has a unique code (e.g., "E001") that maps to:

  • A short message describing the error
  • A detailed explanation
  • A documentation URL

Usage

err := errors.New("E001").
    WithLocation("app/routes/index.go", 15, 12).
    WithSuggestion("Allocate and read signals inside a vango.Setup() render closure")

fmt.Println(err.Format())
// Output:
// ERROR E001: Signal read outside component context
//
//   app/routes/index.go:15:12
//
//     13 │ func HomePage(p vango.NoProps) vango.Component {
//     14 │     return vango.Setup(p, func(s vango.SetupCtx[vango.NoProps]) vango.RenderFn {
//     15 │         count := setup.Signal(&s, 0)
//   → 16 │         return func() *vango.VNode { return Div(Textf("%d", count.Get())) }
//        │                                                     ^
//     17 │     })
//     18 │ }
//
//   Hint: Allocate signals in Setup and read them in the render closure
//
//   Learn more: https://vango.dev/docs/errors/E001

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableColors

func DisableColors()

DisableColors disables ANSI color output.

func EnableColors

func EnableColors()

EnableColors enables ANSI color output.

func GetAllCodes

func GetAllCodes() []string

GetAllCodes returns all registered error codes.

func PrintError

func PrintError(err error)

PrintError prints a formatted error to stderr.

func Register

func Register(code string, template ErrorTemplate)

Register adds a new error template to the registry.

Types

type Category

type Category string

Category represents the type of error.

const (
	CategoryCompile    Category = "compile"
	CategoryRuntime    Category = "runtime"
	CategoryHydration  Category = "hydration"
	CategoryProtocol   Category = "protocol"
	CategoryValidation Category = "validation"
	CategoryConfig     Category = "config"
	CategoryCLI        Category = "cli"
)

type ErrorTemplate

type ErrorTemplate struct {
	Category Category
	Message  string
	Detail   string
	DocURL   string
}

ErrorTemplate defines a registered error type.

func GetTemplate

func GetTemplate(code string) (ErrorTemplate, bool)

GetTemplate returns the template for an error code.

type Location

type Location struct {
	File   string
	Line   int
	Column int
}

Location represents a source code location.

func (*Location) String

func (l *Location) String() string

String returns the location as a formatted string.

type VangoError

type VangoError struct {
	// Code is a unique error identifier (e.g., "E001").
	Code string

	// Category is the error type (compile, runtime, etc.).
	Category Category

	// Message is a short description of the error.
	Message string

	// Detail is a longer explanation of the error.
	Detail string

	// Location is the source code location where the error occurred.
	Location *Location

	// Context contains surrounding source code lines.
	Context []string

	// Suggestion is a hint on how to fix the error.
	Suggestion string

	// Example is code showing the correct approach.
	Example string

	// DocURL is a link to documentation about this error.
	DocURL string

	// Wrapped is the underlying error, if any.
	Wrapped error
}

VangoError is a structured error with source location, suggestions, and documentation.

func FromError

func FromError(err error, code string) *VangoError

FromError wraps a standard error in a VangoError.

func New

func New(code string) *VangoError

New creates a VangoError from a registered error code.

func Newf

func Newf(category Category, format string, args ...any) *VangoError

Newf creates a new VangoError with a formatted message (no code).

func (*VangoError) Error

func (e *VangoError) Error() string

Error implements the error interface.

func (*VangoError) Format

func (e *VangoError) Format() string

Format returns a beautifully formatted error message for terminal display.

func (*VangoError) FormatCompact

func (e *VangoError) FormatCompact() string

FormatCompact returns a compact single-line error format.

func (*VangoError) FormatJSON

func (e *VangoError) FormatJSON() string

FormatJSON returns the error as a JSON object.

func (*VangoError) Unwrap

func (e *VangoError) Unwrap() error

Unwrap returns the wrapped error for errors.Is/As support.

func (*VangoError) WithContext

func (e *VangoError) WithContext(lines []string) *VangoError

WithContext adds custom context lines to the error.

func (*VangoError) WithDetail

func (e *VangoError) WithDetail(d string) *VangoError

WithDetail adds a detailed explanation to the error.

func (*VangoError) WithExample

func (e *VangoError) WithExample(ex string) *VangoError

WithExample adds a code example to the error.

func (*VangoError) WithLocation

func (e *VangoError) WithLocation(file string, line, column int) *VangoError

WithLocation adds source location to the error.

func (*VangoError) WithLocationFromError

func (e *VangoError) WithLocationFromError(err error) *VangoError

WithLocationFromError extracts location from a Go compiler error.

func (*VangoError) WithSuggestion

func (e *VangoError) WithSuggestion(s string) *VangoError

WithSuggestion adds a fix suggestion to the error.

func (*VangoError) Wrap

func (e *VangoError) Wrap(err error) *VangoError

Wrap wraps another error.

Jump to

Keyboard shortcuts

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