-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,72 @@ | ||
package twittermeow | ||
|
||
import "errors" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// Connection errors | ||
ErrConnectPleaseSetEventHandler = errors.New("please set event handler in client before connecting") | ||
ErrNotAuthenticatedYet = errors.New("client has not been authenticated yet") | ||
ErrConnectSetEventHandler = errors.New("event handler must be set before connecting") | ||
ErrNotAuthenticatedYet = errors.New("client has not been authenticated yet") | ||
|
||
// Polling errors | ||
ErrAlreadyPollingUpdates = errors.New("client is already polling for user updates") | ||
ErrNotPollingUpdates = errors.New("client is not polling for user updates") | ||
) | ||
|
||
type TwitterError struct { | ||
Message string `json:"message"` | ||
Code int `json:"code"` | ||
} | ||
|
||
// Api errors | ||
ErrFailedMarkConversationRead = errors.New("failed to mark conversation as read") | ||
var ( | ||
ErrCouldNotAuthenticate error = TwitterError{Code: 32} | ||
ErrUserSuspended error = TwitterError{Code: 63} | ||
ErrAccountSuspended error = TwitterError{Code: 63} | ||
ErrAccountTemporarilyLocked error = TwitterError{Code: 326} | ||
) | ||
|
||
func IsAuthError(err error) bool { | ||
return errors.Is(err, ErrCouldNotAuthenticate) || | ||
errors.Is(err, ErrUserSuspended) || | ||
errors.Is(err, ErrAccountSuspended) || | ||
errors.Is(err, ErrAccountTemporarilyLocked) | ||
} | ||
|
||
func (te TwitterError) Is(other error) bool { | ||
var ote *TwitterError | ||
if errors.As(other, &ote) { | ||
return te.Code == ote.Code || te.Message == ote.Message | ||
} | ||
return false | ||
} | ||
|
||
func (te TwitterError) Error() string { | ||
return fmt.Sprintf("%d: %s", te.Code, te.Message) | ||
} | ||
|
||
type TwitterErrors struct { | ||
Errors []TwitterError `json:"errors"` | ||
} | ||
|
||
func (te *TwitterErrors) Error() string { | ||
if te == nil || len(te.Errors) == 0 { | ||
return "no errors" | ||
} else if len(te.Errors) == 1 { | ||
return te.Errors[0].Error() | ||
} else { | ||
errs := make([]string, len(te.Errors)) | ||
for i, e := range te.Errors { | ||
errs[i] = e.Error() | ||
} | ||
return strings.Join(errs, ", ") | ||
} | ||
} | ||
|
||
func (te *TwitterErrors) Unwrap() []error { | ||
errs := make([]error, len(te.Errors)) | ||
for i, e := range te.Errors { | ||
errs[i] = e | ||
} | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters