response

package
v0.22.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package response provides HTTP response and error handling for requestCore.

Safe error response (for consuming repos):

  • Do not put string(rawResp) or full upstream response bodies in libError descriptions; use size/status/hash and log details separately (see libCallApi).
  • Do not use %+v on whole response structs in error messages that can become client-visible.
  • Use only codes from a fixed catalog and ensure they are seeded/localized (e.g. SYSTEM_FAULT, API_*); avoid dynamic values as public error codes.

Index

Constants

View Source
const (
	NO_DATA_FOUND     = "NO-DATA-FOUND"
	SYSTEM_FAULT      = "SYSTEM_FAULT"
	SYSTEM_FAULT_DESC = "خطای سیستمی"
)
View Source
const MaxDescriptionLength = 512

MaxDescriptionLength is the maximum length of error description sent to API clients.

Variables

This section is empty.

Functions

func GetDescFromCode added in v0.3.21

func GetDescFromCode(code string, data any, errDescList map[string]string) (string, string)

GetDescFromCode returns (code, description) for API response. When code is not in errDescList, it returns a safe fallback (SYSTEM_FAULT + localized text) and never exposes raw data.

func GetStack added in v0.9.40

func GetStack(skip int, exclude string) string

func JustPrintResp

func JustPrintResp(respBytes []byte, desc string, status int) (int, map[string]string, any, error)

func ParseRemoteRespJson

func ParseRemoteRespJson(respBytes []byte, desc string, status int) (int, map[string]string, any, error)

func ParseWsRemoteResp

func ParseWsRemoteResp(respBytes []byte, desc string, status int) (int, map[string]string, any, error)

func SanitizeForClient added in v0.22.6

func SanitizeForClient(candidate any, maxLen int) string

SanitizeForClient turns a candidate description into a safe string for API output. - If candidate is a string: trims, applies max length, and optionally redacts sensitive patterns. - If candidate is not a string (map, struct, etc.): returns SYSTEM_FAULT_DESC without dumping the value.

Types

type DbResponse

type DbResponse struct {
	Status      int    `json:"status"`
	Description string `json:"description"`
	Result      any    `json:"result"`
	ErrorCode   string `json:"error_code,omitempty"`
}

type ErrorData added in v0.9.5

type ErrorData struct {
	Status      int
	Description string
	Message     any
	// contains filtered or unexported fields
}

func (ErrorData) Child added in v0.9.5

func (e ErrorData) Child(err ErrorState) ErrorState

func (ErrorData) ChildErr added in v0.9.5

func (e ErrorData) ChildErr(err error) ErrorState

func (ErrorData) Error added in v0.9.5

func (e ErrorData) Error() string

func (ErrorData) Format added in v0.9.13

func (e ErrorData) Format(header string, stack *strings.Builder)

func (ErrorData) GetDescription added in v0.9.5

func (e ErrorData) GetDescription() string

func (ErrorData) GetInput added in v0.9.6

func (e ErrorData) GetInput() any

func (ErrorData) GetMessage added in v0.9.5

func (e ErrorData) GetMessage() any

func (ErrorData) GetStatus added in v0.9.5

func (e ErrorData) GetStatus() int

func (*ErrorData) Input added in v0.9.5

func (e *ErrorData) Input(in any) ErrorState

func (ErrorData) LogValue added in v0.15.0

func (e ErrorData) LogValue() slog.Value

LogValue implements slog.LogValuer and returns a grouped value with fields redacted. See https://pkg.go.dev/log/slog#LogValuer

func (ErrorData) SetDescription added in v0.9.5

func (e ErrorData) SetDescription(desc string) ErrorState

func (ErrorData) SetMessage added in v0.9.5

func (e ErrorData) SetMessage(msg any) ErrorState

func (ErrorData) SetStatus added in v0.9.5

func (e ErrorData) SetStatus(status int) ErrorState

func (ErrorData) WsResponse added in v0.9.5

func (e ErrorData) WsResponse() string

type ErrorResponse

type ErrorResponse struct {
	Code        string `json:"code"`
	Description string `json:"description"`
}

func FormatErrorResp

func FormatErrorResp(errs error, trans ut.Translator) []ErrorResponse

func GetErrorsArray

func GetErrorsArray(message string, data any) []ErrorResponse

func GetErrorsArrayWithMap

func GetErrorsArrayWithMap(incomingDesc string, data any, errDescList map[string]string) []ErrorResponse

