Getting rid of excessive error types, switching to fmt.Errorf

pull/20/merge
Ilya Kowalewski 9 years ago
parent 613d4d239e
commit 6e16b55de1

@ -124,14 +124,14 @@ func getMe(token string) (User, error) {
err = json.Unmarshal(meJSON, &botInfo)
if err != nil {
return User{}, AuthError{"invalid token"}
return User{}, fmt.Errorf("telebot: invalid token")
}
if botInfo.Ok {
return botInfo.Result, nil
}
return User{}, AuthError{botInfo.Description}
return User{}, fmt.Errorf("telebot: %s", botInfo.Description)
}
func getUpdates(token string, offset int, timeout int) (updates []Update, err error) {
@ -155,7 +155,7 @@ func getUpdates(token string, offset int, timeout int) (updates []Update, err er
}
if !updatesRecieved.Ok {
err = FetchError{updatesRecieved.Description}
err = fmt.Errorf("telebot: %s", updatesRecieved.Description)
return
}

@ -73,7 +73,7 @@ func (b Bot) SendMessage(recipient User, message string, options *SendOptions) e
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
return nil
@ -102,7 +102,7 @@ func (b Bot) ForwardMessage(recipient User, message Message) error {
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
return nil
@ -150,7 +150,7 @@ func (b Bot) SendPhoto(recipient User, photo *Photo, options *SendOptions) error
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
thumbnails := &responseRecieved.Result.Photo
@ -202,7 +202,7 @@ func (b Bot) SendAudio(recipient User, audio *Audio, options *SendOptions) error
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
filename := audio.filename
@ -253,7 +253,7 @@ func (b Bot) SendDocument(recipient User, doc *Document, options *SendOptions) e
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
filename := doc.filename
@ -304,7 +304,7 @@ func (b *Bot) SendSticker(recipient User, sticker *Sticker, options *SendOptions
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
filename := sticker.filename
@ -355,7 +355,7 @@ func (b Bot) SendVideo(recipient User, video *Video, options *SendOptions) error
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
filename := video.filename
@ -399,7 +399,7 @@ func (b Bot) SendLocation(recipient User, geo *Location, options *SendOptions) e
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
return nil
@ -436,7 +436,7 @@ func (b Bot) SendChatAction(recipient User, action string) error {
}
if !responseRecieved.Ok {
return SendError{responseRecieved.Description}
return fmt.Errorf("telebot: %s", responseRecieved.Description)
}
return nil

@ -1,39 +0,0 @@
package telebot
// AuthError occurs if the token appears to be invalid.
type AuthError struct {
Payload string
}
// FetchError occurs when something goes wrong
// while fetching updates.
type FetchError struct {
Payload string
}
// SendError occurs when something goes wrong
// while posting images, documents, etc.
type SendError struct {
Payload string
}
// FileError occurs when local file can't be read.
type FileError struct {
Payload string
}
func (e AuthError) Error() string {
return "AuthError: " + e.Payload
}
func (e FetchError) Error() string {
return "FetchError: " + e.Payload
}
func (e SendError) Error() string {
return "SendError: " + e.Payload
}
func (e FileError) Error() string {
return "FileError: " + e.Payload
}

@ -21,9 +21,7 @@ type File struct {
// a descriptor for it.
func NewFile(path string) (File, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return File{}, FileError{
fmt.Sprintf("'%s' does not exist!", path),
}
return File{}, fmt.Errorf("telebot: '%s' does not exist", path)
}
return File{filename: path}, nil

Loading…
Cancel
Save