Skip to content

Commit

Permalink
Fix error handling conforms to golangci-lint standards
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip-Petruneac committed Oct 17, 2024
1 parent a91d929 commit 7af94cf
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ MYSQL_DATABASE=db
MYSQL_PASSWORD=password
MYSQL_PORT=3306
DB_HOSTNAME=db
API_URL=http://api:8080
API_URL=http://api:8081
161 changes: 139 additions & 22 deletions api/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,145 @@
linters:
enable:
- errcheck
- govet
- staticcheck
- gofmt
- goimports
# https://golangci-lint.run/usage/configuration/


run:
timeout: 5m
tests: true
issues-exit-code: 1
tests: true
timeout: 1m
# skip-dirs:
# - .github
# - .githook
# - vendor

# # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# # If invoked with -mod=readonly, the go command is disallowed from the implicit
# # automatic updating of go.mod described above. Instead, it fails when any changes
# # to go.mod are needed. This setting is most useful to check that go.mod does
# # not need updates, such as in a continuous integration and testing system.
# # If invoked with -mod=vendor, the go command assumes that the vendor
# # directory holds the correct copies of dependencies and ignores
# # the dependency descriptions in go.mod.
# modules-download-mode: readonly|release|vendor
# modules-download-mode: vendor

issues:
exclude-use-default: false
exclude:
- "Error return value of"
- "SA4006"
# output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
# format: colored-line-number

## all available settings of specific linters
#linters-settings:
# lll:
# # max line length, lines longer will be reported. Default is 120.
# # '\t' is counted as 1 character by default, and can be changed with the tab-width option
# line-length: 120
# # tab width in spaces. Default to 1.
# tab-width: 1
# nakedret:
# # make an issue if func has more lines of code than this setting and it has naked returns; default is 30
# max-func-lines: 30
# prealloc:
# # XXX: we don't recommend using this linter before doing performance profiling.
# # For most programs usage of prealloc will be a premature optimization.
#
# # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
# # True by default.
# simple: true
# range-loops: true # Report preallocation suggestions on range loops, true by default
# for-loops: false # Report preallocation suggestions on for loops, false by default
# nolintlint:
# # Enable to ensure that nolint directives are all used. Default is true.
# allow-unused: false
# # Disable to ensure that nolint directives don't have a leading space. Default is true.
# allow-leading-space: true
# # Exclude following linters from requiring an explanation. Default is [].
# allow-no-explanation: []
# # Enable to require an explanation of nonzero length after each nolint directive. Default is false.
# require-explanation: true
# # Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
# require-specific: true

linters-settings:
errcheck:
exclude-functions:
- fmt.Println
- bytes.Buffer.WriteString
gocognit:
min-complexity: 10
gocyclo:
min-complexity: 10
gofmt:
simplify: true
staticcheck:
checks:
- all
simplify: true
# maligned:
# suggest-new: true
misspell:
locale: US
# ignore-words:
# - someword
whitespace:
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
multi-func: false # Enforces newlines (or comments) after every multi-line function signature

linters:
disable:
- godox
enable:
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true, auto-fix: false]
- gosimple # Linter for Go source code that specializes in simplifying a code [fast: true, auto-fix: false]
- unused
- copyloopvar
- revive
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: true, auto-fix: false]
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: true, auto-fix: false]
- unused # Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false]
- bodyclose # checks whether HTTP response body is closed successfully [fast: true, auto-fix: false]
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- errorlint # go-errorlint is a source code linter for Go software that can be used to find code that will cause problemswith the error wrapping scheme introduced in Go 1.13. [fast: true, auto-fix: false]
- exhaustive # check exhaustiveness of enum switch statements [fast: true, auto-fix: false]
# - exhaustivestruct # Checks if all struct's fields are initialized [fast: true, auto-fix: false]
- exportloopref # checks for pointers to enclosing loop variables [fast: true, auto-fix: false]
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
# - funlen # Tool for detection of long functions [fast: true, auto-fix: false]
- gci # Gci control golang package import order and make it always deterministic. [fast: true, auto-fix: true]
- gochecknoglobals # check that no global variables exist
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gocritic # The most opinionated Go source code linter [fast: true, auto-fix: false]
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
- godot # Check if comments end in a period [fast: true, auto-fix: true]
# - godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false]
# - goerr113 # Golang linter to check the errors handling expressions [fast: true, auto-fix: false]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
# - gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
- goheader # Checks is file header matches to pattern [fast: true, auto-fix: false]
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
# - gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false]
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false]
- goprintffuncname # Checks that printf-like functions are named with `f` at the end [fast: true, auto-fix: false]
- gosec # Inspects source code for security problems [fast: true, auto-fix: false]
# - ifshort # Checks that your code uses short syntax for if-statements whenever possible [fast: true, auto-fix: false]
# - interfacer # Linter that suggests narrower interface types [fast: true, auto-fix: false]
# - lll # Reports long lines [fast: true, auto-fix: false]
- makezero # Finds slice declarations with non-zero initial length [fast: true, auto-fix: false]
# - maligned # Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
- nestif # Reports deeply nested if statements [fast: true, auto-fix: false]
# - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false]
- noctx # noctx finds sending http request without context.Context [fast: true, auto-fix: false]
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
# - paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test [fast: true, auto-fix: false]
- prealloc # Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
- predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false]
- rowserrcheck # checks whether Err of rows is checked successfully [fast: true, auto-fix: false]
# - scopelint # Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. [fast: true, auto-fix: false]
- stylecheck # Stylecheck is a replacement for golint [fast: true, auto-fix: false]
# - testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false]
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers [fast: true, auto-fix: false]
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes [fast: true, auto-fix: false]
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]
- unparam # Reports unused function parameters [fast: true, auto-fix: false]
- whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
- wrapcheck # Checks that errors returned from external packages are wrapped [fast: true, auto-fix: false]
- wsl # Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false]
2 changes: 1 addition & 1 deletion api/db_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type TestDBService struct {
func NewTestDBService() (*TestDBService, error) {
db, mock, err := sqlmock.New()
if err != nil {
return nil, fmt.Errorf("an error '%s' was not expected when opening a stub database connection", err)
return nil, fmt.Errorf("an error occurred when opening a stub database connection: %w", err)
}

return &TestDBService{
Expand Down
Loading

0 comments on commit 7af94cf

Please sign in to comment.