mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-02-10 06:40:17 +00:00
Log Web Push server error messages
This commit is contained in:
parent
56a31656ef
commit
23847f0e18
|
@ -21,6 +21,7 @@
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -57,6 +58,9 @@ func NewRealSender(httpClient *http.Client, state *state.State) Sender {
|
||||||
// while waiting for the client to retrieve them.
|
// while waiting for the client to retrieve them.
|
||||||
const TTL = 48 * time.Hour
|
const TTL = 48 * time.Hour
|
||||||
|
|
||||||
|
// responseBodyMaxLen limits how much of the Web Push server response we use for error messages.
|
||||||
|
const responseBodyMaxLen = 1024
|
||||||
|
|
||||||
func (r *realSender) Send(
|
func (r *realSender) Send(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
notification *gtsmodel.Notification,
|
notification *gtsmodel.Notification,
|
||||||
|
@ -229,10 +233,22 @@ func (r *realSender) sendToSubscription(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gtserror.Newf("error sending Web Push notification: %w", err)
|
return gtserror.Newf("error sending Web Push notification: %w", err)
|
||||||
}
|
}
|
||||||
// We're not going to use the response body, but we need to close it so we don't leak the connection.
|
defer func() {
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// If there's an error, log the response.
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return gtserror.Newf("unexpected HTTP status received when sending Web Push notification: %s", resp.Status)
|
bodyBytes, err := io.ReadAll(io.LimitReader(resp.Body, responseBodyMaxLen))
|
||||||
|
if err != nil {
|
||||||
|
return gtserror.Newf("error reading Web Push server response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return gtserror.Newf(
|
||||||
|
"unexpected HTTP status %s received when sending Web Push notification: %s",
|
||||||
|
resp.Status,
|
||||||
|
string(bodyBytes),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue