errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package errors provides typed error handling for zipfs operations.

All error codes are defined in ADR-005 and match the MCP protocol specification.

Example usage:

// Creating errors
err := errors.SessionNotFound("my-session")
err := errors.ZipBombDetected("compression ratio exceeds 100:1")

// Wrapping errors
err := errors.SyncFailed(ioErr)

// Checking error codes
if errors.Is(err, errors.CodeSessionNotFound) {
    // handle session not found
}

// Extracting codes
code := errors.Code(err)
if code == errors.CodeZipInvalid {
    // handle invalid zip
}

// Stdlib compatibility
var zipfsErr *errors.Error
if errors.As(err, &zipfsErr) {
    fmt.Println(zipfsErr.Code, zipfsErr.Message)
}
Example (Basic)

Example_basic demonstrates basic error creation and checking.

package main

import (
	"fmt"

	"github.com/Fuabioo/zipfs/internal/errors"
)

func main() {
	// Create a simple error
	err := errors.SessionNotFound("my-session")
	fmt.Println(err)

	// Check the error code
	if errors.Is(err, errors.CodeSessionNotFound) {
		fmt.Println("Session not found")
	}

}
Output:
SESSION_NOT_FOUND: session "my-session" not found
Session not found
Example (Checking)

Example_checking demonstrates different ways to check errors.

package main

import (
	"fmt"

	"github.com/Fuabioo/zipfs/internal/errors"
)

func main() {
	err := errors.ZipInvalid("/tmp/test.txt")

	// Method 1: Use the Is helper
	if errors.Is(err, errors.CodeZipInvalid) {
		fmt.Println("Invalid zip file")
	}

	// Method 2: Extract and compare code
	if errors.Code(err) == errors.CodeZipInvalid {
		fmt.Println("Still invalid")
	}

	// Method 3: Use errors.As for full access
	var zipfsErr *errors.Error
	if e := err; e != nil {
		zipfsErr = e
		fmt.Printf("Code: %s, Message: %s\n", zipfsErr.Code, zipfsErr.Message)
	}

}
Output:
Invalid zip file
Still invalid
Code: ZIP_INVALID, Message: file "/tmp/test.txt" is not a valid zip archive
Example (Wrapping)

Example_wrapping demonstrates error wrapping.

package main

import (
	"fmt"
	"io/fs"

	"github.com/Fuabioo/zipfs/internal/errors"
)

func main() {
	// Simulate an I/O error
	ioErr := fs.ErrNotExist

	// Wrap it with a zipfs error
	err := errors.SyncFailed(ioErr)
	fmt.Println(err)

	// Extract the code
	code := errors.Code(err)
	fmt.Println("Error code:", code)

}
Output:
SYNC_FAILED: failed to sync workspace to zip: file does not exist
Error code: SYNC_FAILED

Index

Examples

Constants

View Source
const (
	CodeSessionNotFound  = "SESSION_NOT_FOUND"
	CodeAmbiguousSession = "AMBIGUOUS_SESSION"
	CodeNoSessions       = "NO_SESSIONS"
	CodeZipNotFound      = "ZIP_NOT_FOUND"
	CodeZipInvalid       = "ZIP_INVALID"
	CodeZipBombDetected  = "ZIP_BOMB_DETECTED"
	CodeConflictDetected = "CONFLICT_DETECTED"
	CodeSyncFailed       = "SYNC_FAILED"
	CodePathTraversal    = "PATH_TRAVERSAL"
	CodePathNotFound     = "PATH_NOT_FOUND"
	CodeLocked           = "LOCKED"
	CodeLimitExceeded    = "LIMIT_EXCEEDED"
	CodeNameCollision    = "NAME_COLLISION"
)

Error code constants matching ADR-005 error codes

Variables

This section is empty.

Functions

func Code

func Code(err error) string

Code extracts the error code from an error. Returns an empty string if the error is not a zipfs error.

func Is

func Is(err error, code string) bool

Is checks if an error has a specific error code.

Types

type Error

type Error struct {
	Code    string
	Message string
	// contains filtered or unexported fields
}

Error represents a zipfs error with a code and message. It implements the error interface and supports error wrapping.

func AmbiguousSession

func AmbiguousSession(count int) *Error

AmbiguousSession creates an AMBIGUOUS_SESSION error.

func ConflictDetected

func ConflictDetected(path string) *Error

ConflictDetected creates a CONFLICT_DETECTED error.

func LimitExceeded

func LimitExceeded(limit string) *Error

LimitExceeded creates a LIMIT_EXCEEDED error.

func Locked

func Locked(sessionID string) *Error

Locked creates a LOCKED error.

func NameCollision

func NameCollision(name string) *Error

NameCollision creates a NAME_COLLISION error.

func New

func New(code, message string) *Error

New creates a new zipfs error with the given code and message.

func NoSessions

func NoSessions() *Error

NoSessions creates a NO_SESSIONS error.

func PathNotFound

func PathNotFound(path string) *Error

PathNotFound creates a PATH_NOT_FOUND error.

func PathTraversal

func PathTraversal(path string) *Error

PathTraversal creates a PATH_TRAVERSAL error.

func SessionNotFound

func SessionNotFound(name string) *Error

SessionNotFound creates a SESSION_NOT_FOUND error.

func SyncFailed

func SyncFailed(err error) *Error

SyncFailed creates a SYNC_FAILED error wrapping the underlying cause.

func Wrap

func Wrap(code string, message string, err error) *Error

Wrap creates a new zipfs error that wraps an underlying error.

func ZipBombDetected

func ZipBombDetected(reason string) *Error

ZipBombDetected creates a ZIP_BOMB_DETECTED error.

func ZipInvalid

func ZipInvalid(path string) *Error

ZipInvalid creates a ZIP_INVALID error.

func ZipNotFound

func ZipNotFound(path string) *Error

ZipNotFound creates a ZIP_NOT_FOUND error.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message, implementing the error interface.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped error, supporting errors.Is and errors.As.

Jump to

Keyboard shortcuts

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