Skip to content

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

options/locale/locale_en-US.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ step2 = Step 2:
112112

113113
error = Error
114114
error404 = The page you are trying to reach either <strong>does not exist</strong> or <strong>you are not authorized</strong> to view it.
115+
go_back = Go Back
115116

116117
never = Never
117118
unknown = Unknown
@@ -1014,6 +1015,10 @@ blame.ignore_revs = Ignoring revisions in <a href="%s">.git-blame-ignore-revs</a
10141015
blame.ignore_revs.failed = Failed to ignore revisions in <a href="%s">.git-blame-ignore-revs</a>.
10151016
author_search_tooltip = Shows a maximum of 30 users
10161017
1018+
tree_path_not_found_commit = Path %[1]s doesn't exist in commit %[2]s
1019+
tree_path_not_found_branch = Path %[1]s doesn't exist in branch %[2]s
1020+
tree_path_not_found_tag = Path %[1]s doesn't exist in tag %[2]s
1021+
10171022
transfer.accept = Accept Transfer
10181023
transfer.accept_desc = Transfer to "%s"
10191024
transfer.reject = Reject Transfer

routers/web/repo/blame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func RefBlame(ctx *context.Context) {
7070
// Get current entry user currently looking at.
7171
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
7272
if err != nil {
73-
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
73+
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
7474
return
7575
}
7676

routers/web/repo/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
123123
if !isNewFile {
124124
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
125125
if err != nil {
126-
ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
126+
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
127127
return
128128
}
129129

routers/web/repo/helper.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package repo
55

66
import (
7+
"net/url"
78
"sort"
89

910
"code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/git"
1013
)
1114

1215
func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
@@ -20,3 +23,22 @@ func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
2023
}
2124
return users
2225
}
26+
27+
func HandleGitError(ctx *context.Context, msg string, err error) {
28+
if git.IsErrNotExist(err) {
29+
refType := ""
30+
switch {
31+
case ctx.Repo.IsViewBranch:
32+
refType = "branch"
33+
case ctx.Repo.IsViewTag:
34+
refType = "tag"
35+
case ctx.Repo.IsViewCommit:
36+
refType = "commit"
37+
}
38+
ctx.Data["NotFoundPrompt"] = ctx.Locale.Tr("repo.tree_path_not_found_"+refType, ctx.Repo.TreePath, url.PathEscape(ctx.Repo.RefName))
39+
ctx.Data["NotFoundGoBackURL"] = ctx.Repo.RepoLink + "/src/" + refType + "/" + url.PathEscape(ctx.Repo.RefName)
40+
ctx.NotFound(msg, err)
41+
} else {
42+
ctx.ServerError(msg, err)
43+
}
44+
}

routers/web/repo/view.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) {
692692
}
693693
tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
694694
if err != nil {
695-
ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
695+
HandleGitError(ctx, "Repo.Commit.SubTree", err)
696696
return
697697
}
698698
allEntries, err := tree.ListEntries()
@@ -783,7 +783,7 @@ func LastCommit(ctx *context.Context) {
783783
func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entries {
784784
tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
785785
if err != nil {
786-
ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
786+
HandleGitError(ctx, "Repo.Commit.SubTree", err)
787787
return nil
788788
}
789789

@@ -792,12 +792,12 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
792792
// Get current entry user currently looking at.
793793
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
794794
if err != nil {
795-
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
795+
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
796796
return nil
797797
}
798798

799799
if !entry.IsDir() {
800-
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
800+
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
801801
return nil
802802
}
803803

@@ -963,7 +963,7 @@ func renderCode(ctx *context.Context) {
963963
// Get current entry user currently looking at.
964964
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
965965
if err != nil {
966-
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
966+
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
967967
return
968968
}
969969

templates/status/404.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
{{if .IsRepo}}{{template "repo/header" .}}{{end}}
44
<div class="ui container center">
55
<p style="margin-top: 100px"><img src="{{AssetUrlPrefix}}/img/404.png" alt="404"></p>
6+
<p>{{if .NotFoundPrompt}}{{.NotFoundPrompt}}{{else}}{{ctx.Locale.Tr "error404" | Safe}}{{end}}</p>
7+
{{if .NotFoundGoBackURL}}<a class="ui button green" href="{{.NotFoundGoBackURL}}">{{ctx.Locale.Tr "go_back"}}</a>{{end}}
8+
69
<div class="divider"></div>
710
<br>
8-
<p>{{ctx.Locale.Tr "error404" | Safe}}</p>
911
{{if .ShowFooterVersion}}<p>{{ctx.Locale.Tr "admin.config.app_ver"}}: {{AppVer}}</p>{{end}}
1012
</div>
1113
</div>

0 commit comments

Comments
 (0)