• Stars
    star
    193
  • Rank 201,081 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A source code linter that can be used to find code that will cause problems with Go's error wrapping scheme

go-errorlint

Build Status

go-errorlint is a source code linter for Go software that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.

Error wrapping allows for extra context in errors without sacrificing type information about the error's cause.

For details on Go error wrapping, see: https://golang.org/pkg/errors/

Usage

go-errorlint accepts a set of package names similar to golint:

go-errorlint ./...

If there are one or more results, the exit status is set to 1.

Examples

fmt.Errorf wrapping verb

This lint is disabled by default. Use the -errorf flag to toggle.

// bad
fmt.Errorf("oh noes: %v", err)
// ^ non-wrapping format verb for fmt.Errorf. Use `%w` to format errors

// good
fmt.Errorf("oh noes: %w", err)

You can pass -fix to have go-errorlint automatically fix these issues for you.

Caveats:

  • When using the -errorf lint, keep in mind that any errors wrapped by fmt.Errorf implicitly become part of your API as according to Hyrum's Law.
  • This linter will flag all instances of a non-wrapped in a single fmt.Errorf call which will break on Go 1.19 and earlier. Pass -errorf-multi=0 to retain backwards compatibility. This functionality will be passively maintained until at least Go 1.19 has been dropped from official support.

Comparisons of errors

This lint is enabled by default. Use the -comparison flag to toggle.

// bad
err == ErrFoo
// ^ comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error

// bad
switch err {
case ErrFoo:
}
// ^ switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors

// good
errors.Is(err, ErrFoo)

Errors returned from standard library functions that explicitly document that an unwrapped error is returned are allowed by the linter. Notable cases are io.EOF and sql.ErrNoRows.

Caveats:

  • Comparing the error returned from (io.Reader).Read to io.EOF without errors.Is is considered valid as this is explicitly documented behaviour. However, nothing stops 3rd party implementations from still wrapping io.EOF, causing this linter to not detect such cases.

Type assertions of errors

This lint is enabled by default. Use the -asserts flag to toggle.

// bad
myErr, ok := err.(*MyError)
// ^ type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors

// bad
switch err.(type) {
case *MyError:
}
// ^ type switch on error will fail on wrapped errors. Use errors.As to check for specific errors

// good
var me MyError
ok := errors.As(err, &me)

Contributing

Do you think you have found a bug? Then please report it via the Github issue tracker. Make sure to attach any problematic files that can be used to reproduce the issue. Such files are also used to create regression tests that ensure that your bug will never return.

When submitting pull requests, please prefix your commit messages with fix: or feat: for bug fixes and new features respectively. This is the Conventional Commits scheme that is used to automate some maintenance chores such as generating the changelog and inferring the next version number.

More Repositories

1

rust-id3

A rust library for reading and writing ID3 metadata
Rust
220
star
2

shady

CLI tool to render GLSL shaders
Go
108
star
3

ledcat

Control lots of LED's over lots of protocols
Rust
95
star
4

soundcloud-fs

A SoundCloud FUSE driver optimized for music libraries
Rust
64
star
5

trollibox

The hackerspace friendly music player web client
Go
40
star
6

edge-detection-rs

The Canny edge detection algorithm implemented in Rust
Rust
36
star
7

esp32-sensornode

Modular sensor platform
C++
14
star
8

cube-shaders

Animations for my 3D LED-Cube written in GLSL
GLSL
11
star
9

go-opentelemetry-lint

Golang linter to find and automatically fix issues with OpenTelemetry spans
Go
10
star
10

go-iterator

Go 1.18 generics iterator experiment
Go
5
star
11

go-ilda-renderer

A Go program that transcodes ILDA laser shows to animated GIFs
Go
5
star
12

glsl-workshop

GLSL
5
star
13

procsweeper

Minesweeper for your /proc
C
4
star
14

sha2017-badge-co2-sensor

Python
4
star
15

sha2017-lenny-face-generator

The Lenny Face Generator ported to the SHA2017 badge
Python
3
star
16

ledcubesim

A simulator for volumetric displays
Python
2
star
17

ledcat-nyancat

Nyan!
Python
2
star
18

librgbmatrix-sys

System crate for Hzeller's rpi-rgb-led-matrix C-library
Rust
2
star
19

video-mapper

An OpenGL application for projecting videos and images onto cubes
C++
2
star
20

audio-thing

WIP
Rust
2
star
21

gopolyjson

Go Code generator of JSON marshalers/unmarshalers for polymorphic datastructures
Go
2
star
22

buienradar-prometheus-exporter

Python
1
star
23

gyros

Arduino sketch to dump the BNO055 IMU rotation as a 4x4 matrix over serial
C
1
star
24

ektoplazm-flashfree

A free player for free music
JavaScript
1
star
25

webfs

A daemon that presents the contents of one or more directories through a web-interface
Go
1
star
26

hackerhotel2019-tvoc-grapher

An app for the HackerHotel2019 badge that displays readings from the SGP30 volatile organic compound sensor
Python
1
star