Skip to content

Commit

Permalink
chore: Enable the errcheck linter. (#130)
Browse files Browse the repository at this point in the history
Enable the errcheck linter.

Resolve 70+ linter errors.
  • Loading branch information
shahzadlone authored Feb 1, 2022
1 parent 504f84f commit 2100f51
Show file tree
Hide file tree
Showing 27 changed files with 344 additions and 83 deletions.
8 changes: 4 additions & 4 deletions .golangci.sourceinc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ linters-settings:
# list of functions to exclude from checking, where each entry is a single function to exclude.
# see https://github.com/kisielk/errcheck#excluding-functions for details
exclude-functions:
- io/ioutil.ReadFile
- io.Copy(*bytes.Buffer)
- io.Copy(os.Stdout)
# - io/ioutil.ReadFile
# - io.Copy(*bytes.Buffer)
# - io.Copy(os.Stdout)

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
Expand Down Expand Up @@ -789,7 +789,7 @@ linters:
- typecheck
- unused
- varcheck
# - errcheck
- errcheck
# - wrapcheck
# - errorlint
# - gci
Expand Down
127 changes: 107 additions & 20 deletions api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"

"github.com/multiformats/go-multihash"
Expand All @@ -39,7 +40,10 @@ func NewServer(db client.DB) *Server {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"))
_, err := w.Write([]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"))
if err != nil {
log.Printf("DefraDB HTTP API Welcome message writing failed: %v", err)
}
})

r.Get("/ping", s.ping)
Expand All @@ -52,51 +56,88 @@ func NewServer(db client.DB) *Server {
}

func (s *Server) Listen(addr string) {
http.ListenAndServe(addr, s.router)
if err := http.ListenAndServe(addr, s.router); err != nil {
log.Fatalln("Error: HTTP Listening and Serving Failed: ", err)
}
}

func (s *Server) ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
_, err := w.Write([]byte("pong"))
if err != nil {
log.Printf("Writing pong with HTTP failed: %v", err)
}
}

func (s *Server) dump(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
s.db.PrintDump(ctx)
w.Write([]byte("ok"))

_, err := w.Write([]byte("ok"))
if err != nil {
log.Printf("Writing ok with HTTP failed: %v", err)
}
}

func (s *Server) execGQL(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
query := r.URL.Query().Get("query")
result := s.db.ExecQuery(ctx, query)
json.NewEncoder(w).Encode(result)

err := json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func (s *Server) loadSchema(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
var result client.QueryResult
sdl, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()

defer func() {
err = r.Body.Close()
if err != nil {
log.Print(err) // Should this be `log.Fatal(err)` ??
}
}()

if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

err = s.db.AddSchema(ctx, string(sdl))
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

result.Data = map[string]string{
"result": "success",
}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
Expand All @@ -115,7 +156,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = err.Error()
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -125,7 +172,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
block, err := s.db.GetBlock(ctx, c)
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -134,14 +187,26 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = err.Error()
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
buf, err := nd.MarshalJSON()
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -151,26 +216,42 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
delta, err := reg.DeltaDecode(nd)
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

data, err := delta.Marshal()
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

// var val interface{}
// err = cbor.Unmarshal(delta.Value().([]byte), &val)
// if err != nil {
// result.Errors = []interface{}{err.Error()}
// json.NewEncoder(w).Encode(result)
// w.WriteHeader(http.StatusBadRequest)
// return
// result.Errors = []interface{}{err.Error()}
// err = json.NewEncoder(w).Encode(result)
// if err != nil {
// http.Error(w, err.Error(), 500)
// return
// }
// w.WriteHeader(http.StatusBadRequest)
// return
// }
result.Data = map[string]interface{}{
"block": string(buf),
Expand All @@ -184,7 +265,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = nil
json.NewEncoder(w).Encode(result)

err := json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down
10 changes: 9 additions & 1 deletion cli/defradb/cmd/blocks_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ var getCmd = &cobra.Command{
log.Error("request failed: ", err)
return
}
defer res.Body.Close()

defer func() {
err = res.Body.Close()
if err != nil {
// Should this be `log.Fatal` ??
log.Error("response body closing failed: ", err)
}
}()

buf, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("request failed: ", err)
Expand Down
10 changes: 9 additions & 1 deletion cli/defradb/cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ var dumpCmd = &cobra.Command{
log.Error("request failed: ", err)
return
}
defer res.Body.Close()

defer func() {
err = res.Body.Close()
if err != nil {
// Should this be `log.Fatal` ??
log.Error("response body closing failed: ", err)
}
}()

buf, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("request failed: ", err)
Expand Down
28 changes: 18 additions & 10 deletions cli/defradb/cmd/ping.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2020 Source Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Copyright 2020 Source Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cmd

import (
Expand Down Expand Up @@ -38,7 +38,15 @@ var pingCmd = &cobra.Command{
log.Error("request failed: ", err)
return
}
defer res.Body.Close()

defer func() {
err = res.Body.Close()
if err != nil {
// Should this be `log.Fatal` ??
log.Error("response body closing failed: ", err)
}
}()

buf, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("request failed: ", err)
Expand Down
10 changes: 9 additions & 1 deletion cli/defradb/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.
log.Error("request failed: ", err)
return
}
defer res.Body.Close()

defer func() {
err = res.Body.Close()
if err != nil {
// Should this be `log.Fatal` ??
log.Error("response body closing failed: ", err)
}
}()

buf, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error("request failed: ", err)
Expand Down
15 changes: 11 additions & 4 deletions cli/defradb/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ func initConfig() {
bs, err := yaml.Marshal(defaultConfig)
cobra.CheckErr(err)

viper.ReadConfig(bytes.NewBuffer(bs))
err = viper.ReadConfig(bytes.NewBuffer(bs))
cobra.CheckErr(err)

err = viper.WriteConfigAs(home + "/.defradb/" + "config.yaml")
cobra.CheckErr(err)
}

viper.BindPFlag("database.address", rootCmd.Flags().Lookup("url"))
viper.BindPFlag("database.store", startCmd.Flags().Lookup("store"))
viper.Unmarshal(&config)
err := viper.BindPFlag("database.address", rootCmd.Flags().Lookup("url"))
cobra.CheckErr(err)

err = viper.BindPFlag("database.store", startCmd.Flags().Lookup("store"))
cobra.CheckErr(err)

err = viper.Unmarshal(&config)
cobra.CheckErr(err)
}
14 changes: 11 additions & 3 deletions cli/defradb/cmd/schema_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ var addCmd = &cobra.Command{
endpoint, err := url.Parse(endpointStr)
cobra.CheckErr(err)

r, err := http.Post(endpoint.String(), "text", bytes.NewBuffer(schema))
res, err := http.Post(endpoint.String(), "text", bytes.NewBuffer(schema))
cobra.CheckErr(err)
defer r.Body.Close()
result, err := ioutil.ReadAll(r.Body)

defer func() {
err = res.Body.Close()
if err != nil {
// Should this be `log.Fatal` ??
log.Error("response body closing failed: ", err)
}
}()

result, err := ioutil.ReadAll(res.Body)
cobra.CheckErr(err)
fmt.Println(string(result))
},
Expand Down
Loading

0 comments on commit 2100f51

Please sign in to comment.