oaserrors

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package oaserrors provides structured error types for the oastools library.

Import path: github.com/erraggy/oastools/oaserrors

This package enables programmatic error handling via errors.Is and errors.As, allowing callers to distinguish between different categories of errors and implement appropriate recovery strategies.

Error Types

The package provides six core error types:

Sentinel Errors

Each error type has a corresponding sentinel error for use with errors.Is():

Usage Examples

Check error category with errors.Is():

result, err := parser.ParseWithOptions(parser.WithFilePath("api.yaml"))
if errors.Is(err, oaserrors.ErrParse) {
    // Handle parse error
}

Extract error details with errors.As():

var refErr *oaserrors.ReferenceError
if errors.As(err, &refErr) {
    fmt.Printf("Failed to resolve ref: %s\n", refErr.Ref)
    if refErr.IsCircular {
        // Handle circular reference specifically
    }
}

Check for specific conditions:

if errors.Is(err, oaserrors.ErrCircularReference) {
    // Circular reference detected - may be recoverable
}
if errors.Is(err, oaserrors.ErrPathTraversal) {
    // Security issue - log and reject
}

Error Chaining

All error types support error chaining via the Cause field and Unwrap() method. This allows finding root causes through the standard error chain:

var refErr *oaserrors.ReferenceError
if errors.As(err, &refErr) {
    if errors.Is(refErr.Cause, os.ErrNotExist) {
        // The reference file doesn't exist
    }
}

Package oaserrors provides structured error types for oastools.

These error types enable programmatic error handling via errors.Is() and errors.As(), allowing callers to distinguish between different categories of errors and implement appropriate recovery strategies.

Error Categories

  • ParseError: YAML/JSON parsing failures and structural issues
  • ReferenceError: $ref resolution failures, circular references, path traversal
  • ValidationError: OpenAPI specification violations
  • ResourceLimitError: Resource exhaustion (depth, size, count limits)
  • ConversionError: Version conversion failures between OAS versions
  • ConfigError: Invalid configuration or input options

Usage with errors.Is

result, err := parser.ParseWithOptions(parser.WithFilePath("api.yaml"))
if err != nil {
    var refErr *oaserrors.ReferenceError
    if errors.As(err, &refErr) {
        if refErr.IsCircular {
            // Handle circular reference specifically
        }
    }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrParse indicates a parsing failure occurred.
	ErrParse = errors.New("parse error")

	// ErrReference indicates a reference resolution failure.
	ErrReference = errors.New("reference error")

	// ErrCircularReference indicates a circular $ref was detected.
	ErrCircularReference = errors.New("circular reference")

	// ErrPathTraversal indicates a path traversal attempt was blocked.
	ErrPathTraversal = errors.New("path traversal detected")

	// ErrValidation indicates a specification validation failure.
	ErrValidation = errors.New("validation error")

	// ErrResourceLimit indicates a resource limit was exceeded.
	ErrResourceLimit = errors.New("resource limit exceeded")

	// ErrConversion indicates a version conversion failure.
	ErrConversion = errors.New("conversion error")

	// ErrConfig indicates an invalid configuration.
	ErrConfig = errors.New("configuration error")
)

Sentinel errors for use with errors.Is(). These allow quick checks without type assertions.

Functions

This section is empty.

Types

type ConfigError

type ConfigError struct {
	// Option is the name of the problematic configuration option
	Option string
	// Value is the invalid value that was provided (may be nil)
	Value any
	// Message describes the configuration error
	Message string
	// Cause is the underlying error, if any
	Cause error
}

ConfigError represents an invalid configuration or input. This includes invalid options, missing required inputs, and conflicting settings.

func (*ConfigError) Error

func (e *ConfigError) Error() string

Error returns a human-readable error message.

func (*ConfigError) Is

func (e *ConfigError) Is(target error) bool

Is reports whether target matches this error type.

func (*ConfigError) Unwrap

func (e *ConfigError) Unwrap() error

Unwrap returns the underlying cause for error chaining.

type ConversionError

