From 984fa8d83b403baf1b72ec0e9e98a6c700aabf9d Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 3 Nov 2016 20:45:16 -0200 Subject: [PATCH] Fix 500 when repo has invalid .editorconfig Creating a notice instead --- routers/repo/commit.go | 6 ++---- routers/repo/middlewares.go | 23 +++++++++++++++++++++++ routers/repo/pull.go | 12 ++++-------- routers/repo/view.go | 6 ++---- 4 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 routers/repo/middlewares.go diff --git a/routers/repo/commit.go b/routers/repo/commit.go index a8e96f0e5..4a4d4b123 100644 --- a/routers/repo/commit.go +++ b/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" diff --git a/routers/repo/middlewares.go b/routers/repo/middlewares.go new file mode 100644 index 000000000..f025765cb --- /dev/null +++ b/routers/repo/middlewares.go @@ -0,0 +1,23 @@ +package repo + +import ( + "fmt" + + "github.com/go-gitea/gitea/models" + "github.com/go-gitea/gitea/modules/context" + "github.com/gogits/git-module" +) + +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 +} diff --git a/routers/repo/pull.go b/routers/repo/pull.go index baa8c1d0b..6dbb2b602 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -368,12 +368,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" @@ -627,12 +625,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) } diff --git a/routers/repo/view.go b/routers/repo/view.go index 3bb551f2e..ee49d4da1 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -245,12 +245,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)