fix(telegram): a message Telegram refuses is a permanent failure, not retried (#2016)
This commit is contained in:
3 files changed
+52
-1
No files matched your search
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json/jsontext"
|
||||
json "encoding/json/v2"
|
||||
@@ -279,6 +280,7 @@ func (t *telegram) call(ctx context.Context, method string, body, out any) error
|
||||
}
|
||||
var env struct {
|
||||
OK bool `json:"ok"`
|
||||
ErrorCode int `json:"error_code"`
|
||||
Description string `json:"description"`
|
||||
Result jsontext.Value `json:"result"`
|
||||
}
|
||||
@@ -286,7 +288,14 @@ func (t *telegram) call(ctx context.Context, method string, body, out any) error
|
||||
return fmt.Errorf("%s: %w", method, err)
|
||||
}
|
||||
if !env.OK {
|
||||
return fmt.Errorf("%s: %s", method, env.Description)
|
||||
err := fmt.Errorf("%s: %s", method, env.Description)
|
||||
// Telegram answers 400, 403 and 404 for a request it will refuse
|
||||
// again: a bad message, a blocked bot, a missing chat.
|
||||
switch cmp.Or(env.ErrorCode, res.StatusCode) {
|
||||
case http.StatusBadRequest, http.StatusForbidden, http.StatusNotFound:
|
||||
return connector.Permanent(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if out == nil {
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package connector
|
||||
|
||||
import "errors"
|
||||
|
||||
// permanentError marks a delivery failure no retry can fix.
|
||||
type permanentError struct{ err error }
|
||||
|
||||
func (e *permanentError) Error() string { return e.err.Error() }
|
||||
func (e *permanentError) Unwrap() error { return e.err }
|
||||
|
||||
// Permanent marks err as a refusal the same delivery will meet again, such as a
|
||||
// message the service says is invalid or a chat that is gone. The host records
|
||||
// the reason and stops retrying it; any other Deliver error is retried.
|
||||
func Permanent(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &permanentError{err: err}
|
||||
}
|
||||
|
||||
// IsPermanent reports whether err was marked with Permanent. A joined error is
|
||||
// permanent only when every error in it is.
|
||||
func IsPermanent(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var perm *permanentError
|
||||
if errors.As(err, &perm) {
|
||||
if joined, ok := err.(interface{ Unwrap() []error }); ok {
|
||||
for _, e := range joined.Unwrap() {
|
||||
if !IsPermanent(e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -88,6 +88,9 @@ func (s *server) Deliver(ctx context.Context, req *connectorv1.DeliverRequest) (
|
||||
}
|
||||
id, err := d.Deliver(ctx, req.GetDelivery(), req.GetInput(), req.GetIdempotencyKey())
|
||||
if err != nil {
|
||||
if IsPermanent(err) {
|
||||
return nil, status.Errorf(codes.FailedPrecondition, "deliver: %v", err)
|
||||
}
|
||||
return nil, status.Errorf(codes.Unknown, "deliver: %v", err)
|
||||
}
|
||||
return &connectorv1.DeliverResponse{ExternalId: id}, nil
|
||||
|
||||
Reference in new issue
Block a user