Updates to API 404 responses (#6077)

This commit is contained in:
John Olheiser 2019-03-18 21:29:43 -05:00 committed by techknowlogick
parent d10a668ffc
commit cac9e6e760
30 changed files with 120 additions and 91 deletions

View file

@ -1,4 +1,5 @@
// Copyright 2016 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
@ -6,6 +7,8 @@ package context
import (
"fmt"
"net/url"
"path"
"strings"
"github.com/go-macaron/csrf"
@ -140,3 +143,29 @@ func ReferencesGitRepo() macaron.Handler {
}
}
}
// NotFound handles 404s for APIContext
// String will replace message, errors will be added to a slice
func (ctx *APIContext) NotFound(objs ...interface{}) {
var message = "Not Found"
var errors []string
for _, obj := range objs {
if err, ok := obj.(error); ok {
errors = append(errors, err.Error())
} else {
message = obj.(string)
}
}
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{}{
"message": message,
"documentation_url": u.String(),
"errors": errors,
})
}