go-resty / resty

Simple HTTP and REST client library for Go
MIT License
10.01k stars 706 forks source link

panic when URL address is down #873

Closed deferbot closed 2 weeks ago

deferbot commented 2 weeks ago

if URL address is down, call panics on v2.15.2, on v2.14.0 it works ok.

with v2.15.2:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x104d74e5c]

goroutine 1 [running]:
github.com/go-resty/resty/v2.(*Request).Execute.func1()
    /Users/deferbot/go/pkg/mod/github.com/go-resty/resty/v2@v2.15.2/request.go:995 +0x134
panic({0x104e561c0?, 0x105086270?})
    /opt/homebrew/Cellar/go/1.23.1/libexec/src/runtime/panic.go:785 +0x124
github.com/go-resty/resty/v2.responseLogger(0x14000164288, 0x14000096050)
    /Users/deferbot/go/pkg/mod/github.com/go-resty/resty/v2@v2.15.2/middleware.go:378 +0x29c
github.com/go-resty/resty/v2.(*Client).execute(0x14000164288, 0x1400018a000)
    /Users/deferbot/go/pkg/mod/github.com/go-resty/resty/v2@v2.15.2/client.go:1247 +0x258
github.com/go-resty/resty/v2.(*Request).Execute(0x1400018a000, {0x104d7df60?, 0x104d7e509?}, {0x104d7f161, 0x8})
    /Users/deferbot/go/pkg/mod/github.com/go-resty/resty/v2@v2.15.2/request.go:1019 +0x520
github.com/go-resty/resty/v2.(*Request).Get(...)
    /Users/deferbot/go/pkg/mod/github.com/go-resty/resty/v2@v2.15.2/request.go:935
main.main()
    /Users/deferbot/develop/tests/resty-error/main.go:31 +0x464

with v2.14.0:

2024/09/26 12:01:13 Get "http://64.5.122.227:3040/7185551212": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

This is the sample code:

package main

import (
    "context"
    "log"
    "time"

    "github.com/go-resty/resty/v2"
)

type phoneData struct {
    T string `json:"phone,omitempty"`
    P int    `json:"provider,omitempty"`
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*6)
    defer cancel()

    c := resty.New().
        SetDebug(true).
        SetTimeout(3 * time.Second).
        SetBaseURL("http://64.5.122.227:3040").
        SetHeaders(map[string]string{
            "Content-Type": "application/json; charset=utf-8",
            "User-Agent":   "utd.store",
        })

    body := phoneData{}

    resp, err := c.R().SetContext(ctx).SetResult(&body).SetPathParam("phone", "7185551212").Get("/{phone}")
    if err != nil {
        log.Fatal(err)
    }

    if resp.IsError() {
        log.Fatal(resp.Error())
    }

    log.Printf("body: %+v\n", body)
}
jeevatkm commented 2 weeks ago

@deferbot Thanks for reaching out. Coincidently, @matlockx submitted a PR to address this nil panic with #872

I will release v2.15.3 once I get home. Thanks.

jeevatkm commented 2 weeks ago

@deferbot I was able to make the release prep work on the browser. Now, v2.15.3 has been released.

deferbot commented 2 weeks ago

Great, it is working now with v2.15.3.

Thanks.