From 326a359d7e094bca62e10b7e0d37414c58a9b4ff Mon Sep 17 00:00:00 2001 From: suifri Date: Tue, 28 Jul 2026 18:31:26 +0300 Subject: [PATCH] [WTEL-8726]refactor(user_store): add user friendly store errors build --- model/call.go | 54 ++++++++++++++++++++++++++++++++++++ store/sqlstore/pg_dialect.go | 8 ++++-- store/sqlstore/user_store.go | 12 +++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/model/call.go b/model/call.go index 9001c00f..01246f27 100644 --- a/model/call.go +++ b/model/call.go @@ -64,6 +64,60 @@ type EndpointRequest struct { Destination *string } +func NewCallInfoNotFoundError(id string, e *EndpointRequest) AppError { + if e == nil { + return NewNotFoundError(id, "User not found") + } + + var criteria []string + + if e.UserId != nil { + criteria = append(criteria, fmt.Sprintf("ID (%d)", *e.UserId)) + } + if e.Extension != nil { + criteria = append(criteria, fmt.Sprintf("extension ('%s')", *e.Extension)) + } + + var detail string + if len(criteria) > 0 { + detail = fmt.Sprintf("User with provided %s not found", strings.Join(criteria, " and ")) + } else { + detail = "User not found" + } + + return NewNotFoundError(id, detail) +} + +func (e *EndpointRequest) String() string { + if e == nil { + return "" + } + + var parts []string + + if e.AppId != nil { + parts = append(parts, fmt.Sprintf("app_id=%s", *e.AppId)) + } + if e.UserId != nil { + parts = append(parts, fmt.Sprintf("user_id=%d", *e.UserId)) + } + if e.Extension != nil { + parts = append(parts, fmt.Sprintf("extension=%s", *e.Extension)) + } + if e.SchemaId != nil { + parts = append(parts, fmt.Sprintf("schema_id=%d", *e.SchemaId)) + } + if e.Destination != nil { + parts = append(parts, fmt.Sprintf("destination=%s", *e.Destination)) + } + + if len(parts) == 0 { + return "empty request" + } + + return strings.Join(parts, ", ") +} + type CallRequest struct { Endpoints []string Strategy uint8 diff --git a/store/sqlstore/pg_dialect.go b/store/sqlstore/pg_dialect.go index 41de3975..3bda28eb 100644 --- a/store/sqlstore/pg_dialect.go +++ b/store/sqlstore/pg_dialect.go @@ -2,11 +2,13 @@ package sqlstore import ( "database/sql" + "errors" + "net/http" + "reflect" + "github.com/go-gorp/gorp" "github.com/lib/pq" "github.com/webitel/engine/model" - "net/http" - "reflect" ) const ForeignKeyViolationErrorCode = pq.ErrorCode("23503") @@ -33,6 +35,8 @@ func messageFromErr(err error) string { } } +func isNoRowsError(err error) bool { return errors.Is(err, sql.ErrNoRows) } + func extractCodeFromErr(err error) int { code := http.StatusInternalServerError diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index 93d72255..a57b5ddf 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -104,9 +104,19 @@ limit 1`, map[string]any{ "DomainId": domainId, "IsOnline": isOnline, }) + if err != nil { - return nil, model.NewCustomCodeError("store.sql_user.get_call_info.app_error", fmt.Sprintf("UserId=%v, Extension=%v %s", e.UserId, e.Extension, err.Error()), extractCodeFromErr(err)) + if isNoRowsError(err) { + return nil, model.NewCallInfoNotFoundError("store.sql_user.get_call_info.user_not_found", e) + } + + return nil, model.NewCustomCodeError( + "store.sql_user.get_call_info.app_error", + fmt.Sprintf("Executing get call info request: (%s); error: %v", e, err), + extractCodeFromErr(err), + ) } + return info, nil }