API error cleanup (#7186)

release/v1.9
John Olheiser 5 years ago committed by techknowlogick
parent 744fd6a1c8
commit 8f0182c322

@ -11,7 +11,6 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -98,7 +97,7 @@ func testAPIGetFileContents(t *testing.T, u *url.URL) {
resp = session.MakeRequest(t, req, http.StatusInternalServerError) resp = session.MakeRequest(t, req, http.StatusInternalServerError)
expectedAPIError := context.APIError{ expectedAPIError := context.APIError{
Message: "object does not exist [id: " + branch + ", rel_path: ]", Message: "object does not exist [id: " + branch + ", rel_path: ]",
URL: base.DocURL, URL: setting.API.SwaggerURL,
} }
var apiError context.APIError var apiError context.APIError
DecodeJSON(t, resp, &apiError) DecodeJSON(t, resp, &apiError)

@ -13,7 +13,6 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -160,7 +159,7 @@ func TestAPICreateFile(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusInternalServerError) resp = session.MakeRequest(t, req, http.StatusInternalServerError)
expectedAPIError := context.APIError{ expectedAPIError := context.APIError{
Message: "repository file already exists [path: " + treePath + "]", Message: "repository file already exists [path: " + treePath + "]",
URL: base.DocURL, URL: setting.API.SwaggerURL,
} }
var apiError context.APIError var apiError context.APIError
DecodeJSON(t, resp, &apiError) DecodeJSON(t, resp, &apiError)

@ -11,8 +11,8 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -102,7 +102,7 @@ func TestAPIDeleteFile(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusInternalServerError) resp = session.MakeRequest(t, req, http.StatusInternalServerError)
expectedAPIError := context.APIError{ expectedAPIError := context.APIError{
Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]", Message: "sha does not match [given: " + deleteFileOptions.SHA + ", expected: " + correctSHA + "]",
URL: base.DocURL, URL: setting.API.SwaggerURL,
} }
var apiError context.APIError var apiError context.APIError
DecodeJSON(t, resp, &apiError) DecodeJSON(t, resp, &apiError)

@ -13,7 +13,6 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -173,7 +172,7 @@ func TestAPIUpdateFile(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusInternalServerError) resp = session.MakeRequest(t, req, http.StatusInternalServerError)
expectedAPIError := context.APIError{ expectedAPIError := context.APIError{
Message: "sha does not match [given: " + updateFileOptions.SHA + ", expected: " + correctSHA + "]", Message: "sha does not match [given: " + updateFileOptions.SHA + ", expected: " + correctSHA + "]",
URL: base.DocURL, URL: setting.API.SwaggerURL,
} }
var apiError context.APIError var apiError context.APIError
DecodeJSON(t, resp, &apiError) DecodeJSON(t, resp, &apiError)

@ -4,9 +4,6 @@
package base package base
// DocURL api doc url
const DocURL = "https://godoc.org/github.com/go-gitea/go-sdk/gitea"
type ( type (
// TplName template relative path type // TplName template relative path type
TplName string TplName string

@ -7,14 +7,11 @@ package context
import ( import (
"fmt" "fmt"
"net/url"
"path"
"strings" "strings"
"github.com/go-macaron/csrf" "github.com/go-macaron/csrf"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -76,7 +73,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
ctx.JSON(status, APIError{ ctx.JSON(status, APIError{
Message: message, Message: message,
URL: base.DocURL, URL: setting.API.SwaggerURL,
}) })
} }
@ -180,15 +177,9 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
} }
} }
u, err := url.Parse(setting.AppURL)
if err != nil {
ctx.Error(500, "Invalid AppURL", err)
return
}
u.Path = path.Join(u.Path, "api", "swagger")
ctx.JSON(404, map[string]interface{}{ ctx.JSON(404, map[string]interface{}{
"message": message, "message": message,
"documentation_url": u.String(), "documentation_url": setting.API.SwaggerURL,
"errors": errors, "errors": errors,
}) })
} }

@ -297,12 +297,14 @@ var (
// API settings // API settings
API = struct { API = struct {
EnableSwagger bool EnableSwagger bool
SwaggerURL string
MaxResponseItems int MaxResponseItems int
DefaultPagingNum int DefaultPagingNum int
DefaultGitTreesPerPage int DefaultGitTreesPerPage int
DefaultMaxBlobSize int64 DefaultMaxBlobSize int64
}{ }{
EnableSwagger: true, EnableSwagger: true,
SwaggerURL: "",
MaxResponseItems: 50, MaxResponseItems: 50,
DefaultPagingNum: 30, DefaultPagingNum: 30,
DefaultGitTreesPerPage: 1000, DefaultGitTreesPerPage: 1000,
@ -581,17 +583,17 @@ func NewContext() {
AppURL = strings.TrimRight(AppURL, "/") + "/" AppURL = strings.TrimRight(AppURL, "/") + "/"
// Check if has app suburl. // Check if has app suburl.
url, err := url.Parse(AppURL) appURL, err := url.Parse(AppURL)
if err != nil { if err != nil {
log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err) log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err)
} }
// Suburl should start with '/' and end without '/', such as '/{subpath}'. // Suburl should start with '/' and end without '/', such as '/{subpath}'.
// This value is empty if site does not have sub-url. // This value is empty if site does not have sub-url.
AppSubURL = strings.TrimSuffix(url.Path, "/") AppSubURL = strings.TrimSuffix(appURL.Path, "/")
AppSubURLDepth = strings.Count(AppSubURL, "/") AppSubURLDepth = strings.Count(AppSubURL, "/")
// Check if Domain differs from AppURL domain than update it to AppURL's domain // Check if Domain differs from AppURL domain than update it to AppURL's domain
// TODO: Can be replaced with url.Hostname() when minimal GoLang version is 1.8 // TODO: Can be replaced with url.Hostname() when minimal GoLang version is 1.8
urlHostname := strings.SplitN(url.Host, ":", 2)[0] urlHostname := strings.SplitN(appURL.Host, ":", 2)[0]
if urlHostname != Domain && net.ParseIP(urlHostname) == nil { if urlHostname != Domain && net.ParseIP(urlHostname) == nil {
Domain = urlHostname Domain = urlHostname
} }
@ -900,6 +902,10 @@ func NewContext() {
log.Fatal("Failed to map Metrics settings: %v", err) log.Fatal("Failed to map Metrics settings: %v", err)
} }
u := *appURL
u.Path = path.Join(u.Path, "api", "swagger")
API.SwaggerURL = u.String()
newCron() newCron()
newGit() newGit()

Loading…
Cancel
Save