gitea源码

editor_error.go 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2025 Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. git_model "code.gitea.io/gitea/models/git"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/util"
  11. "code.gitea.io/gitea/routers/utils"
  12. context_service "code.gitea.io/gitea/services/context"
  13. files_service "code.gitea.io/gitea/services/repository/files"
  14. )
  15. func errorAs[T error](v error) (e T, ok bool) {
  16. if errors.As(v, &e) {
  17. return e, true
  18. }
  19. return e, false
  20. }
  21. func editorHandleFileOperationErrorRender(ctx *context_service.Context, message, summary, details string) {
  22. flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
  23. "Message": message,
  24. "Summary": summary,
  25. "Details": utils.SanitizeFlashErrorString(details),
  26. })
  27. if err == nil {
  28. ctx.JSONError(flashError)
  29. } else {
  30. log.Error("RenderToHTML: %v", err)
  31. ctx.JSONError(message + "\n" + summary + "\n" + utils.SanitizeFlashErrorString(details))
  32. }
  33. }
  34. func editorHandleFileOperationError(ctx *context_service.Context, targetBranchName string, err error) {
  35. if errAs := util.ErrorAsLocale(err); errAs != nil {
  36. ctx.JSONError(ctx.Tr(errAs.TrKey, errAs.TrArgs...))
  37. } else if errAs, ok := errorAs[git.ErrNotExist](err); ok {
  38. ctx.JSONError(ctx.Tr("repo.editor.file_modifying_no_longer_exists", errAs.RelPath))
  39. } else if errAs, ok := errorAs[git_model.ErrLFSFileLocked](err); ok {
  40. ctx.JSONError(ctx.Tr("repo.editor.upload_file_is_locked", errAs.Path, errAs.UserName))
  41. } else if errAs, ok := errorAs[files_service.ErrFilenameInvalid](err); ok {
  42. ctx.JSONError(ctx.Tr("repo.editor.filename_is_invalid", errAs.Path))
  43. } else if errAs, ok := errorAs[files_service.ErrFilePathInvalid](err); ok {
  44. switch errAs.Type {
  45. case git.EntryModeSymlink:
  46. ctx.JSONError(ctx.Tr("repo.editor.file_is_a_symlink", errAs.Path))
  47. case git.EntryModeTree:
  48. ctx.JSONError(ctx.Tr("repo.editor.filename_is_a_directory", errAs.Path))
  49. case git.EntryModeBlob:
  50. ctx.JSONError(ctx.Tr("repo.editor.directory_is_a_file", errAs.Path))
  51. default:
  52. ctx.JSONError(ctx.Tr("repo.editor.filename_is_invalid", errAs.Path))
  53. }
  54. } else if errAs, ok := errorAs[files_service.ErrRepoFileAlreadyExists](err); ok {
  55. ctx.JSONError(ctx.Tr("repo.editor.file_already_exists", errAs.Path))
  56. } else if errAs, ok := errorAs[git.ErrBranchNotExist](err); ok {
  57. ctx.JSONError(ctx.Tr("repo.editor.branch_does_not_exist", errAs.Name))
  58. } else if errAs, ok := errorAs[git_model.ErrBranchAlreadyExists](err); ok {
  59. ctx.JSONError(ctx.Tr("repo.editor.branch_already_exists", errAs.BranchName))
  60. } else if files_service.IsErrCommitIDDoesNotMatch(err) {
  61. ctx.JSONError(ctx.Tr("repo.editor.commit_id_not_matching"))
  62. } else if files_service.IsErrCommitIDDoesNotMatch(err) || git.IsErrPushOutOfDate(err) {
  63. ctx.JSONError(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+util.PathEscapeSegments(ctx.Repo.CommitID)+"..."+util.PathEscapeSegments(targetBranchName)))
  64. } else if errAs, ok := errorAs[*git.ErrPushRejected](err); ok {
  65. if errAs.Message == "" {
  66. ctx.JSONError(ctx.Tr("repo.editor.push_rejected_no_message"))
  67. } else {
  68. editorHandleFileOperationErrorRender(ctx, ctx.Locale.TrString("repo.editor.push_rejected"), ctx.Locale.TrString("repo.editor.push_rejected_summary"), errAs.Message)
  69. }
  70. } else if errors.Is(err, util.ErrNotExist) {
  71. ctx.JSONError(ctx.Tr("error.not_found"))
  72. } else {
  73. setting.PanicInDevOrTesting("unclear err %T: %v", err, err)
  74. editorHandleFileOperationErrorRender(ctx, ctx.Locale.TrString("repo.editor.failed_to_commit"), ctx.Locale.TrString("repo.editor.failed_to_commit_summary"), err.Error())
  75. }
  76. }