Files

59 lines
1.7 KiB
Go

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")
// ErrNotJSONRPC means the service's receive endpoint is not a websocket:
// the service must run with MODE=json-rpc or MODE=json-rpc-native.
ErrNotJSONRPC = errors.New("signalbot: receive endpoint is not a websocket, run the service with MODE=json-rpc or json-rpc-native")
// 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)
}