Skip to content

Commit 01f2dfc

Browse files
committed
refactor
1 parent cc7658c commit 01f2dfc

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

modules/context/api.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type APIContext struct {
3131
}
3232

3333
// APIError is error format response
34+
// * message: the message for end users (it shouldn't be used for error type detection)
35+
// if we need to indicate some errors, we should introduce some new fields like ErrorCode or ErrorType
36+
// * url: the swagger document URL
3437
// swagger:response error
3538
type APIError struct {
3639
Message string `json:"message"`
@@ -47,8 +50,8 @@ type APIValidationError struct {
4750
// APIInvalidTopicsError is error format response to invalid topics
4851
// swagger:response invalidTopicsError
4952
type APIInvalidTopicsError struct {
50-
Topics []string `json:"invalidTopics"`
51-
Message string `json:"message"`
53+
Message string `json:"message"`
54+
InvalidTopics []string `json:"invalidTopics"`
5255
}
5356

5457
//APIEmpty is an empty response
@@ -122,9 +125,9 @@ func (ctx *APIContext) InternalServerError(err error) {
122125
})
123126
}
124127

125-
var (
126-
apiContextKey interface{} = "default_api_context"
127-
)
128+
type apiContextKeyType struct{}
129+
130+
var apiContextKey = apiContextKeyType{}
128131

129132
// WithAPIContext set up api context in request
130133
func WithAPIContext(req *http.Request, ctx *APIContext) *http.Request {
@@ -351,7 +354,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
351354
// NotFound handles 404s for APIContext
352355
// String will replace message, errors will be added to a slice
353356
func (ctx *APIContext) NotFound(objs ...interface{}) {
354-
var message = "Not Found"
357+
var message = ctx.Tr("error.not_found")
355358
var errors []string
356359
for _, obj := range objs {
357360
// Ignore nil
@@ -367,9 +370,9 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
367370
}
368371

369372
ctx.JSON(http.StatusNotFound, map[string]interface{}{
370-
"message": message,
371-
"documentation_url": setting.API.SwaggerURL,
372-
"errors": errors,
373+
"message": message,
374+
"url": setting.API.SwaggerURL,
375+
"errors": errors,
373376
})
374377
}
375378

options/locale/locale_en-US.ini

+5-4
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ error404 = The page you are trying to reach either <strong>does not exist</stron
104104
never = Never
105105

106106
[error]
107-
occurred = An error has occurred
108-
report_message = If you are sure this is a Gitea bug, please search for issue on <a href="https://github.com/go-gitea/gitea/issues">GitHub</a> and open new issue if necessary.
107+
occurred = An error occurred
108+
report_message = If you are sure this is a Gitea bug, please search for issues on <a href="https://github.com/go-gitea/gitea/issues" target="_blank">GitHub</a> or open a new issue if necessary.
109109
missing_csrf = Bad Request: no CSRF token present
110-
invalid_csrf = Bad Request: Invalid CSRF token
111-
issue_not_found = The referenced issue couldn't be found.
110+
invalid_csrf = Bad Request: invalid CSRF token
111+
not_found = The target couldn't be found.
112+
network_error = Network error
112113
113114
[startpage]
114115
app_desc = A painless, self-hosted Git service

templates/base/head.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
copy_success: '{{.i18n.Tr "copy_success"}}',
5151
copy_error: '{{.i18n.Tr "copy_error"}}',
5252
error_occurred: '{{.i18n.Tr "error.occurred"}}',
53-
issue_not_found: '{{.i18n.Tr "error.issue_not_found"}}',
54-
}
53+
network_error: '{{.i18n.Tr "error.network_error"}}',
54+
},
5555
};
5656
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}}
5757
window.config.pageData = window.config.pageData || {};

web_src/js/components/ContextPopup.vue

+18-26
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
</div>
1818
</div>
1919
<div v-if="!loading && issue === null">
20-
<p><small>{{ errorTitle }}</small></p>
21-
<p>{{ errorBody }}</p>
20+
<p><small>{{ i18nErrorOccurred }}</small></p>
21+
<p>{{ i18nErrorMessage }}</p>
2222
</div>
2323
</div>
2424
</template>
2525

2626
<script>
2727
import {SvgIcon} from '../svg.js';
2828
29-
const {appSubUrl} = window.config;
30-
const {issue_not_found, error_occurred} = window.config.i18n;
29+
const {appSubUrl, i18n} = window.config;
3130
3231
// NOTE: see models/issue_label.go for similar implementation
3332
const srgbToLinear = (color) => {
@@ -54,18 +53,12 @@ export default {
5453
5554
data: () => ({
5655
loading: false,
57-
issue: null
56+
issue: null,
57+
i18nErrorOccurred: i18n.error_occurred,
58+
i18nErrorMessage: null,
5859
}),
5960
6061
computed: {
61-
errorTitle() {
62-
return error_occurred;
63-
},
64-
65-
errorBody() {
66-
return issue_not_found;
67-
},
68-
6962
createdAt() {
7063
return new Date(this.issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
7164
},
@@ -125,21 +118,20 @@ export default {
125118
methods: {
126119
load(data, callback) {
127120
this.loading = true;
128-
$.get(`${appSubUrl}/api/v1/repos/${data.owner}/${data.repo}/issues/${data.index}`, (issue) => {
121+
this.i18nErrorMessage = null;
122+
$.get(`${appSubUrl}/api/v1/repos/${data.owner}/${data.repo}/issues/${data.index}`).done((issue) => {
129123
this.issue = issue;
124+
}).fail((jqXHR) => {
125+
if (jqXHR.responseJSON && jqXHR.responseJSON.message) {
126+
this.i18nErrorMessage = jqXHR.responseJSON.message;
127+
} else {
128+
this.i18nErrorMessage = i18n.network_error;
129+
}
130+
}).always(() => {
130131
this.loading = false;
131-
this.$nextTick(() => {
132-
if (callback) {
133-
callback();
134-
}
135-
});
136-
}).fail(() => {
137-
this.loading = false;
138-
this.$nextTick(() => {
139-
if (callback) {
140-
callback();
141-
}
142-
});
132+
if (callback) {
133+
this.$nextTick(callback);
134+
}
143135
});
144136
}
145137
}

0 commit comments

Comments
 (0)