Skip to content

Commit

Permalink
templates: linting: fix "error return value is not checked (errchkjson)
Browse files Browse the repository at this point in the history
The linter is correct; given that these functions do not allow for an error
to be returned, we panic. Alternatively, we could return the error string
as output, or add a `//nolint:errchkjson` comment.

    templates/templates.go:17:3: Error return value of `(*encoding/json.Encoder).Encode` is not checked: unsafe type `interface{}` found (errchkjson)
            enc.Encode(v)
            ^

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Nov 20, 2023
1 parent 4e2477f commit 30d36e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- dogsled
- dupword # Detects duplicate words.
- durationcheck
- errchkjson
- exportloopref # Detects pointers to enclosing loop variables.
- gocritic # Metalinter; detects bugs, performance, and styling issues.
- gocyclo
Expand Down
6 changes: 5 additions & 1 deletion templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ var basicFunctions = template.FuncMap{
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.Encode(v)
err := enc.Encode(v)
if err != nil {
panic(err)
}

// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
},
Expand Down

0 comments on commit 30d36e9

Please sign in to comment.