Browse Source

Fix 500 when repo has invalid .editorconfig (#3758)

Creating a notice instead

Fixes #3643
Andrey Nering 8 years ago
parent
commit
1d951cfc49
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) {
 		}
 	}
 
-	ec, err := ctx.Repo.GetEditorconfig()
-	if err != nil && !git.IsErrNotExist(err) {
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
+	setEditorconfigIfExists(ctx)
+	if ctx.Written() {
 		return
 	}
-	ctx.Data["Editorconfig"] = ec
 
 	ctx.Data["CommitID"] = commitID
 	ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"

+ 23 - 0
routers/repo/middlewares.go

@@ -0,0 +1,23 @@
+package repo
+
+import (
+	"fmt"
+
+	"github.com/gogits/git-module"
+	"github.com/gogits/gogs/models"
+	"github.com/gogits/gogs/modules/context"
+)
+
+func setEditorconfigIfExists(ctx *context.Context) {
+	ec, err := ctx.Repo.GetEditorconfig()
+
+	if err != nil && !git.IsErrNotExist(err) {
+		description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)
+		if err := models.CreateRepositoryNotice(description); err != nil {
+			ctx.Handle(500, "ErrCreatingReporitoryNotice", err)
+		}
+		return
+	}
+
+	ctx.Data["Editorconfig"] = ec
+}

+ 4 - 8
routers/repo/pull.go

@@ -357,12 +357,10 @@ func ViewPullFiles(ctx *context.Context) {
 		return
 	}
 
-	ec, err := ctx.Repo.GetEditorconfig()
-	if err != nil && !git.IsErrNotExist(err) {
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
+	setEditorconfigIfExists(ctx)
+	if ctx.Written() {
 		return
 	}
-	ctx.Data["Editorconfig"] = ec
 
 	headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
 	ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
@@ -616,12 +614,10 @@ func CompareAndPullRequest(ctx *context.Context) {
 		}
 	}
 
-	ec, err := ctx.Repo.GetEditorconfig()
-	if err != nil && !git.IsErrNotExist(err) {
-		ctx.Handle(500, "ErrGettingEditorconfig", err)
+	setEditorconfigIfExists(ctx)
+	if ctx.Written() {
 		return
 	}
-	ctx.Data["Editorconfig"] = ec
 
 	ctx.HTML(200, COMPARE_PULL)
 }

+ 2 - 4
routers/repo/view.go

@@ -244,12 +244,10 @@ func Home(ctx *context.Context) {
 		return
 	}
 
-	ec, err := ctx.Repo.GetEditorconfig()
-	if err != nil && !git.IsErrNotExist(err) {
-		ctx.Handle(500, "Repo.GetEditorconfig", err)
+	setEditorconfigIfExists(ctx)
+	if ctx.Written() {
 		return
 	}
-	ctx.Data["Editorconfig"] = ec
 
 	var treeNames []string
 	paths := make([]string, 0, 5)