Skip to content

Commit 72f1676

Browse files
committed
early return before timeout
1 parent c91a8a1 commit 72f1676

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

external/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func (r *Repository) GetRepoData(ctx context.Context, userName string, repoName
3838
if err != nil {
3939
return nil, err
4040
}
41+
if res.StatusCode != 200 {
42+
return nil, fmt.Errorf("Failed to request url: %v, Please make sure repository exits", uri)
43+
}
4144
defer res.Body.Close()
4245
body, err := ioutil.ReadAll(res.Body)
4346
if err != nil {
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/po3rin/github_link_creator/lib/env"
10+
l "github.com/po3rin/github_link_creator/lib/logger"
1011
"github.com/po3rin/github_link_creator/pipeline"
1112

1213
"github.com/gin-gonic/gin"
@@ -24,17 +25,30 @@ func (h *Handler) GetCode(c *gin.Context) {
2425

2526
img, err := pipeline.ProcessingImg(ctx, h.Repo, userName, repoName)
2627
if err != nil {
27-
c.JSON(http.StatusInternalServerError, err.Error())
28+
c.JSON(http.StatusInternalServerError, ErrorResponse{
29+
Message: err.Error(),
30+
DocumentationURL: documentationURL,
31+
})
32+
l.Error(err)
33+
doneCh <- struct{}{}
2834
return
2935
}
3036
location, err := h.Repo.UploadImg(img, userName+"/"+repoName)
3137
if err != nil {
32-
c.JSON(http.StatusInternalServerError, err.Error())
38+
c.JSON(http.StatusInternalServerError, ErrorResponse{
39+
Message: err.Error(),
40+
DocumentationURL: documentationURL,
41+
})
42+
l.Error(err)
43+
doneCh <- struct{}{}
3344
return
3445
}
35-
result := fmt.Sprintf(`<a href="https://github.com/%v/%v"><img src="%v" width="460px"></a>`, userName, repoName, location)
36-
c.JSON(http.StatusOK, gin.H{
37-
"value": result,
46+
url := fmt.Sprintf("https://github.com/%v/%v", userName, repoName)
47+
result := fmt.Sprintf(`<a href="%v"><img src="%v" width="460px"></a>`, url, location)
48+
c.JSON(http.StatusOK, Response{
49+
Value: result,
50+
RepositoryURL: url,
51+
CardURL: location,
3852
})
3953
doneCh <- struct{}{}
4054
}()
@@ -43,8 +57,10 @@ func (h *Handler) GetCode(c *gin.Context) {
4357
case <-doneCh:
4458
return
4559
case <-ctx.Done():
60+
msg := fmt.Sprintf("Processing timed out in %d seconds", env.Timeout)
61+
l.Error(msg)
4662
c.JSON(http.StatusRequestTimeout, gin.H{
47-
"message": fmt.Sprintf("Processing timed out in %d seconds", env.Timeout),
63+
"message": msg,
4864
})
4965
}
5066
}

handler/health.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/po3rin/github_link_creator/lib/env"
10+
l "github.com/po3rin/github_link_creator/lib/logger"
1011

1112
"github.com/gin-gonic/gin"
1213
)
@@ -28,8 +29,10 @@ func (h *Handler) HealthCheck(c *gin.Context) {
2829
case <-doneCh:
2930
return
3031
case <-ctx.Done():
32+
msg := fmt.Sprintf("Processing timed out in %d seconds", env.Timeout)
33+
l.Error(msg)
3134
c.JSON(http.StatusRequestTimeout, gin.H{
32-
"message": fmt.Sprintf("Processing timed out in %d seconds", env.Timeout),
35+
"message": msg,
3336
})
3437
}
3538
}

handler/response.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package handler
2+
3+
var documentationURL = "https://github.com/po3rin/github_link_creator"
4+
5+
// Response is common response format.
6+
type Response struct {
7+
Value string `json:"value"`
8+
CardURL string `json:"card_url"`
9+
RepositoryURL string `json:"repository_url"`
10+
}
11+
12+
// ErrorResponse is common errror response format.
13+
type ErrorResponse struct {
14+
Message string `json:"message"`
15+
DocumentationURL string `json:"documentaion_url"`
16+
}

0 commit comments

Comments
 (0)