status

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorsFrom

func ErrorsFrom(err error) iter.Seq[error]

func Wrap

func Wrap(err error, s Status) error

func WrapCode

func WrapCode(err error, code int) error

Types

type CanUnmarshalResponse

type CanUnmarshalResponse interface {
	UnmarshalResponse(code int, data []byte) error
}

type Describer

type Describer interface {
	StatusCode() int
}

type Description

type Description struct {
	// Text error code text
	Text string `json:"code,omitzero"`
	// Message error message
	Message string `json:"message,omitzero"`
	// Detail error detail
	Detail string `json:"detail,omitzero"`
	// Location error location, see payload.Locations
	Location string `json:"location,omitzero"`
	// Position pointer to field positon. eg: Type.Field
	Position string `json:"position,omitzero"`
	// Source error caused source. eg: srv-abc@v1.1.0
	Source string `json:"source,omitzero"`
	// Errors error chain
	Errors []*Description `json:"errors,omitzero"`

	Status Status `json:"-"`
}

func AsDescription

func AsDescription(err error, src, loc string) *Description
Example
package main

import (
	"errors"
	"fmt"
	"net/http"

	"github.com/xoctopus/httpx/internal/status"
	"github.com/xoctopus/httpx/internal/validation"
)

func main() {
	// error
	e := status.WrapCode(errors.New("x"), http.StatusInternalServerError)
	fmt.Println(e)

	// wrapped error
	e = status.WrapCode(errors.New("e1"), http.StatusForbidden)
	de := status.AsDescription(e, "server-name", "query")
	fmt.Println(de)

	// user error
	e = errors.New("e2")
	de = status.AsDescription(e, "server-name", "query")
	fmt.Println(de)

	// validation error
	x := errors.New("e3")

	e = validation.WrapLocationError(x, "query")
	de = status.AsDescription(e, "server-name", "")
	OutputDescription(de)

	e = validation.WrapPositionError(x, "field")
	de2 := status.AsDescription(e, "server-name", "")
	OutputDescription(de2)

	de3 := status.AsDescription(de2, "", "")
	de3.Location = "body"
	OutputDescription(de3)

}

func OutputDescription(de *status.Description) {
	fmt.Println(de)
	if len(de.Location) > 0 {
		fmt.Println("  in:  ", de.Location)
	}
	if len(de.Position) > 0 {
		fmt.Println("  pos: ", de.Position)
	}
	fmt.Println("  code:", de.StatusCode())
	fmt.Println("  text:", de.StatusText())
}
Output:
INTERNAL_SERVER_ERROR{message="x",status=500}
FORBIDDEN{message="e1"}
INTERNAL_SERVER_ERROR{message="e2"}
BAD_REQUEST{message="e3"}
  in:   query
  code: 400
  text: BAD_REQUEST
BAD_REQUEST{message="e3"}
  pos:  field
  code: 400
  text: BAD_REQUEST
BAD_REQUEST{message="e3"}
  in:   body
  pos:  field
  code: 400
  text: BAD_REQUEST

func UnmarshalResponse

func UnmarshalResponse(code int, rspraw []byte) *Description
Example
package main

import (
	"fmt"
	"net/http"

	"github.com/xoctopus/httpx/internal/jsonv2/json"
	"github.com/xoctopus/httpx/internal/status"
	"github.com/xoctopus/httpx/pkg/httpx"
)

