diff --git a/label-studio/users.go b/label-studio/users.go index 5f2b8dcf..7ae34b0c 100644 --- a/label-studio/users.go +++ b/label-studio/users.go @@ -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 diff --git a/platform/email/template.go b/platform/email/template.go index a15f5f08..bb94be0c 100644 --- a/platform/email/template.go +++ b/platform/email/template.go @@ -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") diff --git a/platform/oauth/oauth.go b/platform/oauth/oauth.go index 0619974c..bcb00101 100644 --- a/platform/oauth/oauth.go +++ b/platform/oauth/oauth.go @@ -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 { diff --git a/platform/tile.go b/platform/tile.go index aa15cdbe..44e8d6f7 100644 --- a/platform/tile.go +++ b/platform/tile.go @@ -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) diff --git a/rmo/email.go b/rmo/email.go index 2cd4f7f7..05f5fbf8 100644 --- a/rmo/email.go +++ b/rmo/email.go @@ -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") diff --git a/rmo/scss.go b/rmo/scss.go index d71cd849..81cce583 100644 --- a/rmo/scss.go +++ b/rmo/scss.go @@ -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) diff --git a/stadia/stadia.go b/stadia/stadia.go index 7c24982d..86ac1981 100644 --- a/stadia/stadia.go +++ b/stadia/stadia.go @@ -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") } diff --git a/static/static.go b/static/static.go index 4289daa3..f9fb0a07 100644 --- a/static/static.go +++ b/static/static.go @@ -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 { diff --git a/sync/sms.go b/sync/sms.go index 45ab4cc1..836c0d8e 100644 --- a/sync/sms.go +++ b/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") }