Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"slices"
"strings"
)
Expand Down Expand Up @@ -146,3 +147,20 @@ func position(err error) (int, int) {

return 0, 0
}

// newUnreachableError reports that the URL in key did not answer. err
// gives the cause and does not name the URL, so the URL appears once.
func newUnreachableError(key string, u *URL, err error) ValidationError {
return newValidationErrorf(key, "'%s' not reachable: %s", (*url.URL)(u).String(), err.Error())
}

// newNotImageError reports that the file value in key is not an image.
// When the request failed for the URL value itself, the URL is named only
// once. A URL resolved from a relative path stays in the message.
func newNotImageError(key string, value string, err error) ValidationError {
if dlErr, ok := errors.AsType[*downloadError](err); ok && dlErr.URL == value {
err = dlErr.Err
}

return newValidationErrorf(key, "'%s' is not an image: %s", value, err.Error())
}
33 changes: 33 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package publiccode

import (
"encoding/json"
"errors"
"net/url"
"testing"
)

Expand Down Expand Up @@ -121,3 +123,34 @@ func TestValidationWarningErrorEmptyKey(t *testing.T) {
t.Errorf("got %q, want %q", got, want)
}
}

func TestNewUnreachableErrorNamesURLOnce(t *testing.T) {
u, _ := url.Parse("https://example.invalid/x/y.git")
got := newUnreachableError("url", (*URL)(u), errors.New("no such host"))

want := "'https://example.invalid/x/y.git' not reachable: no such host"
if got.Description != want {
t.Errorf("Description = %q, want %q", got.Description, want)
}
}

func TestNewNotImageErrorNamesURLOnce(t *testing.T) {
const raw = "https://example.com/raw/shot.png"

tests := []struct {
value string
want string
}{
{raw, "'" + raw + "' is not an image: not found"},
{"shot.png", "'shot.png' is not an image: downloading " + raw + ": not found"},
}

for _, tt := range tests {
err := &downloadError{URL: raw, Err: errors.New("not found")}

got := newNotImageError("screenshots[0]", tt.value, err)
if got.Description != tt.want {
t.Errorf("Description = %q, want %q", got.Description, tt.want)
}
}
}
63 changes: 24 additions & 39 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU

if checksNetwork && publiccodev0.URL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.URL)); !reachable {
vr = append(vr, newValidationErrorf("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
vr = append(vr, newUnreachableError("url", publiccodev0.URL, err))
}

if !parser.isRepo((*url.URL)(publiccodev0.URL)) {
Expand All @@ -39,19 +39,13 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU

if checksNetwork && publiccodev0.LandingURL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.LandingURL)); !reachable {
vr = append(vr, newValidationErrorf(
"landingURL",
"'%s' not reachable: %s", publiccodev0.LandingURL, err.Error(),
))
vr = append(vr, newUnreachableError("landingURL", publiccodev0.LandingURL, err))
}
}

if checksNetwork && publiccodev0.Roadmap != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.Roadmap)); !reachable {
vr = append(vr, newValidationErrorf(
"roadmap",
"'%s' not reachable: %s", publiccodev0.Roadmap, err.Error(),
))
vr = append(vr, newUnreachableError("roadmap", publiccodev0.Roadmap, err))
}
}

Expand Down Expand Up @@ -213,18 +207,16 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU

if checksNetwork && desc.Documentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.documentation", lang),
"'%s' not reachable: %s", desc.Documentation, err.Error(),
vr = append(vr, newUnreachableError(
fmt.Sprintf("description.%s.documentation", lang), desc.Documentation, err,
))
}
}

if checksNetwork && desc.APIDocumentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.apiDocumentation", lang),
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
vr = append(vr, newUnreachableError(
fmt.Sprintf("description.%s.apiDocumentation", lang), desc.APIDocumentation, err,
))
}
}
Expand All @@ -238,10 +230,7 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU
if u != nil {
isImage, err := parser.isImageFile(*u, network)
if !isImage {
vr = append(vr, newValidationErrorf(
keyName,
"'%s' is not an image: %s", v, err.Error(),
))
vr = append(vr, newNotImageError(keyName, v, err))
}
}
}
Expand Down Expand Up @@ -272,10 +261,17 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU

it := publiccodev0.IT

if publiccodev0.IT != nil {
vr = append(vr, newValidationWarning(
"IT",
"This key is DEPRECATED and will be removed in the future. Migrate its keys as their warnings say, then drop it",
))
}

if publiccodev0.It != nil {
vr = append(vr, newValidationWarning(
"it",
"Lowercase country codes are DEPRECATED and will be removed in the future. Use 'IT' instead",
"This key is DEPRECATED and will be removed in the future. Migrate its keys as their warnings say, then drop it",
))

it = publiccodev0.It
Expand Down Expand Up @@ -359,7 +355,7 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU

if checksNetwork && publiccodev1.URL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.URL)); !reachable {
vr = append(vr, newValidationErrorf("url", "'%s' not reachable: %s", publiccodev1.URL, err.Error()))
vr = append(vr, newUnreachableError("url", publiccodev1.URL, err))
}

if !parser.isRepo((*url.URL)(publiccodev1.URL)) {
Expand All @@ -369,19 +365,13 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU

if checksNetwork && publiccodev1.LandingURL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.LandingURL)); !reachable {
vr = append(vr, newValidationErrorf(
"landingURL",
"'%s' not reachable: %s", publiccodev1.LandingURL, err.Error(),
))
vr = append(vr, newUnreachableError("landingURL", publiccodev1.LandingURL, err))
}
}

if checksNetwork && publiccodev1.Roadmap != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.Roadmap)); !reachable {
vr = append(vr, newValidationErrorf(
"roadmap",
"'%s' not reachable: %s", publiccodev1.Roadmap, err.Error(),
))
vr = append(vr, newUnreachableError("roadmap", publiccodev1.Roadmap, err))
}
}

Expand Down Expand Up @@ -429,18 +419,16 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU
for lang, desc := range publiccodev1.Description {
if checksNetwork && desc.Documentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.documentation", lang),
"'%s' not reachable: %s", desc.Documentation, err.Error(),
vr = append(vr, newUnreachableError(
fmt.Sprintf("description.%s.documentation", lang), desc.Documentation, err,
))
}
}

if checksNetwork && desc.APIDocumentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.apiDocumentation", lang),
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
vr = append(vr, newUnreachableError(
fmt.Sprintf("description.%s.apiDocumentation", lang), desc.APIDocumentation, err,
))
}
}
Expand All @@ -454,10 +442,7 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU
if u != nil {
isImage, err := parser.isImageFile(*u, network)
if !isImage {
vr = append(vr, newValidationErrorf(
keyName,
"'%s' is not an image: %s", v, err.Error(),
))
vr = append(vr, newNotImageError(keyName, v, err))
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions internal/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
neturl "net/url"
"strconv"
"time"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func New(doer Doer) *Client {
// it waits and tries again: for as long as Retry-After says, until the
// time in X-Ratelimit-Reset, or with an exponential backoff from one
// minute when the server gave no time. A wait the server asks for is
// capped at 30 seconds.
// capped at 30 seconds. Errors give the cause without naming url.
func (c *Client) Get(url string, headers map[string]string) ([]byte, error) {
return c.GetWithContext(context.Background(), url, headers)
}
Expand Down Expand Up @@ -118,7 +119,13 @@ func (c *Client) do(ctx context.Context, url string, headers map[string]string)

resp, err := c.doer.Do(req)
if err != nil {
return nil, fmt.Errorf("requesting %s: %w", url, err)
// *url.Error names the method and the URL. The caller names the
// URL in its own message, so return only the cause.
if urlErr, ok := errors.AsType[*neturl.Error](err); ok {
return nil, urlErr.Err
}

return nil, fmt.Errorf("sending the request: %w", err)
}

return resp, nil
Expand Down
22 changes: 22 additions & 0 deletions internal/httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
neturl "net/url"
"slices"
"testing"
"time"
Expand Down Expand Up @@ -359,3 +360,24 @@ func TestGetCountsBlindRetriesOnTheirOwn(t *testing.T) {
t.Errorf("waits = %v, want %v", *waits, want)
}
}

type doerFunc func(*http.Request) (*http.Response, error)

func (f doerFunc) Do(req *http.Request) (*http.Response, error) {
return f(req)
}

func TestGetTransportErrorOmitsURL(t *testing.T) {
errDial := errors.New("dial tcp: lookup example.invalid: no such host")
c := New(doerFunc(func(req *http.Request) (*http.Response, error) {
return nil, &neturl.Error{Op: "Get", URL: req.URL.String(), Err: errDial}
}))

_, err := c.Get("https://example.invalid/x/y.git", nil)
if !errors.Is(err, errDial) {
t.Fatalf("err = %v, want %v", err, errDial)
}
if err.Error() != errDial.Error() {
t.Errorf("err = %q, want only the cause %q", err, errDial)
}
}
9 changes: 0 additions & 9 deletions testdata/v0/valid/dependsOn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,3 @@ dependsOn: # List of dependencies. The only mandatory list is the proprietary on
optional: true
- name: NFC Reader2
optional: true

IT:
countryExtensionVersion: "0.2"

piattaforme:
spid: false
pagopa: false
cie: false
anpr: false
8 changes: 0 additions & 8 deletions testdata/v0/valid/no-network/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,3 @@ dependsOn:
hardware:
- name: NFC Reader
optional: true

IT:
piattaforme:
spid: false
pagopa: false
cie: false
anpr: false
io: false
8 changes: 0 additions & 8 deletions testdata/v0/valid/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,3 @@ dependsOn:
hardware:
- name: NFC Reader
optional: true

IT:
piattaforme:
spid: false
pagopa: false
cie: false
anpr: false
io: false
Loading