lint: fix errcheck for Close and Write calls across multiple files
Use lint.LogOnErr for deferred Body/File/Client Close calls. Use lint.Write for unchecked w.Write calls. Fix bug in sync/sms.go where fmt.Errorf result was discarded (replace with proper log.Error call).
This commit is contained in:
parent
c7a7e8431c
commit
0ec810591e
9 changed files with 24 additions and 15 deletions
|
|
@ -52,7 +52,7 @@ func (c *Client) GetUser(userID int) (*User, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to GET /api/users/%d: %w", userID, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer lint.LogOnErr(resp.Body.Close, "close response body")
|
||||
|
||||
// Parse response
|
||||
var user User
|
||||
|
|
|
|||
|
|
@ -267,7 +267,7 @@ func (bt *builtTemplate) executeTemplateHTML(w io.Writer, content any) error {
|
|||
return fmt.Errorf("Failed to parse template file: %w", err)
|
||||
}
|
||||
if templ == nil {
|
||||
w.Write([]byte("Failed to read from disk: "))
|
||||
lint.Write(w, []byte("Failed to read from disk: "))
|
||||
return errors.New("Template parsing failed")
|
||||
}
|
||||
//log.Debug().Str("name", templ.Name()).Msg("Parsed template")
|
||||
|
|
@ -284,7 +284,7 @@ func (bt *builtTemplate) executeTemplateTXT(w io.Writer, content any) error {
|
|||
return fmt.Errorf("Failed to parse template file: %w", err)
|
||||
}
|
||||
if templ == nil {
|
||||
w.Write([]byte("Failed to read from disk: "))
|
||||
lint.Write(w, []byte("Failed to read from disk: "))
|
||||
return errors.New("Template parsing failed")
|
||||
}
|
||||
//log.Debug().Str("name", templ.Name()).Msg("Parsed template")
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/Gleipnir-Technology/arcgis-go"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/arcgis/model"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
|
|
@ -48,7 +49,7 @@ func DoTokenRequest(ctx context.Context, form url.Values) (*OAuthTokenResponse,
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to do request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer lint.LogOnErr(resp.Body.Close, "close response body")
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
log.Info().Int("status", resp.StatusCode).Msg("Token request")
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/oauth"
|
||||
|
|
@ -242,7 +243,7 @@ func loadTileFromDisk(tile_path string) (*TileRaster, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("open: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer lint.LogOnErr(file.Close, "close tile file")
|
||||
img, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("readall from %s: %w", tile_path, err)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/html"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/email"
|
||||
"github.com/aarondl/opt/omit"
|
||||
|
|
@ -36,7 +37,7 @@ func getEmailByCode(w http.ResponseWriter, r *http.Request) {
|
|||
respondError(w, "Failed to render email_log: %w", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(html)
|
||||
lint.Write(w, html)
|
||||
}
|
||||
func getEmailReportUnsubscribe(w http.ResponseWriter, r *http.Request) {
|
||||
email := r.FormValue("email")
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
@ -20,7 +22,7 @@ func getScssDebug(w http.ResponseWriter, r *http.Request) {
|
|||
respondError(w, "failed to open file", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
defer lint.LogOnErr(file.Close, "close scss file")
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
respondError(w, "failed to stat file", err, http.StatusInternalServerError)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package stadia
|
|||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"resty.dev/v3"
|
||||
|
|
@ -39,5 +40,5 @@ func (s *StadiaMaps) AddResponseMiddleware(m resty.ResponseMiddleware) {
|
|||
s.client.AddResponseMiddleware(m)
|
||||
}
|
||||
func (s *StadiaMaps) Close() {
|
||||
s.client.Close()
|
||||
lint.LogOnErr(s.client.Close, "close stadia client")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
@ -136,7 +137,7 @@ func serveFileMaybeEmbedded(w http.ResponseWriter, r *http.Request, fileToServe
|
|||
http.ServeContent(crw, r, path, startedTime, fileToServe)
|
||||
|
||||
// Close the file
|
||||
fileToServe.Close()
|
||||
lint.LogOnErr(fileToServe.Close, "close static file")
|
||||
}
|
||||
|
||||
type embeddedFileWrapper struct {
|
||||
|
|
|
|||
14
sync/sms.go
14
sync/sms.go
|
|
@ -11,6 +11,8 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
@ -52,7 +54,7 @@ func handleSMSMessage(data *SMSWebhookData) error {
|
|||
for _, media := range data.Payload.Media {
|
||||
filePath, err := downloadMedia(media.URL)
|
||||
if err != nil {
|
||||
fmt.Errorf("Failed to download media from %s: %w", filePath, err)
|
||||
log.Error().Err(err).Str("filePath", filePath).Msg("Failed to download media")
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Downloaded media to: %s\n", filePath)
|
||||
|
|
@ -68,7 +70,7 @@ func downloadMedia(mediaURL string) (string, error) {
|
|||
if err != nil {
|
||||
return "", fmt.Errorf("failed to download media: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer lint.LogOnErr(resp.Body.Close, "close media response body")
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("failed to download media: status code %d", resp.StatusCode)
|
||||
|
|
@ -87,7 +89,7 @@ func downloadMedia(mediaURL string) (string, error) {
|
|||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temporary file: %w", err)
|
||||
}
|
||||
defer out.Close()
|
||||
defer lint.LogOnErr(out.Close, "close output file")
|
||||
|
||||
// Write the response body to the file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
|
|
@ -167,7 +169,7 @@ func postSMS(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
log.Info().Str("body", string(bodyBytes)).Msg("body")
|
||||
// Close the original body
|
||||
defer r.Body.Close()
|
||||
defer lint.LogOnErr(r.Body.Close, "close request body")
|
||||
|
||||
// Parse JSON into webhook struct
|
||||
var body SMSWebhookBody
|
||||
|
|
@ -181,7 +183,7 @@ func postSMS(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
lint.Write(w, []byte("ok"))
|
||||
}
|
||||
func getSMS(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
|
|
@ -198,5 +200,5 @@ func getSMS(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-type", "text/plain")
|
||||
// Signifies to Voip.ms that the callback worked.
|
||||
fmt.Fprintf(w, "ok")
|
||||
lint.Fprintf(w, "ok")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue