Skip to content

error-log-or-return/error-log-or-return

Repository files navigation

error-log-or-return

Go Version Go Report Card coverage Last Commit Project Status

🚀 Golang linter: или логируем ошибку, или возвращаем.

🎯 Overview

error-log-or-return is a powerful static analysis tool. Built on top of golang.org/x/tools/go/analysis, it seamlessly integrates with your development workflow.

🤔 Problem Example

func (s *Service) GetStatus() error {
  var err error
  defer func() {
    s.log.ErrorOrDebug(err, "")
  }()
  return err
}
  • Если функция возвращает ошибку (error) — она НЕ должна логировать ошибку внутри.
  • Если функция НЕ возвращает ошибку, но внутри есть обработка ошибки — она ДОЛЖНА логировать ошибку.
  • Если функция не возвращает ошибку и не обрабатывает ошибку — всё ОК.
  • Если функция возвращает ошибку и не логирует — всё ОК.

🧠 Зачем

Правило выработано постепенно:

🧪 Пример применения - пет-проект.

✨ Features

  • 📊 Clean Output: Sorted by file path and line numbers
  • 🔌 Editor Integration: Works with go vet, gopls, and your favorite IDE
  • 🌍 Cross-Platform: Full support for Windows, Linux, and macOS

🚀 Quick Start

# Install the tool globally
go install github.com/error-log-or-return/error-log-or-return@latest

error-log-or-return ./...

⚙️ Configuration

# error-log-or-return.yml
ignore:
  - "**/*_test.go"
  - "test/**"
  - "**/*_mock.go"
  - "**/mock/**"
  - "**/mocks/**"

The configuration file is automatically searched in the current directory (or .config/) with an optional dot prefix.

🔧 VS Code Integration

Ctrl+Shift+P (Cmd+Shift+P on Mac) → "Tasks: Run Task" → "Go: Check Error Log or Return"

  • Real-time highlighting of rule violations
  • Problems panel integration with clickable errors
  • File explorer markers showing files with issues

🔄 Optional: Install the Trigger Task on Save extension to automatically run the task silently on file save.

📋 Sample Output

path/to/file.go:25:1: возвращает error и есть defer с err
path/to/file.go:31:1: есть err, нет defer, нет возврата error

💡 Pro Tip: Output format is identical to go vet - your editor will highlight issues automatically!

🔧 Integration with other analyzers

import (
    "golang.org/x/tools/go/analysis"
    "github.com/error-log-or-return/error-log-or-return"
)

// Add to your multichecker
analyzers := []*analysis.Analyzer{
    errorLogOrReturn.Analyzer,
    // ... other analyzers
}

🔨 Development

# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Clone and build
git clone https://github.com/error-log-or-return/error-log-or-return.git
cd error-log-or-return
make build

🤝 Contributing

We ❤️ contributions! Please include:

  1. 🐛 Reproducer (code snippet or minimal repo)
  2. 📊 Expected vs actual output
  3. 🔖 Go version (go version)

📬 PRs are welcome too.

Star this repo if it helped you write cleaner Go code.

About

Golang linter: или логируем ошибку, или возвращаем

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages