diff --git a/sign/pdfsignature.go b/sign/pdfsignature.go index 216d262..f8c6a57 100644 --- a/sign/pdfsignature.go +++ b/sign/pdfsignature.go @@ -404,27 +404,20 @@ func (context *SignContext) GetTSA(sign_content []byte) (timestamp_response []by client := &http.Client{} resp, err := client.Do(req) - code := 0 - - if resp != nil { - code = resp.StatusCode + if err != nil { + return nil, fmt.Errorf("POST %s failed: %w", context.SignData.TSA.URL, err) } - if err != nil || (code < 200 || code > 299) { - if err == nil { - defer func() { - _ = resp.Body.Close() - }() - body, _ := io.ReadAll(resp.Body) - return nil, errors.New("non success response (" + strconv.Itoa(code) + "): " + string(body)) - } + defer resp.Body.Close() - return nil, errors.New("non success response (" + strconv.Itoa(code) + ")") + if resp.StatusCode < 200 || resp.StatusCode > 299 { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf( + "TSA returned HTTP %d: %s", + resp.StatusCode, + string(body), + ) } - - defer func() { - _ = resp.Body.Close() - }() timestamp_response_body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response: %w", err)