From aa3973f95643bc2dfd6e1fc4eb2a4b8f02dcd9fe Mon Sep 17 00:00:00 2001 From: legolord208 Date: Mon, 8 May 2017 15:48:19 +0200 Subject: [PATCH] Made error constants (Fixed #315) (#377) --- restapi.go | 23 +++++++++++++++-------- state.go | 28 ++++++++++++++++------------ wsapi.go | 20 ++++++++++++++++---- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/restapi.go b/restapi.go index 7c9fd81..cb482e6 100644 --- a/restapi.go +++ b/restapi.go @@ -29,8 +29,15 @@ import ( "time" ) -// ErrJSONUnmarshal is returned for JSON Unmarshall errors. -var ErrJSONUnmarshal = errors.New("json unmarshal") +// All error constants +var ( + ErrJSONUnmarshal = errors.New("json unmarshal") + ErrStatusOffline = errors.New("You can't set your Status to offline") + ErrVerificationLevelBounds = errors.New("VerificationLevel out of bounds, should be between 0 and 3") + ErrPruneDaysBounds = errors.New("the number of days should be more than or equal to 1") + ErrGuildNoIcon = errors.New("guild does not have an icon set") + ErrGuildNoSplash = errors.New("guild does not have a splash set") +) // Request is the same as RequestWithBucketID but the bucket id is the same as the urlStr func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) { @@ -334,7 +341,7 @@ func (s *Session) UserSettings() (st *Settings, err error) { // status : The new status (Actual valid status are 'online','idle','dnd','invisible') func (s *Session) UserUpdateStatus(status Status) (st *Settings, err error) { if status == StatusOffline { - err = errors.New("You can't set your Status to offline") + err = ErrStatusOffline return } @@ -595,7 +602,7 @@ func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error if g.VerificationLevel != nil { val := *g.VerificationLevel if val < 0 || val > 3 { - err = errors.New("VerificationLevel out of bounds, should be between 0 and 3") + err = ErrVerificationLevelBounds return } } @@ -988,7 +995,7 @@ func (s *Session) GuildPruneCount(guildID string, days uint32) (count uint32, er count = 0 if days <= 0 { - err = errors.New("the number of days should be more than or equal to 1") + err = ErrPruneDaysBounds return } @@ -1018,7 +1025,7 @@ func (s *Session) GuildPrune(guildID string, days uint32) (count uint32, err err count = 0 if days <= 0 { - err = errors.New("the number of days should be more than or equal to 1") + err = ErrPruneDaysBounds return } @@ -1120,7 +1127,7 @@ func (s *Session) GuildIcon(guildID string) (img image.Image, err error) { } if g.Icon == "" { - err = errors.New("guild does not have an icon set") + err = ErrGuildNoIcon return } @@ -1142,7 +1149,7 @@ func (s *Session) GuildSplash(guildID string) (img image.Image, err error) { } if g.Splash == "" { - err = errors.New("guild does not have a splash set") + err = ErrGuildNoSplash return } diff --git a/state.go b/state.go index c2c5519..7400ef6 100644 --- a/state.go +++ b/state.go @@ -21,6 +21,10 @@ import ( // ErrNilState is returned when the state is nil. var ErrNilState = errors.New("state not instantiated, please use discordgo.New() or assign Session.State") +// ErrStateNotFound is returned when the state cache +// requested is not found +var ErrStateNotFound = errors.New("state cache not found") + // A State contains the current known state. // As discord sends this in a READY blob, it seems reasonable to simply // use that struct as the data store. @@ -146,7 +150,7 @@ func (s *State) Guild(guildID string) (*Guild, error) { return g, nil } - return nil, errors.New("guild not found") + return nil, ErrStateNotFound } // PresenceAdd adds a presence to the current world state, or @@ -227,7 +231,7 @@ func (s *State) PresenceRemove(guildID string, presence *Presence) error { } } - return errors.New("presence not found") + return ErrStateNotFound } // Presence gets a presence by ID from a guild. @@ -247,7 +251,7 @@ func (s *State) Presence(guildID, userID string) (*Presence, error) { } } - return nil, errors.New("presence not found") + return nil, ErrStateNotFound } // TODO: Consider moving Guild state update methods onto *Guild. @@ -299,7 +303,7 @@ func (s *State) MemberRemove(member *Member) error { } } - return errors.New("member not found") + return ErrStateNotFound } // Member gets a member by ID from a guild. @@ -322,7 +326,7 @@ func (s *State) Member(guildID, userID string) (*Member, error) { } } - return nil, errors.New("member not found") + return nil, ErrStateNotFound } // RoleAdd adds a role to the current world state, or @@ -372,7 +376,7 @@ func (s *State) RoleRemove(guildID, roleID string) error { } } - return errors.New("role not found") + return ErrStateNotFound } // Role gets a role by ID from a guild. @@ -395,7 +399,7 @@ func (s *State) Role(guildID, roleID string) (*Role, error) { } } - return nil, errors.New("role not found") + return nil, ErrStateNotFound } // ChannelAdd adds a channel to the current world state, or @@ -428,7 +432,7 @@ func (s *State) ChannelAdd(channel *Channel) error { } else { guild, ok := s.guildMap[channel.GuildID] if !ok { - return errors.New("guild for channel not found") + return ErrStateNotFound } guild.Channels = append(guild.Channels, channel) @@ -507,7 +511,7 @@ func (s *State) Channel(channelID string) (*Channel, error) { return c, nil } - return nil, errors.New("channel not found") + return nil, ErrStateNotFound } // Emoji returns an emoji for a guild and emoji id. @@ -530,7 +534,7 @@ func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) { } } - return nil, errors.New("emoji not found") + return nil, ErrStateNotFound } // EmojiAdd adds an emoji to the current world state. @@ -647,7 +651,7 @@ func (s *State) messageRemoveByID(channelID, messageID string) error { } } - return errors.New("message not found") + return ErrStateNotFound } func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error { @@ -701,7 +705,7 @@ func (s *State) Message(channelID, messageID string) (*Message, error) { } } - return nil, errors.New("message not found") + return nil, ErrStateNotFound } // OnReady takes a Ready event and updates all internal state. diff --git a/wsapi.go b/wsapi.go index adab402..0912850 100644 --- a/wsapi.go +++ b/wsapi.go @@ -25,6 +25,18 @@ import ( "github.com/gorilla/websocket" ) +// ErrWSAlreadyOpen is thrown when you attempt to open +// a websocket that already is open. +var ErrWSAlreadyOpen = errors.New("web socket already opened") + +// ErrWSNotFound is thrown when you attempt to use a websocket +// that doesn't exist +var ErrWSNotFound = errors.New("no websocket connection exists") + +// ErrWSShardBounds is thrown when you try to use a shard ID that is +// less than the total shard count +var ErrWSShardBounds = errors.New("ShardID must be less than ShardCount") + type resumePacket struct { Op int `json:"op"` Data struct { @@ -58,7 +70,7 @@ func (s *Session) Open() (err error) { } if s.wsConn != nil { - err = errors.New("web socket already opened") + err = ErrWSAlreadyOpen return } @@ -250,7 +262,7 @@ func (s *Session) UpdateStreamingStatus(idle int, game string, url string) (err s.RLock() defer s.RUnlock() if s.wsConn == nil { - return errors.New("no websocket connection exists") + return ErrWSNotFound } var usd updateStatusData @@ -307,7 +319,7 @@ func (s *Session) RequestGuildMembers(guildID, query string, limit int) (err err s.RLock() defer s.RUnlock() if s.wsConn == nil { - return errors.New("no websocket connection exists") + return ErrWSNotFound } data := requestGuildMembersData{ @@ -621,7 +633,7 @@ func (s *Session) identify() error { if s.ShardCount > 1 { if s.ShardID >= s.ShardCount { - return errors.New("ShardID must be less than ShardCount") + return ErrWSShardBounds } data.Shard = &[2]int{s.ShardID, s.ShardCount}