Skip to content

Commit 906a897

Browse files
committed
alerting: extract more information from PagerDuty error responses
Issue: #171 Issue: PagerDuty/go-pagerduty#476
1 parent ffc226d commit 906a897

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

internal/alerting/pagerduty.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"crypto/sha256"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"runtime"
11+
"strings"
1012
"time"
1113

1214
"github.com/PagerDuty/go-pagerduty"
@@ -23,6 +25,9 @@ func NewPagerDutyAlerter(cfg *service.PagerDutyAlerting) (*PagerDuty, error) {
2325
client: pagerduty.NewClient(cfg.ApiKey),
2426
routingKey: cfg.RoutingKey,
2527
}
28+
if notifier.client != nil {
29+
notifier.client.SetDebugFlag(pagerduty.DebugCaptureLastResponse)
30+
}
2631
if err := notifier.ping(); err != nil {
2732
return nil, err
2833
}
@@ -69,9 +74,20 @@ func (pd *PagerDuty) AlertError(e error) error {
6974
}
7075

7176
ctx := context.Background()
72-
_, err = pd.client.ManageEventWithContext(ctx, event)
77+
v2EventResponse, err := pd.client.ManageEventWithContext(ctx, event)
7378
if err != nil {
74-
return fmt.Errorf("creating event in PagerDuty: %v", err)
79+
var httpRespBody []byte
80+
httpResp, _ := pd.client.LastAPIResponse()
81+
if httpResp != nil && httpResp.Body != nil {
82+
httpRespBody, _ = io.ReadAll(httpResp.Body)
83+
}
84+
var outErr error
85+
if v2EventResponse != nil {
86+
outErr = fmt.Errorf("%s problem creating PagerDuty event caused by %s: %s", v2EventResponse.Status, v2EventResponse.Message, strings.Join(v2EventResponse.Errors, ", "))
87+
} else {
88+
outErr = fmt.Errorf("unexpected response of %s from creating event in PagerDuty: %v", string(httpRespBody), err)
89+
}
90+
return outErr
7591
}
7692

7793
return nil

0 commit comments

Comments
 (0)