Back to list
JamesPrial

go-error-checking

by JamesPrial

WIP - collection of various Claude stuff i make/use/have_Claude_hallucinate

2🍴 0📅 Jan 23, 2026

SKILL.md


name: go-error-checking description: Type-safe error inspection using errors.Is and errors.As

Error Checking

Pattern

Use errors.Is to check sentinel errors and errors.As to extract error types.

errors.Is - Check Sentinel Errors

// CORRECT
if errors.Is(err, user.ErrNotFound) {
    return http.StatusNotFound
}

// WRONG - breaks with wrapped errors
if err == user.ErrNotFound { }

errors.As - Extract Error Types

// CORRECT - extracts specific error type
var pathErr *fs.PathError
if errors.As(err, &pathErr) {
    log.Printf("failed path: %s", pathErr.Path)
    log.Printf("operation: %s", pathErr.Op)
}

// WRONG - only checks direct type
if pathErr, ok := err.(*fs.PathError); ok { }

Complete Example

func HandleError(err error) {
    // Check sentinel errors
    if errors.Is(err, user.ErrNotFound) {
        fmt.Println("User not found")
        return
    }

    // Extract custom error types
    var validationErr *ValidationError
    if errors.As(err, &validationErr) {
        fmt.Printf("Validation failed: %v\n", validationErr.Fields)
        return
    }

    // Extract standard library errors
    var pathErr *fs.PathError
    if errors.As(err, &pathErr) {
        fmt.Printf("File operation failed: %s\n", pathErr.Path)
        return
    }

    // Generic error
    fmt.Printf("Unknown error: %v\n", err)
}

Key Differences

  • errors.Is: Checks if error matches a sentinel value (works through wrapping)
  • errors.As: Extracts error of specific type (works through wrapping)
  • Never use == or type assertions for wrapped errors

Score

Total Score

65/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

1ヶ月以内に更新

+10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

Reviews

💬

Reviews coming soon