Browse Source

Fix 500 when repo has invalid .editorconfig

Creating a notice instead
Andrey Nering 3 years ago
parent
commit
984fa8d83b
4 changed files with 31 additions and 16 deletions
  1. 2 4
      routers/repo/commit.go
  2. 23 0
      routers/repo/middlewares.go
  3. 4 8
      routers/repo/pull.go
  4. 2 4
      routers/repo/view.go

+ 2 - 4
routers/repo/commit.go

@@ -179,12 +179,10 @@ func Diff(ctx *context.Context) {
179 179
 		}
180 180
 	}
181 181
 
182
-	ec, err := ctx.Repo.GetEditorconfig()
183
-	if err != nil && !git.IsErrNotExist(err) {
184
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
182
+	setEditorconfigIfExists(ctx)
183
+	if ctx.Written() {
185 184
 		return
186 185
 	}
187
-	ctx.Data["Editorconfig"] = ec
188 186
 
189 187
 	ctx.Data["CommitID"] = commitID
190 188
 	ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"

+ 23 - 0
routers/repo/middlewares.go

@@ -0,0 +1,23 @@
1
+package repo
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"github.com/go-gitea/gitea/models"
7
+	"github.com/go-gitea/gitea/modules/context"
8
+	"github.com/gogits/git-module"
9
+)
10
+
11
+func setEditorconfigIfExists(ctx *context.Context) {
12
+	ec, err := ctx.Repo.GetEditorconfig()
13
+
14
+	if err != nil && !git.IsErrNotExist(err) {
15
+		description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)
16
+		if err := models.CreateRepositoryNotice(description); err != nil {
17
+			ctx.Handle(500, "ErrCreatingReporitoryNotice", err)
18
+		}
19
+		return
20
+	}
21
+
22
+	ctx.Data["Editorconfig"] = ec
23
+}

+ 4 - 8
routers/repo/pull.go

@@ -368,12 +368,10 @@ func ViewPullFiles(ctx *context.Context) {
368 368
 		return
369 369
 	}
370 370
 
371
-	ec, err := ctx.Repo.GetEditorconfig()
372
-	if err != nil && !git.IsErrNotExist(err) {
373
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
371
+	setEditorconfigIfExists(ctx)
372
+	if ctx.Written() {
374 373
 		return
375 374
 	}
376
-	ctx.Data["Editorconfig"] = ec
377 375
 
378 376
 	headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
379 377
 	ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
@@ -627,12 +625,10 @@ func CompareAndPullRequest(ctx *context.Context) {
627 625
 		}
628 626
 	}
629 627
 
630
-	ec, err := ctx.Repo.GetEditorconfig()
631
-	if err != nil && !git.IsErrNotExist(err) {
632
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
628
+	setEditorconfigIfExists(ctx)
629
+	if ctx.Written() {
633 630
 		return
634 631
 	}
635
-	ctx.Data["Editorconfig"] = ec
636 632
 
637 633
 	ctx.HTML(200, COMPARE_PULL)
638 634
 }

+ 2 - 4
routers/repo/view.go

@@ -245,12 +245,10 @@ func Home(ctx *context.Context) {
245 245
 		return
246 246
 	}
247 247
 
248
-	ec, err := ctx.Repo.GetEditorconfig()
249
-	if err != nil && !git.IsErrNotExist(err) {
250
-		ctx.Handle(500, "Repo.GetEditorconfig", err)
248
+	setEditorconfigIfExists(ctx)
249
+	if ctx.Written() {
251 250
 		return
252 251
 	}
253
-	ctx.Data["Editorconfig"] = ec
254 252
 
255 253
 	var treeNames []string
256 254
 	paths := make([]string, 0, 5)