remove "panic()" as much as posible from code (#682)

This commit is contained in:
6543 2022-01-09 23:27:30 +01:00 committed by GitHub
parent 6eecfa4b88
commit 5a120db69d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View file

@ -111,7 +111,11 @@ func (g *Gitlab) Login(ctx context.Context, res http.ResponseWriter, req *http.R
// get the OAuth code // get the OAuth code
code := req.FormValue("code") code := req.FormValue("code")
if len(code) == 0 { if len(code) == 0 {
http.Redirect(res, req, config.AuthCodeURL("drone"), http.StatusSeeOther) authCodeURL, err := config.AuthCodeURL("drone")
if err != nil {
return nil, fmt.Errorf("authCodeURL error: %v", err)
}
http.Redirect(res, req, authCodeURL, http.StatusSeeOther)
return nil, nil return nil, nil
} }

View file

@ -29,10 +29,10 @@ import (
"github.com/woodpecker-ci/woodpecker/server/remote/gitlab/testdata" "github.com/woodpecker-ci/woodpecker/server/remote/gitlab/testdata"
) )
func load(config string) *Gitlab { func load(t *testing.T, config string) *Gitlab {
_url, err := url.Parse(config) _url, err := url.Parse(config)
if err != nil { if err != nil {
panic(err) t.FailNow()
} }
params := _url.Query() params := _url.Query()
_url.RawQuery = "" _url.RawQuery = ""
@ -57,7 +57,7 @@ func Test_Gitlab(t *testing.T) {
env := server.URL + "?client_id=test&client_secret=test" env := server.URL + "?client_id=test&client_secret=test"
client := load(env) client := load(t, env)
user := model.User{ user := model.User{
Login: "test_user", Login: "test_user",

View file

@ -213,13 +213,13 @@ func (t *Transport) transport() http.RoundTripper {
// AuthCodeURL returns a URL that the end-user should be redirected to, // AuthCodeURL returns a URL that the end-user should be redirected to,
// so that they may obtain an authorization code. // so that they may obtain an authorization code.
func (c *Config) AuthCodeURL(state string) string { func (c *Config) AuthCodeURL(state string) (string, error) {
_url, err := url.Parse(c.AuthURL) _url, err := url.Parse(c.AuthURL)
if err != nil { if err != nil {
panic("AuthURL malformed: " + err.Error()) return "", fmt.Errorf("AuthURL malformed: %v", err)
} }
if err := _url.Query().Get("error"); err != "" { if err := _url.Query().Get("error"); err != "" {
panic("AuthURL contains error: " + err) return "", fmt.Errorf("AuthURL contains error: %v", err)
} }
q := url.Values{ q := url.Values{
"response_type": {"code"}, "response_type": {"code"},
@ -235,7 +235,7 @@ func (c *Config) AuthCodeURL(state string) string {
} else { } else {
_url.RawQuery += "&" + q _url.RawQuery += "&" + q
} }
return _url.String() return _url.String(), nil
} }
func condVal(v string) []string { func condVal(v string) []string {