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 ¶
- Constants
- func Code(err error) string
- func Is(err error, code string) bool
- type Error
- func AmbiguousSession(count int) *Error
- func ConflictDetected(path string) *Error
- func LimitExceeded(limit string) *Error
- func Locked(sessionID string) *Error
- func NameCollision(name string) *Error
- func New(code, message string) *Error
- func NoSessions() *Error
- func PathNotFound(path string) *Error
- func PathTraversal(path string) *Error
- func SessionNotFound(name string) *Error
- func SyncFailed(err error) *Error
- func Wrap(code string, message string, err error) *Error
- func ZipBombDetected(reason string) *Error
- func ZipInvalid(path string) *Error
- func ZipNotFound(path string) *Error
Examples ¶
Constants ¶
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 ¶
Types ¶
type Error ¶
Error represents a zipfs error with a code and message. It implements the error interface and supports error wrapping.
func AmbiguousSession ¶
AmbiguousSession creates an AMBIGUOUS_SESSION error.
func ConflictDetected ¶
ConflictDetected creates a CONFLICT_DETECTED error.
func LimitExceeded ¶
LimitExceeded creates a LIMIT_EXCEEDED error.
func NameCollision ¶
NameCollision creates a NAME_COLLISION error.
func PathNotFound ¶
PathNotFound creates a PATH_NOT_FOUND error.
func PathTraversal ¶
PathTraversal creates a PATH_TRAVERSAL error.
func SessionNotFound ¶
SessionNotFound creates a SESSION_NOT_FOUND error.
func SyncFailed ¶
SyncFailed creates a SYNC_FAILED error wrapping the underlying cause.
func ZipBombDetected ¶
ZipBombDetected creates a ZIP_BOMB_DETECTED error.