fix: resolve golangci-lint violations (errcheck, unused, gosimple, ineffassign)
errcheck: add proper error handling with slog.Error for tx.Rollback, key generation, and s3/dav operations. Add nolint comments for intentionally discarded DB scan errors and HTTP write errors. unused: remove dead code (svcRow type, processImage, nonDepositPaymentType, generateSecureCode, colorBold, nGreen, nRed) gosimple S1021: merge var declaration with assignment in manage.go ineffassign: remove dead assignments in settings.go, till.go, images.go Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -9,11 +9,11 @@ import (
|
||||
"crussell/internal/validators"
|
||||
"crussell/internal/zxcvbnjs"
|
||||
"crussell/mw"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -219,7 +219,11 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(r.Context())
|
||||
defer func() {
|
||||
if err := tx.Rollback(r.Context()); err != nil {
|
||||
slog.Error("failed to rollback transaction", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
now := clock.Now()
|
||||
|
||||
@@ -376,7 +380,11 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(r.Context())
|
||||
defer func() {
|
||||
if err := tx.Rollback(r.Context()); err != nil {
|
||||
slog.Error("failed to rollback transaction", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = tx.QueryRow(r.Context(), `
|
||||
UPDATE users
|
||||
@@ -421,7 +429,11 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(r.Context())
|
||||
defer func() {
|
||||
if err := tx.Rollback(r.Context()); err != nil {
|
||||
slog.Error("failed to rollback transaction", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = tx.Exec(r.Context(), `UPDATE users SET failed_attempts = 0, locked_until = NULL, last_login_at = NOW() WHERE id = $1`, userID)
|
||||
if err != nil {
|
||||
@@ -451,7 +463,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(auth.AuthResponse{
|
||||
_ = json.NewEncoder(w).Encode(auth.AuthResponse{
|
||||
Token: tokenString,
|
||||
JTI: jti,
|
||||
RefreshToken: refreshToken,
|
||||
@@ -500,7 +512,7 @@ func RefreshTokenHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(auth.AuthResponse{
|
||||
_ = json.NewEncoder(w).Encode(auth.AuthResponse{
|
||||
Token: newToken,
|
||||
JTI: jti,
|
||||
RefreshToken: refreshToken,
|
||||
@@ -518,7 +530,7 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Revoke the JTI — match the access token lifetime (1 hour)
|
||||
auth.RevokeJTI(r.Context(), jti, clock.Now().Add(1*time.Hour))
|
||||
|
||||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||
_ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||
}
|
||||
|
||||
type VerificationCodeRequest struct {
|
||||
@@ -557,7 +569,7 @@ func GenerateVerificationCodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
).Scan(&userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "If the email exists, a verification code will be sent"})
|
||||
_ = json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "If the email exists, a verification code will be sent"})
|
||||
return
|
||||
}
|
||||
log.Printf("Failed to look up user: %v", err)
|
||||
@@ -578,7 +590,7 @@ func GenerateVerificationCodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "Verification code generated"})
|
||||
_ = json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "Verification code generated"})
|
||||
}
|
||||
|
||||
func VerifyCodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -639,7 +651,11 @@ func VerifyCodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(r.Context())
|
||||
defer func() {
|
||||
if err := tx.Rollback(r.Context()); err != nil {
|
||||
slog.Error("failed to rollback transaction", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = tx.Exec(r.Context(),
|
||||
`UPDATE verification_codes SET used_at = NOW() WHERE code = $1`,
|
||||
@@ -669,15 +685,7 @@ func VerifyCodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "Email verified successfully"})
|
||||
_ = json.NewEncoder(w).Encode(VerificationResponse{Success: true, Message: "Email verified successfully"})
|
||||
}
|
||||
|
||||
// TODO(M8): Replace crypto/rand fallback with proper error handling - time-based fallback is predictable
|
||||
func generateSecureCode(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
log.Printf("Failed to generate random code: %v", err)
|
||||
return strings.ToLower(fmt.Sprintf("%x", clock.Now().UnixNano()))
|
||||
}
|
||||
return strings.ToLower(fmt.Sprintf("%x", bytes))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user