Skip to content

Commit 2d9ea2e

Browse files
authored
Merge pull request #28 from kyoshidajp/report_errors
Report errors detail
2 parents 66d28ad + 1b41663 commit 2d9ea2e

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

cmd/diagnose.go

+34-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,34 @@ type Diagnosis struct {
3232
Ignored bool
3333
Diagnosed bool
3434
IsActive bool
35+
Error error
36+
}
37+
38+
func (d *Diagnosis) ErrorMessage() string {
39+
if d.Error == nil {
40+
return ""
41+
}
42+
return fmt.Sprintf("%s", d.Error)
3543
}
3644

3745
type MedicalTechnician interface {
3846
Libraries(r parser_io.ReadSeekerAt) []types.Library
3947
SourceCodeURL(lib types.Library) (string, error)
4048
}
4149

42-
func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) []github.FetchRepositoryParam {
50+
type RepositoryParams []github.FetchRepositoryParam
51+
52+
func (p RepositoryParams) CanSearchParams() []github.FetchRepositoryParam {
53+
params := []github.FetchRepositoryParam{}
54+
for _, param := range p {
55+
if param.CanSearch {
56+
params = append(params, param)
57+
}
58+
}
59+
return params
60+
}
61+
62+
func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) RepositoryParams {
4363
var params []github.FetchRepositoryParam
4464
var wg sync.WaitGroup
4565
sem := make(chan struct{}, FETCH_REPOS_PER_ONCE)
@@ -55,6 +75,13 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) []github.F
5575

5676
url, err := g.SourceCodeURL(lib)
5777
if err != nil {
78+
params = append(params,
79+
github.FetchRepositoryParam{
80+
PackageName: lib.Name,
81+
CanSearch: false,
82+
Error: err,
83+
},
84+
)
5885
return
5986
}
6087

@@ -64,6 +91,7 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) []github.F
6491
github.FetchRepositoryParam{
6592
PackageName: lib.Name,
6693
CanSearch: false,
94+
Error: err,
6795
},
6896
)
6997
return
@@ -90,14 +118,15 @@ func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []st
90118
slicedParams := [][]github.FetchRepositoryParam{}
91119
libs := d.Libraries(r)
92120
fetchRepositoryParams := FetchRepositoryParams(libs, d)
93-
sliceSize := len(fetchRepositoryParams)
121+
canSearchRepositoryParams := fetchRepositoryParams.CanSearchParams()
122+
sliceSize := len(canSearchRepositoryParams)
94123

95124
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
96125
end := i + github.SEARCH_REPOS_PER_ONCE
97126
if sliceSize < end {
98127
end = sliceSize
99128
}
100-
slicedParams = append(slicedParams, fetchRepositoryParams[i:end])
129+
slicedParams = append(slicedParams, canSearchRepositoryParams[i:end])
101130
}
102131

103132
var wg sync.WaitGroup
@@ -135,6 +164,7 @@ func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []st
135164
diagnosis := Diagnosis{
136165
Name: fetchRepositoryParam.PackageName,
137166
Diagnosed: false,
167+
Error: fetchRepositoryParam.Error,
138168
}
139169
diagnoses[fetchRepositoryParam.PackageName] = diagnosis
140170
}
@@ -225,7 +255,7 @@ func Report(diagnoses map[string]Diagnosis) error {
225255
}
226256

227257
if !diagnosis.Diagnosed {
228-
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown):", diagnosis.Name))
258+
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown): %s", diagnosis.Name, diagnosis.ErrorMessage()))
229259
unDiagnosedCount += 1
230260
warnCount += 1
231261
continue

cmd/github/github.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package github
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"log"
87
"os"
@@ -39,6 +38,7 @@ type FetchRepositoryParam struct {
3938
Repo string
4039
Owner string
4140
CanSearch bool
41+
Error error
4242
}
4343

4444
func (n FetchRepositoryParam) QueryWord() string {
@@ -51,12 +51,12 @@ type GitHubURL struct {
5151

5252
func (githuburl GitHubURL) Parse() (string, string, error) {
5353
if githuburl.URL == "" {
54-
return "", "", errors.New("error: Blank URL")
54+
return "", "", fmt.Errorf("source code URL is blank")
5555
}
5656

5757
u, err := giturls.Parse(githuburl.URL)
5858
if err != nil {
59-
return "", "", errors.New("error: Unknown URL")
59+
return "", "", fmt.Errorf("unknown source code URL: %s", githuburl.URL)
6060
}
6161

6262
var owner, repo string
@@ -66,7 +66,7 @@ func (githuburl GitHubURL) Parse() (string, string, error) {
6666
repo = strings.Replace(paths[1], ".git", "", 1)
6767
} else if u.Scheme == "https" || u.Scheme == "http" {
6868
if len(paths) < 3 {
69-
return "", "", errors.New("error: Unknown URL")
69+
return "", "", fmt.Errorf("unknown source code URL: %s", githuburl.URL)
7070
}
7171
owner = paths[1]
7272
repo = strings.Replace(paths[2], ".git", "", 1)
@@ -78,7 +78,10 @@ func (githuburl GitHubURL) Parse() (string, string, error) {
7878
owner = paths[3]
7979
repo = strings.Replace(paths[4], ".git", "", 1)
8080
}
81+
} else {
82+
return "", "", fmt.Errorf("unknown source code URL: %s", githuburl.URL)
8183
}
84+
8285
return owner, repo, nil
8386
}
8487

cmd/ruby_gems.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ func (g *RubyGems) fetchURLFromRegistry(client http.Client) (string, error) {
5353
return Gem.HomepageUri, nil
5454
}
5555

56-
return "", nil
56+
return "", errors.New("source code URL is not found")
5757
}

0 commit comments

Comments
 (0)