Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions model/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<nil request>"
}

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
Expand Down
8 changes: 6 additions & 2 deletions store/sqlstore/pg_dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down
12 changes: 11 additions & 1 deletion store/sqlstore/user_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading