Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 30be767

Browse files
committed
fix error cache
1 parent a32e08c commit 30be767

3 files changed

Lines changed: 14 additions & 21 deletions

File tree

api.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"github.com/kataras/iris/v12"
76
)
@@ -96,23 +95,23 @@ func CollectLanguages(username string, repos []interface{}) (data map[string]flo
9695

9796
type AnalysisData struct {
9897
Data iris.Map
99-
Err error
98+
Err string
10099
Code int
101100
}
102101

103102
func AnalysisUser(username string) AnalysisData {
104103
if GetUserExist(username) {
105104
res, err := GetUser(username)
106105
if err != nil {
107-
return AnalysisData{nil, err, iris.StatusInternalServerError}
106+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
108107
}
109108
repos, err := iterRepos(username)
110109
if err != nil {
111-
return AnalysisData{nil, err, iris.StatusInternalServerError}
110+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
112111
}
113112
languages, err := CollectLanguages(username, repos)
114113
if err != nil {
115-
return AnalysisData{nil, err, iris.StatusInternalServerError}
114+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
116115
}
117116
return AnalysisData{
118117
iris.Map{
@@ -126,21 +125,21 @@ func AnalysisUser(username string) AnalysisData {
126125
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
127126
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
128127
"languages": CountLanguages(languages),
129-
}, nil, iris.StatusOK,
128+
}, "", iris.StatusOK,
130129
}
131130
}
132-
return AnalysisData{nil, errors.New("user not found"), iris.StatusNotFound}
131+
return AnalysisData{nil, "user not found", iris.StatusNotFound}
133132
}
134133

135134
func AnalysisRepo(username string, repo string) AnalysisData {
136135
if GetRepoExist(username, repo) {
137136
res, err := GetRepo(username, repo)
138137
if err != nil {
139-
return AnalysisData{nil, err, iris.StatusInternalServerError}
138+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
140139
}
141140
languages, err := GetLanguages(username, repo)
142141
if err != nil {
143-
return AnalysisData{nil, err, iris.StatusInternalServerError}
142+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
144143
}
145144
return AnalysisData{
146145
iris.Map{
@@ -154,8 +153,8 @@ func AnalysisRepo(username string, repo string) AnalysisData {
154153
"color": GetColor(res["language"]),
155154
"license": getLicense(res["license"]),
156155
"languages": CountLanguages(languages),
157-
}, nil, iris.StatusOK,
156+
}, "", iris.StatusOK,
158157
}
159158
}
160-
return AnalysisData{nil, errors.New("repo not found"), iris.StatusNotFound}
159+
return AnalysisData{nil, "repo not found", iris.StatusNotFound}
161160
}

cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func CachedHandler(h iris.Handler, params ...string) iris.Handler {
8989
}
9090

9191
func EndBody(ctx iris.Context, data AnalysisData) {
92-
if data.Err != nil {
93-
ThrowError(ctx, data.Err.Error(), data.Code)
92+
if data.Err != "" {
93+
ThrowError(ctx, data.Err, data.Code)
9494
} else {
9595
ctx.JSON(data.Data)
9696
}

utils.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/kataras/iris/v12"
7-
"github.com/sirupsen/logrus"
87
"io"
98
"net/http"
109
"sort"
@@ -87,7 +86,7 @@ func Get(uri string, ptr interface{}) (err error) {
8786

8887
defer func(Body io.ReadCloser) {
8988
if err := Body.Close(); err != nil {
90-
logrus.Infoln(err)
89+
logger.Infoln(err)
9190
}
9291
}(resp.Body)
9392

@@ -105,16 +104,11 @@ func CountLanguages(languages map[string]float64) []map[string]any {
105104
total += v
106105
}
107106

108-
var cursor float64
109-
110107
for k, v := range languages {
111-
ratio := v / total
112-
cursor += ratio
113108
res = append(res, map[string]any{
114109
"lang": k,
115110
"value": v,
116-
"percent": ratio * 100,
117-
"cursor": cursor,
111+
"percent": v / total * 100,
118112
"color": GetColor(k),
119113
"text": fmt.Sprintf("%.0f%% (%s)", v/total*100, ScaleConvert(v, false)),
120114
})

0 commit comments

Comments
 (0)