refactor: устранить дублирование кода (Go-хендлеры, JS-автокомплит)
Найдено статическим анализом (dupl/jscpd) + проверено вручную:
Go:
- appstate/path.go: ResolveDBPath/ResolveConfigPath → общий resolvePath()
- handlers/respond.go: дженерик BindJSON[T] для bind+422-ошибки
- article/generator.go: buildNetSegment/buildPSUSegment → buildProfileSegment()
- cmd/qfs/main.go (+respond.go): 8 повторяющихся switch{case errors.Is(...)}
→ respondByErrCase(c, err, errCase{...}, ...)
Frontend (index.html):
- 5 идентичных обработчиков клавиатурной навигации автокомплита
→ handleAutocompleteKeyGeneric(event, onSelect)
- 4 функции сборки нового элемента корзины из автокомплита
→ buildNewCartItem()/commitCartChange()
Также: PricingMarkup — единая точка правды для аплифт-коэффициента
в JS (render + export), с cross-reference комментариями к Go-константам
в export.go (defaultSaleMarkup/stockCompetitorMarkupFactor).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
b087b7eb58
commit
4d7b0e13ef
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/handlers"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// errCase maps a sentinel error to the HTTP status/message respondByErrCase
|
||||
// should use when errors.Is matches it.
|
||||
type errCase struct {
|
||||
err error
|
||||
status int
|
||||
message string
|
||||
}
|
||||
|
||||
// respondByErrCase responds with the status/message of the first matching
|
||||
// case (checked in order), or a 500 "internal server error" fallback when
|
||||
// none match. Centralizes the switch-on-sentinel-error pattern repeated
|
||||
// across the config/project HTTP handlers in this file.
|
||||
func respondByErrCase(c *gin.Context, err error, cases ...errCase) {
|
||||
for _, cs := range cases {
|
||||
if errors.Is(err, cs.err) {
|
||||
handlers.RespondError(c, cs.status, cs.message, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
handlers.RespondError(c, http.StatusInternalServerError, "internal server error", err)
|
||||
}
|
||||
Reference in New Issue
Block a user