feat: client core, accounts, link URI, errors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c78dd3bd1
commit
a3136305d3
7 files changed
+583
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package signalbot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotLinked means the service has no linked account (or not the one
|
||||
// requested with WithNumber). Run `signalbot link`.
|
||||
ErrNotLinked = errors.New("signalbot: no linked account")
|
||||
|
||||
// ErrMultipleAccounts means the service has several linked accounts and
|
||||
// no WithNumber option was given to choose one.
|
||||
ErrMultipleAccounts = errors.New("signalbot: several linked accounts, use WithNumber")
|
||||
|
||||
// ErrUnavailable means the service could not be reached or answered with
|
||||
// a server error (5xx).
|
||||
ErrUnavailable = errors.New("signalbot: service unavailable")
|
||||
|
||||
// ErrRateLimited means Signal rate-limited the account (HTTP 429).
|
||||
ErrRateLimited = errors.New("signalbot: rate limited")
|
||||
|
||||
// ErrEmptyText means Send was called with an empty or blank text.
|
||||
ErrEmptyText = errors.New("signalbot: empty text")
|
||||
)
|
||||
|
||||
// APIError is an error response from signal-cli-rest-api.
|
||||
//
|
||||
// errors.Is reports ErrUnavailable for 5xx responses and ErrRateLimited
|
||||
// for 429 responses.
|
||||
type APIError struct {
|
||||
StatusCode int
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return fmt.Sprintf("signalbot: API error %d: %s", e.StatusCode, e.Message)
|
||||
}
|
||||
|
||||
func (e *APIError) Is(target error) bool {
|
||||
switch target {
|
||||
case ErrUnavailable:
|
||||
return e.StatusCode >= 500
|
||||
case ErrRateLimited:
|
||||
return e.StatusCode == http.StatusTooManyRequests
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func unavailable(err error) error {
|
||||
return fmt.Errorf("%w: %w", ErrUnavailable, err)
|
||||
}
|
||||
Reference in new issue
Block a user