func main() {
	// unmarshal from response body
	rspraw, _ := json.Marshal(status.Response{
		Code: http.StatusUnauthorized,
		Errors: []*status.Description{
			{
				Text:     "BAD_REQUEST",
				Message:  "",
				Location: "body",
				Position: "username",
				Source:   "srv-auth-gateway@v0.0.1",
				Status:   httpx.STATUS__BAD_REQUEST,
			},
		},
		Extra: map[string]any{
			"title":  "用户名不合法",
			"detail": "请输入8-16位, 以应为字母开头且仅包含英文字符和数字",
		},
	})
	de := status.UnmarshalResponse(http.StatusUnauthorized, rspraw)
	OutputDescription(de)

	rspraw, _ = json.Marshal(status.Response{
		Code:    http.StatusBadRequest,
		Message: "用户名不合法\n请输入8-16位, 以应为字母开头且仅包含英文字符和数字",
	})
	de = status.UnmarshalResponse(http.StatusBadRequest, rspraw)
	OutputDescription(de)

	rspraw, _ = json.Marshal(status.Response{Code: http.StatusBadRequest})
	de = status.UnmarshalResponse(http.StatusBadRequest, rspraw)
	OutputDescription(de)

	de = status.UnmarshalResponse(http.StatusBadRequest, []byte("用户名不合法"))
	OutputDescription(de)

}

func OutputDescription(de *status.Description) {
	fmt.Println(de)
	if len(de.Location) > 0 {
		fmt.Println("  in:  ", de.Location)
	}
	if len(de.Position) > 0 {
		fmt.Println("  pos: ", de.Position)
	}
	fmt.Println("  code:", de.StatusCode())
	fmt.Println("  text:", de.StatusText())
}
Output:
BAD_REQUEST{message="用户名不合法\n请输入8-16位, 以应为字母开头且仅包含英文字符和数字"}
  in:   body
  pos:  username
  code: 401
  text: UNAUTHORIZED
BAD_REQUEST{message="用户名不合法\n请输入8-16位, 以应为字母开头且仅包含英文字符和数字"}
  code: 400
  text: BAD_REQUEST
BAD_REQUEST{message="BAD_REQUEST"}
  code: 400
  text: BAD_REQUEST
INTERNAL_SERVER_ERROR{message="用户名不合法"}
  code: 500
  text: INTERNAL_SERVER_ERROR

func (*Description) Error

func (d *Description) Error() string

func (*Description) StatusCode

func (d *Description) StatusCode() int

func (*Description) StatusText

func (d *Description) StatusText() string

func (*Description) UnmarshalResponse

func (d *Description) UnmarshalResponse(code int, data []byte) error

type Err

type Err struct {
	// contains filtered or unexported fields
}

func (*Err) Error

func (e *Err) Error() string

func (*Err) StatusCode

func (e *Err) StatusCode() int

func (*Err) StatusText

func (e *Err) StatusText() string

func (*Err) Unwrap

func (e *Err) Unwrap() error

type Error

type Error interface {
	Describer
	HasCodeText
	error
}

type HasCodeText

type HasCodeText interface {
	// StatusText returns code text. eg: http.StatusOK => http.StatusText(http.StatusOK)
	StatusText() string
}

type HasLocation

type HasLocation interface {
	Location() string
}

HasLocation presents error location. see payload.Locations

type HasPosition

type HasPosition interface {
	Position() string
}

HasPosition presents error position field or key.

type Modifier

type Modifier interface {
	SetStatusCode(int)
}

type Response

type Response struct {
	// 错误码
	Code int `json:"code,omitzero"`
	// 错误信息
	Message string `json:"message,omitzero"`
	// 错误详情
	Errors []*Description `json:"errors,omitzero"`

	Extra map[string]any `json:",inline"`
}

func AsResponse

func AsResponse(err error, source string) *Response

func (*Response) StatusCode

func (e *Response) StatusCode() int

StatusCode returns http.StatusCode For extending the semantics of HTTP status codes, the original HTTP status codes are multiplied to represent specific business logic errors. For example: http.StatusBadRequest = 400 400000001: Missing field A 400000002: Invalid field B

func (*Response) Unwrap

func (e *Response) Unwrap() []error

type Status

type Status interface {
	Describer
	HasCodeText

	IsValid() bool
}

func AsStatus

func AsStatus(code int) Status

Jump to

Keyboard shortcuts

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