type ConversionError struct {
	// SourceVersion is the source OAS version (e.g., "2.0", "3.0.3")
	SourceVersion string
	// TargetVersion is the target OAS version
	TargetVersion string
	// Path is the JSON path where conversion failed
	Path string
	// Message describes the conversion failure
	Message string
	// Cause is the underlying error, if any
	Cause error
}

ConversionError represents a failure during OAS version conversion.

func (*ConversionError) Error

func (e *ConversionError) Error() string

Error returns a human-readable error message.

func (*ConversionError) Is

func (e *ConversionError) Is(target error) bool

Is reports whether target matches this error type.

func (*ConversionError) Unwrap

func (e *ConversionError) Unwrap() error

Unwrap returns the underlying cause for error chaining.

type ParseError

type ParseError struct {
	// Path is the file path or source identifier
	Path string
	// Line is the line number where the error occurred (0 if unknown)
	Line int
	// Column is the column number where the error occurred (0 if unknown)
	Column int
	// Message describes the parsing failure
	Message string
	// Cause is the underlying error, if any
	Cause error
}

ParseError represents a failure to parse an OpenAPI document. This includes YAML/JSON deserialization errors and structural issues.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns a human-readable error message.

func (*ParseError) Is

func (e *ParseError) Is(target error) bool

Is reports whether target matches this error type.

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

Unwrap returns the underlying cause for error chaining.

type ReferenceError

type ReferenceError struct {
	// Ref is the reference string that failed to resolve
	Ref string
	// RefType indicates the reference type: "local", "file", or "http"
	RefType string
	// IsCircular is true if this error is due to a circular reference
	IsCircular bool
	// IsPathTraversal is true if this error is due to a path traversal attempt
	IsPathTraversal bool
	// Message provides additional context about the failure
	Message string
	// Cause is the underlying error, if any
	Cause error
}

ReferenceError represents a failure to resolve a $ref. This includes missing references, circular references, and path traversal attempts.

func (*ReferenceError) Error

func (e *ReferenceError) Error() string

Error returns a human-readable error message.

func (*ReferenceError) Is

func (e *ReferenceError) Is(target error) bool

Is reports whether target matches this error type. Matches ErrReference, and also ErrCircularReference or ErrPathTraversal when appropriate flags are set.

func (*ReferenceError) Unwrap

func (e *ReferenceError) Unwrap() error

Unwrap returns the underlying cause for error chaining.

type ResourceLimitError

type ResourceLimitError struct {
	// ResourceType identifies what limit was exceeded
	// Common values: "ref_depth", "cached_documents", "file_size", "nesting_depth"
	ResourceType string
	// Limit is the configured maximum value
	Limit int64
	// Actual is the value that exceeded the limit (may be 0 if unknown)
	Actual int64
	// Message provides additional context
	Message string
}

ResourceLimitError represents a resource exhaustion condition. This occurs when parsing or validation exceeds configured limits.

func (*ResourceLimitError) Error

func (e *ResourceLimitError) Error() string

Error returns a human-readable error message.

func (*ResourceLimitError) Is

func (e *ResourceLimitError) Is(target error) bool

Is reports whether target matches this error type.

func (*ResourceLimitError) Unwrap

func (e *ResourceLimitError) Unwrap() error

Unwrap returns nil as ResourceLimitError has no underlying cause.

type ValidationError

type ValidationError struct {
	// Path is the JSON path to the problematic field (e.g., "paths./pets.get.responses")
	Path string
	// Field is the specific field name with the issue
	Field string
	// Value is the problematic value (may be nil)
	Value any
	// Message describes the validation failure
	Message string
	// SpecRef is a URL to the relevant OAS specification section
	SpecRef string
	// Cause is the underlying error, if any
	Cause error
}

ValidationError represents an OpenAPI specification violation.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error returns a human-readable error message.

func (*ValidationError) Is

func (e *ValidationError) Is(target error) bool

Is reports whether target matches this error type.

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Unwrap returns the underlying cause for error chaining.

Jump to

Keyboard shortcuts

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