Handle invalid issues (#18111)

* Handle invalid issues

- When you hover over a issue reference, and the issue doesn't exist, it
will just hang on the loading animation.
- This patch fixes that by showing them the pop-up with a "Error
occured" message.

* Add I18N

* refactor

* fix comment for lint

* fix unit test for i18n

* fix unit test for i18n

* add comments

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Gusted 2021-12-28 13:28:27 +00:00 committed by GitHub
parent d2fac636d1
commit e4e3df6c66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 23 deletions

View file

@ -30,6 +30,11 @@ type APIContext struct {
Org *APIOrganization
}
// Currently, we have the following common fields in error response:
// * message: the message for end users (it shouldn't be used for error type detection)
// if we need to indicate some errors, we should introduce some new fields like ErrorCode or ErrorType
// * url: the swagger document URL
// APIError is error format response
// swagger:response error
type APIError struct {
@ -47,8 +52,8 @@ type APIValidationError struct {
// APIInvalidTopicsError is error format response to invalid topics
// swagger:response invalidTopicsError
type APIInvalidTopicsError struct {
Topics []string `json:"invalidTopics"`
Message string `json:"message"`
Message string `json:"message"`
InvalidTopics []string `json:"invalidTopics"`
}
//APIEmpty is an empty response
@ -122,9 +127,9 @@ func (ctx *APIContext) InternalServerError(err error) {
})
}
var (
apiContextKey interface{} = "default_api_context"
)
type apiContextKeyType struct{}
var apiContextKey = apiContextKeyType{}
// WithAPIContext set up api context in request
func WithAPIContext(req *http.Request, ctx *APIContext) *http.Request {
@ -351,7 +356,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.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 message = ctx.Tr("error.not_found")
var errors []string
for _, obj := range objs {
// Ignore nil
@ -367,9 +372,9 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
}
ctx.JSON(http.StatusNotFound, map[string]interface{}{
"message": message,
"documentation_url": setting.API.SwaggerURL,
"errors": errors,
"message": message,
"url": setting.API.SwaggerURL,
"errors": errors,
})
}