From 0492be9d4a6a5ca7e8f81ef8bf2e1e300349555f Mon Sep 17 00:00:00 2001 From: vicanso Date: Thu, 20 Apr 2023 21:02:49 +0800 Subject: [PATCH] refactor: uniq sub error --- .github/workflows/test.yml | 1 + http_errors.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d13a696..4f828d6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,7 @@ jobs: strategy: matrix: go: + - '1.20' - '1.19' - '1.18' - '1.17' diff --git a/http_errors.go b/http_errors.go index cceaf7d..3385ccf 100644 --- a/http_errors.go +++ b/http_errors.go @@ -157,6 +157,18 @@ func (e *Error) AddExtra(key string, value interface{}) { e.Extra[key] = value } +// Exists return true if it already exists +func (e *Error) exists(he *Error) bool { + for _, item := range e.Errs { + if he.Title == item.Title && + he.Message == item.Message && + he.Category == item.Category { + return true + } + } + return false +} + // Add add error to error list func (e *Error) Add(errs ...error) { if len(errs) == 0 { @@ -181,6 +193,11 @@ func (e *Error) Add(errs ...error) { } continue } + // 判断是否已存在相同的error + // 如果已有相同error,则不添加 + if e.exists(he) { + continue + } e.Errs = append(e.Errs, he) }