type ErrorState

type ErrorState interface {
	Error() string
	Input(in any) ErrorState
	GetInput() any
	WsResponse() string
	SetStatus(int) ErrorState
	SetDescription(string) ErrorState
	SetMessage(any) ErrorState
	ChildErr(error) ErrorState
	Child(ErrorState) ErrorState
	GetStatus() int
	GetDescription() string
	GetMessage() any
	LogValue() slog.Value
}

func Error

func Error(status int, desc string, message any, err error) ErrorState

func Errors added in v0.9.12

func Errors(status int, desc string, message any, err ErrorState) ErrorState

func ToError

func ToError(desc string, message any, err error) ErrorState

func ToErrorState

func ToErrorState(err error) ErrorState

func Unwrap added in v0.15.0

func Unwrap(err error) (bool, ErrorState)

type FileResponse added in v0.10.4

type FileResponse struct {
	FileName string `json:"fileName"`
	Path     string `json:"path"`
}

type Receipt

type Receipt struct {
	Id    string `json:"id"`
	Title string `json:"title"`
	Rows  []any  `json:"rows"`
}

type RespData added in v0.10.4

type RespData struct {
	Code           int              `json:"code"`
	Status         int              `json:"status"`
	Message        string           `json:"message"`
	Type           RespType         `json:"type"`
	JSON           any              `json:"description"`
	PrintData      *Receipt         `json:"receipt"`
	Attachment     *FileResponse    `json:"attachment"`
	PreBuiltErrors *[]ErrorResponse `json:"-"` // when set, used instead of GetErrorsArray (e.g. for PublicDescription)
}

type RespType added in v0.10.4

type RespType int
const (
	Json RespType = iota
	JsonWithReceipt
	FileAttachment
)

type ResponseHandler

type ResponseHandler interface {
	OK(w webFramework.WebFramework, resp any)
	OKWithReceipt(w webFramework.WebFramework, resp any, receipt *Receipt)
	OKWithAttachment(w webFramework.WebFramework, file *FileResponse)
	Error(w webFramework.WebFramework, err error)
}

type WebHanlder added in v0.15.0

type WebHanlder struct {
	MessageDesc map[string]string
	ErrorDesc   map[string]string
}

func (WebHanlder) Error added in v0.15.0

func (m WebHanlder) Error(w webFramework.WebFramework, err error)

func (WebHanlder) GetErrorsArray added in v0.15.0

func (m WebHanlder) GetErrorsArray(message string, data any) []ErrorResponse

func (WebHanlder) OK added in v0.15.0

func (m WebHanlder) OK(w webFramework.WebFramework, resp any)

func (WebHanlder) OKWithAttachment added in v0.15.0

func (m WebHanlder) OKWithAttachment(w webFramework.WebFramework, attachment *FileResponse)

func (WebHanlder) OKWithReceipt added in v0.15.0

func (m WebHanlder) OKWithReceipt(w webFramework.WebFramework, resp any, receipt *Receipt)

func (WebHanlder) Respond added in v0.15.0

func (m WebHanlder) Respond(code, status int, message string, data any, abort bool, w webFramework.WebFramework)

func (WebHanlder) RespondWithAttachment added in v0.15.0

func (m WebHanlder) RespondWithAttachment(code, status int, message string, file *FileResponse, abort bool, w webFramework.WebFramework)

func (WebHanlder) RespondWithReceipt added in v0.15.0

func (m WebHanlder) RespondWithReceipt(code, status int, message string, data any, printData *Receipt, abort bool, w webFramework.WebFramework)

type WsRemoteResponse

type WsRemoteResponse struct {
	Status      int             `json:"status"`
	Description string          `json:"description"`
	Result      any             `json:"result,omitempty"`
	ErrorData   []ErrorResponse `json:"errors,omitempty"`
}

func (WsRemoteResponse) ToErrorState added in v0.1.7

func (e WsRemoteResponse) ToErrorState() ErrorState

type WsResponse

type WsResponse struct {
	Status       int      `json:"status"`
	Description  string   `json:"description"`
	Result       any      `json:"result,omitempty"`
	ErrorData    any      `json:"errors,omitempty"`
	PrintReceipt *Receipt `json:"printReceipt,omitempty"`
}

func (WsResponse) LogValue added in v0.22.5

func (r WsResponse) LogValue() slog.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL