// Package tgerr implements helpers for error handling.
package tgerr import ( ) // Error represents RPC error returned as result to request. type Error struct { Code int // 420 Message string // FLOOD_WAIT_3 Type string // FLOOD_WAIT Argument int // 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 { return false } return .Type == } // IsCode reports whether error Code is equal to code. func ( *Error) ( int) bool { if == nil { return false } return .Code == } // IsOneOf returns true if error type is in tt. func ( *Error) ( ...string) bool { if == nil { return false } for , := range { if .IsType() { return true } } return false } // IsCodeOneOf returns true if error code is one of codes. func ( *Error) ( ...int) bool { if == nil { return false } for , := range { if .IsCode() { return true } } return false } // extractArgument extracts Type and Argument from Message. func ( *Error) () { if .Message == "" { return } // Defaulting Type to Message. .Type = .Message // Splitting by underscore. := strings.Split(.Message, "_") if len() < 2 { return } var []string : for , := range { for , := range { if ascii.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 { return fmt.Sprintf("rpc error code %d: %s (%d)", .Code, .Type, .Argument) } return fmt.Sprintf("rpc error code %d: %s", .Code, .Message) } // AsType returns *Error from err if rpc error type is t. func ( error, string) ( *Error, bool) { if errors.As(, &) && .Type == { return , true } return nil, false } // As extracts *Error from err if possible. func ( error) ( *Error, bool) { if errors.As(, &) { return , true } return nil, false } // Is returns true if err type is t. func ( error, ...string) bool { if , := As(); { return .IsOneOf(...) } return false } // IsCode returns true of error code is as provided. func ( error, ...int) bool { if , := As(); { return .IsCodeOneOf(...) } return false }