// Package tgerr implements helpers for error handling.
package tgerrimport ()// Error represents RPC error returned as result to request.typeErrorstruct {Codeint// 420Messagestring// FLOOD_WAIT_3Typestring// FLOOD_WAITArgumentint// 3}// New creates new *Error from code and message, extracting argument// and type.func ( int, string) *Error { := &Error{Code: ,Message: , } .extractArgument()return}// IsType reports whether error has type t.func ( *Error) ( string) bool {if == nil {returnfalse }return .Type == }// IsCode reports whether error Code is equal to code.func ( *Error) ( int) bool {if == nil {returnfalse }return .Code == }// IsOneOf returns true if error type is in tt.func ( *Error) ( ...string) bool {if == nil {returnfalse }for , := range {if .IsType() {returntrue } }returnfalse}// IsCodeOneOf returns true if error code is one of codes.func ( *Error) ( ...int) bool {if == nil {returnfalse }for , := range {if .IsCode() {returntrue } }returnfalse}// extractArgument extracts Type and Argument from Message.func ( *Error) () {if .Message == "" {return }// Defaulting Type to Message. .Type = .Message// Splitting by underscore. := strings.Split(.Message, "_")iflen() < 2 {return }var []string:for , := range {for , := range {ifascii.IsDigit() {continue }// Found non-digit part, skipping. = append(, )continue }// Found digit-only part, using as argument. , := strconv.Atoi()if != nil {// Should be unreachable.return } .Argument = } .Type = strings.Join(, "_")}func ( *Error) () string {if .Type != .Message {returnfmt.Sprintf("rpc error code %d: %s (%d)", .Code, .Type, .Argument) }returnfmt.Sprintf("rpc error code %d: %s", .Code, .Message)}// AsType returns *Error from err if rpc error type is t.func ( error, string) ( *Error, bool) {iferrors.As(, &) && .Type == {return , true }returnnil, false}// As extracts *Error from err if possible.func ( error) ( *Error, bool) {iferrors.As(, &) {return , true }returnnil, false}// Is returns true if err type is t.func ( error, ...string) bool {if , := As(); {return .IsOneOf(...) }returnfalse}// IsCode returns true of error code is as provided.func ( error, ...int) bool {if , := As(); {return .IsCodeOneOf(...) }returnfalse}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.