Skip to content

Commit 8f0182c

Browse files
jolheisertechknowlogick
authored andcommitted
API error cleanup (#7186)
1 parent 744fd6a commit 8f0182c

File tree

7 files changed

+16
-25
lines changed

7 files changed

+16
-25
lines changed

integrations/api_repo_file_content_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/modules/base"
1514
"code.gitea.io/gitea/modules/context"
1615
"code.gitea.io/gitea/modules/setting"
1716
api "code.gitea.io/gitea/modules/structs"
@@ -98,7 +97,7 @@ func testAPIGetFileContents(t *testing.T, u *url.URL) {
9897
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
9998
expectedAPIError := context.APIError{
10099
Message: "object does not exist [id: " + branch + ", rel_path: ]",
101-
URL: base.DocURL,
100+
URL: setting.API.SwaggerURL,
102101
}
103102
var apiError context.APIError
104103
DecodeJSON(t, resp, &apiError)

integrations/api_repo_file_create_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16-
"code.gitea.io/gitea/modules/base"
1716
"code.gitea.io/gitea/modules/context"
1817
"code.gitea.io/gitea/modules/git"
1918
"code.gitea.io/gitea/modules/setting"
@@ -160,7 +159,7 @@ func TestAPICreateFile(t *testing.T) {
160159
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
161160
expectedAPIError := context.APIError{
162161
Message: "repository file already exists [path: " + treePath + "]",
163-
URL: base.DocURL,
162+
URL: setting.API.SwaggerURL,
164163
}
165164
var apiError context.APIError
166165
DecodeJSON(t, resp, &apiError)

integrations/api_repo_file_delete_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"testing"
1212

1313
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/modules/base"
1514
"code.gitea.io/gitea/modules/context"
15+
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/gitea/modules/structs"
1717

1818
"github.com/stretchr/testify/assert"
@@ -102,7 +102,7 @@ func TestAPIDeleteFile(t *testing.T) {
102102
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
103103
expectedAPIError := context.APIError{
104104
Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]",
105-
URL: base.DocURL,
105+
URL: setting.API.SwaggerURL,
106106
}
107107
var apiError context.APIError
108108
DecodeJSON(t, resp, &apiError)

integrations/api_repo_file_update_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16-
"code.gitea.io/gitea/modules/base"
1716
"code.gitea.io/gitea/modules/context"
1817
"code.gitea.io/gitea/modules/git"
1918
"code.gitea.io/gitea/modules/setting"
@@ -173,7 +172,7 @@ func TestAPIUpdateFile(t *testing.T) {
173172
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
174173
expectedAPIError := context.APIError{
175174
Message: "sha does not match [given: " + updateFileOptions.SHA + ", expected: " + correctSHA + "]",
176-
URL: base.DocURL,
175+
URL: setting.API.SwaggerURL,
177176
}
178177
var apiError context.APIError
179178
DecodeJSON(t, resp, &apiError)

modules/base/base.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
package base
66

7-
// DocURL api doc url
8-
const DocURL = "https://godoc.org/github.com/go-gitea/go-sdk/gitea"
9-
107
type (
118
// TplName template relative path type
129
TplName string

modules/context/api.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ package context
77

88
import (
99
"fmt"
10-
"net/url"
11-
"path"
1210
"strings"
1311

1412
"github.com/go-macaron/csrf"
1513

1614
"code.gitea.io/gitea/models"
17-
"code.gitea.io/gitea/modules/base"
1815
"code.gitea.io/gitea/modules/git"
1916
"code.gitea.io/gitea/modules/log"
2017
"code.gitea.io/gitea/modules/setting"
@@ -76,7 +73,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
7673

7774
ctx.JSON(status, APIError{
7875
Message: message,
79-
URL: base.DocURL,
76+
URL: setting.API.SwaggerURL,
8077
})
8178
}
8279

@@ -180,15 +177,9 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
180177
}
181178
}
182179

183-
u, err := url.Parse(setting.AppURL)
184-
if err != nil {
185-
ctx.Error(500, "Invalid AppURL", err)
186-
return
187-
}
188-
u.Path = path.Join(u.Path, "api", "swagger")
189180
ctx.JSON(404, map[string]interface{}{
190181
"message": message,
191-
"documentation_url": u.String(),
182+
"documentation_url": setting.API.SwaggerURL,
192183
"errors": errors,
193184
})
194185
}

modules/setting/setting.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,14 @@ var (
297297
// API settings
298298
API = struct {
299299
EnableSwagger bool
300+
SwaggerURL string
300301
MaxResponseItems int
301302
DefaultPagingNum int
302303
DefaultGitTreesPerPage int
303304
DefaultMaxBlobSize int64
304305
}{
305306
EnableSwagger: true,
307+
SwaggerURL: "",
306308
MaxResponseItems: 50,
307309
DefaultPagingNum: 30,
308310
DefaultGitTreesPerPage: 1000,
@@ -581,17 +583,17 @@ func NewContext() {
581583
AppURL = strings.TrimRight(AppURL, "/") + "/"
582584

583585
// Check if has app suburl.
584-
url, err := url.Parse(AppURL)
586+
appURL, err := url.Parse(AppURL)
585587
if err != nil {
586588
log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err)
587589
}
588590
// Suburl should start with '/' and end without '/', such as '/{subpath}'.
589591
// This value is empty if site does not have sub-url.
590-
AppSubURL = strings.TrimSuffix(url.Path, "/")
592+
AppSubURL = strings.TrimSuffix(appURL.Path, "/")
591593
AppSubURLDepth = strings.Count(AppSubURL, "/")
592594
// Check if Domain differs from AppURL domain than update it to AppURL's domain
593595
// TODO: Can be replaced with url.Hostname() when minimal GoLang version is 1.8
594-
urlHostname := strings.SplitN(url.Host, ":", 2)[0]
596+
urlHostname := strings.SplitN(appURL.Host, ":", 2)[0]
595597
if urlHostname != Domain && net.ParseIP(urlHostname) == nil {
596598
Domain = urlHostname
597599
}
@@ -900,6 +902,10 @@ func NewContext() {
900902
log.Fatal("Failed to map Metrics settings: %v", err)
901903
}
902904

905+
u := *appURL
906+
u.Path = path.Join(u.Path, "api", "swagger")
907+
API.SwaggerURL = u.String()
908+
903909
newCron()
904910
newGit()
905911

0 commit comments

Comments
 (0)