From 202c3aa961e952c4f54e441074b52808bcc0bea7 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sun, 26 Dec 2021 22:15:26 +0100 Subject: [PATCH 1/7] 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. --- web_src/js/components/ContextPopup.vue | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/web_src/js/components/ContextPopup.vue b/web_src/js/components/ContextPopup.vue index efaa7be89e126..7f600daaff03b 100644 --- a/web_src/js/components/ContextPopup.vue +++ b/web_src/js/components/ContextPopup.vue @@ -16,6 +16,10 @@ +
+

Error occured

+

The referenced issue couldn't be found.

+
@@ -120,6 +124,13 @@ export default { callback(); } }); + }).fail(() => { + this.loading = false; + this.$nextTick(() => { + if (callback) { + callback(); + } + }); }); } } From 3b87a0d015a28af2eeedabbcb4a7b7fd41773a22 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 27 Dec 2021 00:38:53 +0100 Subject: [PATCH 2/7] Add I18N --- options/locale/locale_en-US.ini | 1 + templates/base/head.tmpl | 2 ++ web_src/js/components/ContextPopup.vue | 13 +++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 5248f98069585..7e7c61c3ed4f1 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -108,6 +108,7 @@ occurred = An error has occurred report_message = If you are sure this is a Gitea bug, please search for issue on GitHub and open new issue if necessary. missing_csrf = Bad Request: no CSRF token present invalid_csrf = Bad Request: Invalid CSRF token +issue_not_found = The referenced issue couldn't be found. [startpage] app_desc = A painless, self-hosted Git service diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index e9c2d512dfbd4..b42e83d0ccfe0 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -49,6 +49,8 @@ i18n: { copy_success: '{{.i18n.Tr "copy_success"}}', copy_error: '{{.i18n.Tr "copy_error"}}', + error_occurred: '{{.i18n.Tr "error.occurred"}}', + issue_not_found: '{{.i18n.Tr "error.issue_not_found"}}', } }; {{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}} diff --git a/web_src/js/components/ContextPopup.vue b/web_src/js/components/ContextPopup.vue index 7f600daaff03b..6be2b44f1c836 100644 --- a/web_src/js/components/ContextPopup.vue +++ b/web_src/js/components/ContextPopup.vue @@ -17,8 +17,8 @@
-

Error occured

-

The referenced issue couldn't be found.

+

{{ errorTitle }}

+

{{ errorBody }}

@@ -27,6 +27,7 @@ import {SvgIcon} from '../svg.js'; const {appSubUrl} = window.config; +const {issue_not_found, error_occurred} = window.config.i18n; // NOTE: see models/issue_label.go for similar implementation const srgbToLinear = (color) => { @@ -57,6 +58,14 @@ export default { }), computed: { + errorTitle() { + return error_occurred; + }, + + errorBody() { + return issue_not_found; + }, + createdAt() { return new Date(this.issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}); }, From 01f2dfc26236d2066c2901b5fb0845db04652643 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 28 Dec 2021 13:39:04 +0800 Subject: [PATCH 3/7] refactor --- modules/context/api.go | 21 ++++++------ options/locale/locale_en-US.ini | 9 +++--- templates/base/head.tmpl | 4 +-- web_src/js/components/ContextPopup.vue | 44 +++++++++++--------------- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 574d7c424883e..035842ee5fe1c 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -31,6 +31,9 @@ type APIContext struct { } // APIError is error format 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 // swagger:response error type APIError struct { Message string `json:"message"` @@ -47,8 +50,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 +125,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 +354,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 +370,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, }) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 7e7c61c3ed4f1..9164d5ffdceda 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -104,11 +104,12 @@ error404 = The page you are trying to reach either does not existGitHub and open new issue if necessary. +occurred = An error occurred +report_message = If you are sure this is a Gitea bug, please search for issues on GitHub or open a new issue if necessary. missing_csrf = Bad Request: no CSRF token present -invalid_csrf = Bad Request: Invalid CSRF token -issue_not_found = The referenced issue couldn't be found. +invalid_csrf = Bad Request: invalid CSRF token +not_found = The target couldn't be found. +network_error = Network error [startpage] app_desc = A painless, self-hosted Git service diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index b42e83d0ccfe0..af4a66b7b2302 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -50,8 +50,8 @@ copy_success: '{{.i18n.Tr "copy_success"}}', copy_error: '{{.i18n.Tr "copy_error"}}', error_occurred: '{{.i18n.Tr "error.occurred"}}', - issue_not_found: '{{.i18n.Tr "error.issue_not_found"}}', - } + network_error: '{{.i18n.Tr "error.network_error"}}', + }, }; {{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}} window.config.pageData = window.config.pageData || {}; diff --git a/web_src/js/components/ContextPopup.vue b/web_src/js/components/ContextPopup.vue index 6be2b44f1c836..c002a3d0667ea 100644 --- a/web_src/js/components/ContextPopup.vue +++ b/web_src/js/components/ContextPopup.vue @@ -17,8 +17,8 @@
-

{{ errorTitle }}

-

{{ errorBody }}

+

{{ i18nErrorOccurred }}

+

{{ i18nErrorMessage }}

@@ -26,8 +26,7 @@