forked from pothtonswer/discordmuffin
Return a consistent JSON Unmarshal error.
This commit is contained in:
parent
5b312ac464
commit
f15b389ac8
2 changed files with 49 additions and 38 deletions
82
restapi.go
82
restapi.go
|
@ -13,6 +13,7 @@ package discordgo
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
_ "image/jpeg" // For JPEG decoding
|
_ "image/jpeg" // For JPEG decoding
|
||||||
|
@ -24,6 +25,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrJSONUnmarshal = errors.New("json unmarshal")
|
||||||
|
|
||||||
// Request makes a (GET/POST/...) Requests to Discord REST API.
|
// Request makes a (GET/POST/...) Requests to Discord REST API.
|
||||||
// All the other Discord REST Calls in this file use this function.
|
// All the other Discord REST Calls in this file use this function.
|
||||||
func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
|
func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
|
||||||
|
@ -112,6 +115,15 @@ func (s *Session) Request(method, urlStr string, data interface{}) (response []b
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
err := json.Unmarshal(data, v)
|
||||||
|
if err != nil {
|
||||||
|
return ErrJSONUnmarshal
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// Functions specific to Discord Sessions
|
// Functions specific to Discord Sessions
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
@ -133,7 +145,7 @@ func (s *Session) Login(email string, password string) (token string, err error)
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
err = json.Unmarshal(response, &temp)
|
err = s.Unmarshal(response, &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -160,7 +172,7 @@ func (s *Session) Register(username string) (token string, err error) {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
err = json.Unmarshal(response, &temp)
|
err = s.Unmarshal(response, &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -202,7 +214,7 @@ func (s *Session) User(userID string) (st *User, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +257,7 @@ func (s *Session) UserUpdate(email, password, username, avatar, newPassword stri
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +269,7 @@ func (s *Session) UserSettings() (st *Settings, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +282,7 @@ func (s *Session) UserChannels() (st []*Channel, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,7 +299,7 @@ func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,7 +311,7 @@ func (s *Session) UserGuilds() (st []*Guild, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,7 +328,7 @@ func (s *Session) Guild(guildID string) (st *Guild, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,7 +345,7 @@ func (s *Session) GuildCreate(name string) (st *Guild, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,7 +363,7 @@ func (s *Session) GuildEdit(guildID, name string) (st *Guild, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,7 +376,7 @@ func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,7 +390,7 @@ func (s *Session) GuildBans(guildID string) (st []*User, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -427,7 +439,7 @@ func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -448,7 +460,7 @@ func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel,
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -460,7 +472,7 @@ func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,7 +494,7 @@ func (s *Session) GuildInviteCreate(guildID string, i *Invite) (st *Invite, err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,7 +506,7 @@ func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return // TODO return pointer
|
return // TODO return pointer
|
||||||
}
|
}
|
||||||
|
@ -507,7 +519,7 @@ func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -527,7 +539,7 @@ func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist b
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -540,7 +552,7 @@ func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []*Role, er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -565,7 +577,7 @@ func (s *Session) Channel(channelID string) (st *Channel, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -583,7 +595,7 @@ func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -596,7 +608,7 @@ func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -638,7 +650,7 @@ func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, aft
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -669,7 +681,7 @@ func (s *Session) ChannelMessageSend(channelID string, content string) (st *Mess
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(response, &st)
|
err = s.Unmarshal(response, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -688,7 +700,7 @@ func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(response, &st)
|
err = s.Unmarshal(response, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -708,7 +720,7 @@ func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -730,7 +742,7 @@ func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, e
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -771,7 +783,7 @@ func (s *Session) Invite(inviteID string) (st *Invite, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -784,7 +796,7 @@ func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -797,7 +809,7 @@ func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -813,7 +825,7 @@ func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -825,7 +837,7 @@ func (s *Session) VoiceICE() (st *VoiceICE, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &st)
|
err = s.Unmarshal(body, &st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -845,7 +857,7 @@ func (s *Session) Gateway() (gateway string, err error) {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
err = json.Unmarshal(response, &temp)
|
err = s.Unmarshal(response, &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
5
wsapi.go
5
wsapi.go
|
@ -11,7 +11,6 @@
|
||||||
package discordgo
|
package discordgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
@ -166,7 +165,7 @@ func (s *Session) Listen() (err error) {
|
||||||
// somewhere.
|
// somewhere.
|
||||||
|
|
||||||
func unmarshalEvent(event *Event, i interface{}) (err error) {
|
func unmarshalEvent(event *Event, i interface{}) (err error) {
|
||||||
if err = json.Unmarshal(event.RawData, i); err != nil {
|
if err = s.Unmarshal(event.RawData, i); err != nil {
|
||||||
fmt.Println(event.Type, err)
|
fmt.Println(event.Type, err)
|
||||||
printJSON(event.RawData) // TODO: Better error loggingEvent.
|
printJSON(event.RawData) // TODO: Better error loggingEvent.
|
||||||
}
|
}
|
||||||
|
@ -187,7 +186,7 @@ func (s *Session) event(messageType int, message []byte) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var e *Event
|
var e *Event
|
||||||
if err = json.Unmarshal(message, &e); err != nil {
|
if err = s.Unmarshal(message, &e); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue