Documentation
¶
Index ¶
- Constants
- Variables
- func Annotate(cause error, annotations ...Annotation) error
- func Flatten(err error, mappers ...DetailMapper) error
- func Register(typeUrl string, provider func() Any)
- func Temporary(err error) (out bool)
- func WithAborted(cause error, detail ErrorInfo) (err error)
- func WithAlreadyExists(cause error, detail ResourceInfo) (err error)
- func WithBadRequest(cause error, detail BadRequest) error
- func WithCancelled(cause error) (err error)
- func WithDataLoss(cause error, detail DebugInfo) (err error)
- func WithDeadlineExceeded(cause error, detail DebugInfo) (err error)
- func WithFailedPrecondition(cause error, detail PreconditionFailure) error
- func WithInternal(cause error, detail DebugInfo) (err error)
- func WithNotFound(cause error, detail ResourceInfo) error
- func WithOutOfRange(cause error, detail BadRequest) (err error)
- func WithPermissionDenied(cause error, detail ErrorInfo) (err error)
- func WithResourceExhausted(cause error, detail QuotaFailure) (err error)
- func WithUnauthenticated(cause error, detail ErrorInfo) (err error)
- func WithUnavailable(cause error, detail DebugInfo) (err error)
- func WithUnimplemented(cause error) (err error)
- func WithUnknown(cause error, detail DebugInfo) (err error)
- type Annotation
- type Any
- type AnyDetail
- type BadRequest
- type DebugInfo
- type Decoder
- type DetailMapper
- type Duration
- type Encoder
- type ErrorInfo
- type FieldViolation
- type Help
- type Link
- type LocalizedMessage
- type Message
- type Modifier
- type PreconditionFailure
- type QuotaFailure
- type RequestInfo
- type ResourceInfo
- type RetryInfo
- type RoundTripper
- type StackTrace
- type StatusCode
- type StatusName
- type TypedViolation
- type Violation
Examples ¶
Constants ¶
View Source
const ( TypeUrlRetryInfo = typeUrlPrefix + "RetryInfo" TypeUrlDebugInfo = typeUrlPrefix + "DebugInfo" TypeUrlResourceInfo = typeUrlPrefix + "ResourceInfo" TypeUrlBadRequest = typeUrlPrefix + "BadRequest" TypeUrlPreconditionFailure = typeUrlPrefix + "PreconditionFailure" TypeUrlErrorInfo = typeUrlPrefix + "ErrorInfo" TypeUrlQuotaFailure = typeUrlPrefix + "QuotaFailure" TypeUrlRequestInfo = typeUrlPrefix + "RequestInfo" TypeUrlHelp = typeUrlPrefix + "Help" TypeUrlLocalizedMessage = typeUrlPrefix + "LocalizedMessage" )
Variables ¶
View Source
var ( ErrNoEncoder = errors.New("encoder: inner encoder is required") ErrNoDecoder = errors.New("decoder: inner decoder is required") )
Functions ¶
func Annotate ¶
func Annotate(cause error, annotations ...Annotation) error
Example ¶
err := Annotate(
context.DeadlineExceeded,
DeadlineExceeded,
StackTrace("heavy job"),
RequestInfo{RequestId: "<uuid>"},
LocalizedMessage{Local: "en-US", Message: "Background task timeout"},
LocalizedMessage{Local: "zh-CN", Message: "后台任务超时"},
)
fmt.Printf("%+v", err)
// Example Output:
// status: "504 DEADLINE_EXCEEDED"
// message: "context deadline exceeded"
// detail[0]:
// type: "type.googleapis.com/google.rpc.DebugInfo"
// detail: "heavy job"
// stack:
// goroutine 1 [running]:
// runtime/debug.Stack(0xc00005e980, 0x40, 0x40)
// /home/user/go/src/runtime/debug/stack.go:24 +0xa5
// github.com/gota33/errors.StackTrace.Annotate(0xfe36af, 0x9, 0x1056490, 0xc00005e980)
// /home/user/github/gota33/errors/detail.go:368 +0x2d
// github.com/gota33/errors.Annotate(0x1051780, 0x1257e60, 0xc00010fc00, 0x5, 0x5, 0xc00010fba8, 0x10)
// /home/user/github/gota33/errors/errors.go:79 +0x97
// github.com/gota33/errors.ExampleAnnotate()
// /home/user/github/gota33/errors/example_test.go:10 +0x251
// testing.runExample(0xfe589a, 0xf, 0xfff6c0, 0xfead08, 0x1a, 0x0, 0x0)
// /home/user/go/src/testing/run_example.go:63 +0x222
// testing.runExamples(0xc00010fed0, 0x120aee0, 0x3, 0x3, 0x0)
// /home/user/go/src/testing/example.go:44 +0x185
// testing.(*M).Run(0xc000114100, 0x0)
// /home/user/go/src/testing/testing.go:1419 +0x27d
// main.main()
// _testmain.go:71 +0x145
//
// detail[1]:
// type: "type.googleapis.com/google.rpc.RequestInfo"
// request_id: "<uuid>"
// serving_data: ""
// detail[2]:
// type: "type.googleapis.com/google.rpc.LocalizedMessage"
// local: "en-US"
// message: "Background task timeout"
// detail[3]:
// type: "type.googleapis.com/google.rpc.LocalizedMessage"
// local: "zh-CN"
// message: "后台任务超时"
func Flatten ¶
func Flatten(err error, mappers ...DetailMapper) error
func Temporary ¶ added in v0.1.6
Example ¶
fmt.Println(Temporary(OK))
fmt.Println(Temporary(Internal))
fmt.Println(Temporary(Unavailable))
fmt.Println(Temporary(&net.DNSError{IsTemporary: true}))
Output: false false true true
func WithAborted ¶ added in v0.1.4
func WithAlreadyExists ¶ added in v0.1.4
func WithAlreadyExists(cause error, detail ResourceInfo) (err error)
func WithBadRequest ¶ added in v0.1.4
func WithBadRequest(cause error, detail BadRequest) error
func WithCancelled ¶ added in v0.1.4
func WithDataLoss ¶ added in v0.1.4
func WithDeadlineExceeded ¶ added in v0.1.4
func WithFailedPrecondition ¶ added in v0.1.4
func WithFailedPrecondition(cause error, detail PreconditionFailure) error
func WithInternal ¶ added in v0.1.4
func WithNotFound ¶ added in v0.1.4
func WithNotFound(cause error, detail ResourceInfo) error
func WithOutOfRange ¶ added in v0.1.4
func WithOutOfRange(cause error, detail BadRequest) (err error)
func WithPermissionDenied ¶ added in v0.1.4
func WithResourceExhausted ¶ added in v0.1.4
func WithResourceExhausted(cause error, detail QuotaFailure) (err error)
func WithUnauthenticated ¶ added in v0.1.4
func WithUnavailable ¶ added in v0.1.4
func WithUnimplemented ¶ added in v0.1.4
func WithUnknown ¶ added in v0.1.4
Types ¶
type Annotation ¶
type Annotation interface {
Annotate(m Modifier)
}
type Any ¶
type Any interface {
Annotation
TypeUrl() string
}
func Details ¶
Example ¶
client := &http.Client{}
client.Get("")
err := WithNotFound(sql.ErrNoRows, ResourceInfo{
ResourceType: "pet.com/pet.v1.Cat",
ResourceName: "cat123@pet.com",
Owner: "user123",
Description: "cat123 not found",
})
for _, detail := range Details(err) {
fmt.Printf("%+v", detail)
}
Output: type: "type.googleapis.com/google.rpc.ResourceInfo" resource_type: "pet.com/pet.v1.Cat" resource_name: "cat123@pet.com" owner: "user123" description: "cat123 not found"
func HideDebugInfo ¶ added in v0.1.4
type BadRequest ¶
type BadRequest struct {
FieldViolations []FieldViolation `json:"fieldViolations,omitempty"`
}
func (BadRequest) Annotate ¶ added in v0.1.3
func (d BadRequest) Annotate(m Modifier)
func (BadRequest) MarshalJSON ¶
func (d BadRequest) MarshalJSON() ([]byte, error)
func (BadRequest) TypeUrl ¶
func (d BadRequest) TypeUrl() string
type DebugInfo ¶
type DebugInfo struct {
StackEntries []string `json:"stackEntries,omitempty"`
Detail string `json:"detail,omitempty"`
}
func (DebugInfo) MarshalJSON ¶
type Decoder ¶ added in v0.1.4
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder ¶ added in v0.1.4
func NewDecoder(dec decoder) *Decoder
type DetailMapper ¶ added in v0.1.5
type Duration ¶ added in v0.1.7
func (Duration) MarshalJSON ¶ added in v0.1.7
func (*Duration) UnmarshalJSON ¶ added in v0.1.7
type Encoder ¶ added in v0.1.4
type Encoder struct {
Mappers []DetailMapper
// contains filtered or unexported fields
}
Example ¶
err := Annotate(
context.DeadlineExceeded,
DeadlineExceeded,
StackTrace("heavy job"),
RequestInfo{RequestId: "<uuid>"},
LocalizedMessage{Local: "en-US", Message: "Background task timeout"},
LocalizedMessage{Local: "zh-CN", Message: "后台任务超时"},
)
jEnc := json.NewEncoder(os.Stdout)
jEnc.SetIndent(" ", " ")
enc := NewEncoder(jEnc)
enc.Mappers = []DetailMapper{HideDebugInfo}
_ = enc.Encode(err)
// Example Output:
// {
// "error": {
// "code": 504,
// "message": "context deadline exceeded",
// "status": "DEADLINE_EXCEEDED",
// "details": [
// {
// "@type": "type.googleapis.com/google.rpc.RequestInfo",
// "requestId": "\u003cuuid\u003e"
// },
// {
// "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
// "local": "en-US",
// "message": "Background task timeout"
// },
// {
// "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
// "local": "zh-CN",
// "message": "后台任务超时"
// }
// ]
// }
// }
func NewEncoder ¶ added in v0.1.4
func NewEncoder(enc encoder) *Encoder
type ErrorInfo ¶
type ErrorInfo struct {
Reason string `json:"reason,omitempty"`
Domain string `json:"domain,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
func (ErrorInfo) MarshalJSON ¶
type FieldViolation ¶
type LocalizedMessage ¶
type LocalizedMessage struct {
Local string `json:"local,omitempty"`
Message string `json:"message,omitempty"`
}
func (LocalizedMessage) Annotate ¶ added in v0.1.3
func (d LocalizedMessage) Annotate(m Modifier)
func (LocalizedMessage) MarshalJSON ¶
func (d LocalizedMessage) MarshalJSON() ([]byte, error)
func (LocalizedMessage) TypeUrl ¶
func (d LocalizedMessage) TypeUrl() string
type Modifier ¶ added in v0.1.3
type Modifier interface {
SetCode(code StatusCode)
WrapMessage(msg string)
AppendDetails(details ...Any)
}
type PreconditionFailure ¶
type PreconditionFailure struct {
Violations []TypedViolation `json:"violations,omitempty"`
}
func (PreconditionFailure) Annotate ¶ added in v0.1.3
func (d PreconditionFailure) Annotate(m Modifier)
func (PreconditionFailure) MarshalJSON ¶
func (d PreconditionFailure) MarshalJSON() ([]byte, error)
func (PreconditionFailure) TypeUrl ¶
func (d PreconditionFailure) TypeUrl() string
type QuotaFailure ¶
type QuotaFailure struct {
Violations []Violation `json:"violations,omitempty"`
}
func (QuotaFailure) Annotate ¶ added in v0.1.3
func (d QuotaFailure) Annotate(m Modifier)
func (QuotaFailure) MarshalJSON ¶
func (d QuotaFailure) MarshalJSON() ([]byte, error)
func (QuotaFailure) TypeUrl ¶
func (d QuotaFailure) TypeUrl() string
type RequestInfo ¶
type RequestInfo struct {
RequestId string `json:"requestId,omitempty"`
ServingData string `json:"servingData,omitempty"`
}
func (RequestInfo) Annotate ¶ added in v0.1.3
func (d RequestInfo) Annotate(m Modifier)
func (RequestInfo) MarshalJSON ¶
func (d RequestInfo) MarshalJSON() ([]byte, error)
func (RequestInfo) TypeUrl ¶
func (d RequestInfo) TypeUrl() string
type ResourceInfo ¶
type ResourceInfo struct {
ResourceType string `json:"resourceType,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
Owner string `json:"owner,omitempty"`
Description string `json:"description,omitempty"`
}
func (ResourceInfo) Annotate ¶ added in v0.1.3
func (d ResourceInfo) Annotate(m Modifier)
func (ResourceInfo) MarshalJSON ¶
func (d ResourceInfo) MarshalJSON() ([]byte, error)
func (ResourceInfo) TypeUrl ¶
func (d ResourceInfo) TypeUrl() string
type RetryInfo ¶ added in v0.1.7
type RetryInfo struct {
RetryDelay Duration `json:"retryDelay"`
}
func (RetryInfo) MarshalJSON ¶ added in v0.1.7
type RoundTripper ¶ added in v0.1.1
type RoundTripper struct {
Parent http.RoundTripper
}
type StackTrace ¶ added in v0.1.3
type StackTrace string
func (StackTrace) Annotate ¶ added in v0.1.3
func (s StackTrace) Annotate(m Modifier)
type StatusCode ¶
type StatusCode int
const ( OK StatusCode = iota Cancelled Unknown InvalidArgument DeadlineExceeded NotFound AlreadyExists PermissionDenied ResourceExhausted FailedPrecondition Aborted OutOfRange Unimplemented Internal DataLoss Unauthenticated )
func Code ¶
func Code(err error) (out StatusCode)
Example ¶
err := WithNotFound(sql.ErrNoRows, ResourceInfo{
ResourceType: "pet.com/pet.v1.Cat",
ResourceName: "cat123@pet.com",
Owner: "user123",
Description: "cat123 not found",
})
code := Code(err)
fmt.Println(code)
Output: 404 NOT_FOUND
func (StatusCode) Annotate ¶ added in v0.1.3
func (c StatusCode) Annotate(m Modifier)
func (StatusCode) Error ¶
func (c StatusCode) Error() string
func (StatusCode) Http ¶ added in v0.1.4
func (c StatusCode) Http() int
func (StatusCode) Name ¶ added in v0.1.4
func (c StatusCode) Name() StatusName
func (StatusCode) String ¶
func (c StatusCode) String() string
func (StatusCode) Temporary ¶ added in v0.1.6
func (c StatusCode) Temporary() bool
func (StatusCode) Valid ¶
func (c StatusCode) Valid() bool
type StatusName ¶ added in v0.1.4
type StatusName string
func (StatusName) StatusCode ¶ added in v0.1.4
func (s StatusName) StatusCode() StatusCode
func (StatusName) String ¶ added in v0.1.4
func (s StatusName) String() string
type TypedViolation ¶
Click to show internal directories.
Click to hide internal directories.