Skip to content

Commit

Permalink
debugging: add printing of request & response for schedule override
Browse files Browse the repository at this point in the history
This is to help with debugging issue #339. This branch is based off of #325, so
that I could make use of the new response debugging functionality I added.

I ran the following commands from within the `command` directory to get the
below result:

```shell
go build -o pd .
./pd schedule override create -authtoken "$(cat ../api.key)" PF4QFIN debug_override.json
```

The `api.key` file is just an API key from the PD console. `PF4QFIN` is a
PagerDuty service in my development account. Here is the output:

```
time="2021-07-04T01:00:24-07:00" level=info msg="service id is:PF4QFIN"
time="2021-07-04T01:00:24-07:00" level=info msg="Input file is:debug_override.json"
req:
req.Method: POST
req.URL: https://api.pagerduty.com/schedules/PF4QFIN/overrides
req.Header: http.Header{"Accept":[]string{"application/vnd.pagerduty+json;version=2"}, "Authorization":[]string{"Token token=u+XP5yPH7pzsQ3aJA5EQ"}, "Content-Type":[]string{"application/json"}, "User-Agent":[]string{"go-pagerduty/1.5.0"}}
req.Body:
{"override":{"start":"2021-06-29T12:00:00Z","end":"2021-06-29T18:00:00Z","user":{"id":"P6QKA9O"}}}

err: HTTP response with status code 400, JSON error object decode failed: json: cannot unmarshal string into Go struct field APIError.error of type []string

resp:
resp.Status: 400 Bad Request
resp.Header: http.Header{"Access-Control-Allow-Headers":[]string{"Authorization, Content-Type, AuthorizationOauth, X-EARLY-ACCESS"}, "Access-Control-Allow-Methods":[]string{"GET, POST, PUT, DELETE, OPTIONS"}, "Access-Control-Allow-Origin":[]string{"*"}, "Access-Control-Expose-Headers":[]string{""}, "Access-Control-Max-Age":[]string{"1728000"}, "Cache-Control":[]string{"no-cache"}, "Connection":[]string{"keep-alive"}, "Content-Length":[]string{"95"}, "Content-Type":[]string{"application/json"}, "Date":[]string{"Sun, 04 Jul 2021 08:00:24 GMT"}, "Server":[]string{"nginx"}, "X-Request-Id":[]string{"f6cda459-587c-49a6-ab98-17fc5bd60642"}}
resp.Body:
{"error":{"message":"Invalid Override","code":2001,"errors":"Invalid input for this override"}}

```

Note that `error.errors` is not an array, it's a single string. This seems to
violate the API spec.
  • Loading branch information
theckman committed Jul 4, 2021
1 parent 919619a commit b053fc7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
16 changes: 16 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,22 @@ func (c *Client) doWithEndpoint(ctx context.Context, endpoint, method, path stri

c.prepRequest(req, authRequired, headers)

fmt.Println("req:")
fmt.Printf("req.Method: %s\n", req.Method)
fmt.Printf("req.URL: %s\n", req.URL.String())
fmt.Printf("req.Header: %#v\n", req.Header)

data, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err.Error())
}

req.Body.Close()

fmt.Printf("req.Body:\n%s\n", string(data))

req.Body = ioutil.NopCloser(bytes.NewReader(data))

resp, err := c.HTTPClient.Do(req)

// debug mode support
Expand Down
7 changes: 7 additions & 0 deletions command/debug_override.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"start": "2021-06-29T12:00:00Z",
"end": "2021-06-29T18:00:00Z",
"user": {
"id": "P6QKA9O"
}
}
29 changes: 24 additions & 5 deletions command/schedule_override_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"encoding/json"
"fmt"
"github.com/PagerDuty/go-pagerduty"
log "github.com/sirupsen/logrus"
"github.com/mitchellh/cli"
"io/ioutil"
"os"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
)

type ScheduleOverrideCreate struct {
Expand Down Expand Up @@ -41,14 +43,15 @@ func (c *ScheduleOverrideCreate) Run(args []string) int {
return -1
}
client := c.Meta.Client()
client.SetDebugFlag(pagerduty.DebugCaptureLastResponse)
var o pagerduty.Override
if len(flags.Args()) != 2 {
log.Error("Please specify input json file")
return -1
}
log.Info("service id is:", flags.Arg(0))
log.Info("Input file is:", flags.Arg(1))
f, err := os.Open(flags.Arg(0))
f, err := os.Open(flags.Arg(1))
if err != nil {
log.Error(err)
return -1
Expand All @@ -62,7 +65,23 @@ func (c *ScheduleOverrideCreate) Run(args []string) int {
log.Debugf("%#v", o)
o1, err := client.CreateOverride(flags.Arg(0), o)
if err != nil {
log.Error(err)
// log.Error(err)

fmt.Printf("\n\nerr: %s\n\n", err)

resp, ok := client.LastAPIResponse()
if ok {
fmt.Println("resp:")
fmt.Printf("resp.Status: %s\n", resp.Status)
fmt.Printf("resp.Header: %#v\n", resp.Header)

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}

fmt.Printf("resp.Body:\n%s\n", string(body))
}
return -1
}
log.Println("New override id:", o1.ID)
Expand Down

0 comments on commit b053fc7

Please sign in to comment.