Merge pull request #8246 from ninjadq/fix_chart_museum_500_error

Fix: Internal server error with messy code when chartmuseum not work
This commit is contained in:
Wang Yan 2019-07-22 11:07:55 +08:00 committed by GitHub
commit 834e604ec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 28 deletions

View File

@ -1,16 +1,16 @@
package chartserver package chartserver
import ( import (
"errors"
"fmt" "fmt"
commonhttp "github.com/goharbor/harbor/src/common/http"
hlog "github.com/goharbor/harbor/src/common/utils/log"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
commonhttp "github.com/goharbor/harbor/src/common/http"
"github.com/pkg/errors"
) )
const ( const (
@ -49,11 +49,13 @@ func NewChartClient(credential *Credential) *ChartClient { // Create http client
func (cc *ChartClient) GetContent(addr string) ([]byte, error) { func (cc *ChartClient) GetContent(addr string) ([]byte, error) {
response, err := cc.sendRequest(addr, http.MethodGet, nil) response, err := cc.sendRequest(addr, http.MethodGet, nil)
if err != nil { if err != nil {
err = errors.Wrap(err, "get content failed")
return nil, err return nil, err
} }
content, err := ioutil.ReadAll(response.Body) content, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
err = errors.Wrap(err, "Read response body error")
return nil, err return nil, err
} }
defer response.Body.Close() defer response.Body.Close()
@ -61,6 +63,7 @@ func (cc *ChartClient) GetContent(addr string) ([]byte, error) {
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
text, err := extractError(content) text, err := extractError(content)
if err != nil { if err != nil {
err = errors.Wrap(err, "Extract content error failed")
return nil, err return nil, err
} }
return nil, &commonhttp.Error{ return nil, &commonhttp.Error{
@ -106,7 +109,8 @@ func (cc *ChartClient) sendRequest(addr string, method string, body io.Reader) (
fullURI, err := url.Parse(addr) fullURI, err := url.Parse(addr)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid url: %s", err.Error()) err = errors.Wrap(err, "Invalid url")
return nil, err
} }
request, err := http.NewRequest(method, addr, body) request, err := http.NewRequest(method, addr, body)
@ -121,7 +125,7 @@ func (cc *ChartClient) sendRequest(addr string, method string, body io.Reader) (
response, err := cc.httpClient.Do(request) response, err := cc.httpClient.Do(request)
if err != nil { if err != nil {
hlog.Errorf("%s '%s' failed with error: %s", method, fullURI.Path, err) err = errors.Wrap(err, fmt.Sprintf("send request %s %s failed", method, fullURI.Path))
return nil, err return nil, err
} }

View File

@ -2,19 +2,17 @@ package chartserver
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/replication" "github.com/goharbor/harbor/src/replication"
rep_event "github.com/goharbor/harbor/src/replication/event" rep_event "github.com/goharbor/harbor/src/replication/event"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/pkg/errors"
helm_repo "k8s.io/helm/pkg/repo" helm_repo "k8s.io/helm/pkg/repo"
"os"
"github.com/goharbor/harbor/src/common/utils/log"
) )
// ListCharts gets the chart list under the namespace // ListCharts gets the chart list under the namespace

View File

@ -20,12 +20,11 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/astaxie/beego"
"github.com/astaxie/beego/validation" "github.com/astaxie/beego/validation"
commonhttp "github.com/goharbor/harbor/src/common/http" commonhttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
"github.com/pkg/errors"
"errors"
"github.com/astaxie/beego"
) )
const ( const (

View File

@ -15,9 +15,9 @@
package api package api
import ( import (
"errors"
"net/http" "net/http"
"errors"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/goharbor/harbor/src/common/api" "github.com/goharbor/harbor/src/common/api"
"github.com/goharbor/harbor/src/common/security" "github.com/goharbor/harbor/src/common/security"

View File

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"strconv"
"time"
"github.com/goharbor/harbor/src/common" "github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/common/dao"
@ -27,10 +29,7 @@ import (
errutil "github.com/goharbor/harbor/src/common/utils/error" errutil "github.com/goharbor/harbor/src/common/utils/error"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config" "github.com/goharbor/harbor/src/core/config"
"github.com/pkg/errors"
"errors"
"strconv"
"time"
) )
type deletableResp struct { type deletableResp struct {
@ -232,7 +231,10 @@ func (p *ProjectAPI) Get() {
return return
} }
p.populateProperties(p.project) err := p.populateProperties(p.project)
if err != nil {
log.Errorf("populate project poroperties failed with : %+v", err)
}
p.Data["json"] = p.project p.Data["json"] = p.project
p.ServeJSON() p.ServeJSON()
@ -402,15 +404,17 @@ func (p *ProjectAPI) List() {
} }
for _, project := range result.Projects { for _, project := range result.Projects {
p.populateProperties(project) err = p.populateProperties(project)
if err != nil {
log.Errorf("populate project properties failed %v", err)
}
} }
p.SetPaginationHeader(result.Total, page, size) p.SetPaginationHeader(result.Total, page, size)
p.Data["json"] = result.Projects p.Data["json"] = result.Projects
p.ServeJSON() p.ServeJSON()
} }
func (p *ProjectAPI) populateProperties(project *models.Project) { func (p *ProjectAPI) populateProperties(project *models.Project) error {
if p.SecurityCtx.IsAuthenticated() { if p.SecurityCtx.IsAuthenticated() {
roles := p.SecurityCtx.GetProjectRoles(project.ProjectID) roles := p.SecurityCtx.GetProjectRoles(project.ProjectID)
if len(roles) != 0 { if len(roles) != 0 {
@ -427,9 +431,8 @@ func (p *ProjectAPI) populateProperties(project *models.Project) {
ProjectIDs: []int64{project.ProjectID}, ProjectIDs: []int64{project.ProjectID},
}) })
if err != nil { if err != nil {
log.Errorf("failed to get total of repositories of project %d: %v", project.ProjectID, err) err = errors.Wrap(err, fmt.Sprintf("get repo count of project %d failed", project.ProjectID))
p.SendInternalServerError(errors.New("")) return err
return
} }
project.RepoCount = total project.RepoCount = total
@ -438,13 +441,13 @@ func (p *ProjectAPI) populateProperties(project *models.Project) {
if config.WithChartMuseum() { if config.WithChartMuseum() {
count, err := chartController.GetCountOfCharts([]string{project.Name}) count, err := chartController.GetCountOfCharts([]string{project.Name})
if err != nil { if err != nil {
log.Errorf("Failed to get total of charts under project %s: %v", project.Name, err) err = errors.Wrap(err, fmt.Sprintf("get chart count of project %d failed", project.ProjectID))
p.SendInternalServerError(errors.New("")) return err
return
} }
project.ChartCount = count project.ChartCount = count
} }
return nil
} }
// Put ... // Put ...