nopanic

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2022 License: MIT

README

Code check Go Report Card

nopanic

Reports instances where program is terminated in places other then main package.

Motivation

Preferd way of hadling errors is to propage them to the caller, because callers know how to handle them. Usually developers write code in such way. But it not a rare case to find panic or os.Exit buried deep down in code which can lead to unexpected program termination. This can epecially be dangerous when when code uses mix of both approaches.

This lint forces developers to propagate errors back to the caller and leaving heandling decision to the caller.

Lint error messages

  • "calling panic is not allowed" - is reported when panic or log.Panic is called in places other then main package.

  • "program exit is not allowed" - is reported when os.Exit or log.Fatal is called in places other then main package.

Examples

messages := []string {"Wisdom", "Knowledge", "Courage", "Humanity", "Justice", "Temperance", "Transcendence"}


func GetMessageForNumber(num int) string {
	if num < 0 {
		// Don't panic, propagate error instead
		panic("can't get messsage for specified number")
	}

	return messages[num%len(messages)]
}

func GetMessageForNumber(num int) (string,error) {
	if num < 0 {
		// Propagating error is much batter
		return "", errors.New("can't get messsage for specified number")
	}

	return messages[num%len(messages)], nil
}

Contribution

Feel free to contribute to nopanic, pull requests are wellcome. :)

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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