code.gitea.io/gitea@v1.21.7/models/error.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // Copyright 2019 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package models
     6  
     7  import (
     8  	"fmt"
     9  
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	"code.gitea.io/gitea/modules/git"
    12  	"code.gitea.io/gitea/modules/util"
    13  )
    14  
    15  // ErrUserOwnRepos represents a "UserOwnRepos" kind of error.
    16  type ErrUserOwnRepos struct {
    17  	UID int64
    18  }
    19  
    20  // IsErrUserOwnRepos checks if an error is a ErrUserOwnRepos.
    21  func IsErrUserOwnRepos(err error) bool {
    22  	_, ok := err.(ErrUserOwnRepos)
    23  	return ok
    24  }
    25  
    26  func (err ErrUserOwnRepos) Error() string {
    27  	return fmt.Sprintf("user still has ownership of repositories [uid: %d]", err.UID)
    28  }
    29  
    30  // ErrUserHasOrgs represents a "UserHasOrgs" kind of error.
    31  type ErrUserHasOrgs struct {
    32  	UID int64
    33  }
    34  
    35  // IsErrUserHasOrgs checks if an error is a ErrUserHasOrgs.
    36  func IsErrUserHasOrgs(err error) bool {
    37  	_, ok := err.(ErrUserHasOrgs)
    38  	return ok
    39  }
    40  
    41  func (err ErrUserHasOrgs) Error() string {
    42  	return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID)
    43  }
    44  
    45  // ErrUserOwnPackages notifies that the user (still) owns the packages.
    46  type ErrUserOwnPackages struct {
    47  	UID int64
    48  }
    49  
    50  // IsErrUserOwnPackages checks if an error is an ErrUserOwnPackages.
    51  func IsErrUserOwnPackages(err error) bool {
    52  	_, ok := err.(ErrUserOwnPackages)
    53  	return ok
    54  }
    55  
    56  func (err ErrUserOwnPackages) Error() string {
    57  	return fmt.Sprintf("user still has ownership of packages [uid: %d]", err.UID)
    58  }
    59  
    60  // ErrDeleteLastAdminUser represents a "DeleteLastAdminUser" kind of error.
    61  type ErrDeleteLastAdminUser struct {
    62  	UID int64
    63  }
    64  
    65  // IsErrDeleteLastAdminUser checks if an error is a ErrDeleteLastAdminUser.
    66  func IsErrDeleteLastAdminUser(err error) bool {
    67  	_, ok := err.(ErrDeleteLastAdminUser)
    68  	return ok
    69  }
    70  
    71  func (err ErrDeleteLastAdminUser) Error() string {
    72  	return fmt.Sprintf("can not delete the last admin user [uid: %d]", err.UID)
    73  }
    74  
    75  // ErrNoPendingRepoTransfer is an error type for repositories without a pending
    76  // transfer request
    77  type ErrNoPendingRepoTransfer struct {
    78  	RepoID int64
    79  }
    80  
    81  func (err ErrNoPendingRepoTransfer) Error() string {
    82  	return fmt.Sprintf("repository doesn't have a pending transfer [repo_id: %d]", err.RepoID)
    83  }
    84  
    85  // IsErrNoPendingTransfer is an error type when a repository has no pending
    86  // transfers
    87  func IsErrNoPendingTransfer(err error) bool {
    88  	_, ok := err.(ErrNoPendingRepoTransfer)
    89  	return ok
    90  }
    91  
    92  func (err ErrNoPendingRepoTransfer) Unwrap() error {
    93  	return util.ErrNotExist
    94  }
    95  
    96  // ErrRepoTransferInProgress represents the state of a repository that has an
    97  // ongoing transfer
    98  type ErrRepoTransferInProgress struct {
    99  	Uname string
   100  	Name  string
   101  }
   102  
   103  // IsErrRepoTransferInProgress checks if an error is a ErrRepoTransferInProgress.
   104  func IsErrRepoTransferInProgress(err error) bool {
   105  	_, ok := err.(ErrRepoTransferInProgress)
   106  	return ok
   107  }
   108  
   109  func (err ErrRepoTransferInProgress) Error() string {
   110  	return fmt.Sprintf("repository is already being transferred [uname: %s, name: %s]", err.Uname, err.Name)
   111  }
   112  
   113  func (err ErrRepoTransferInProgress) Unwrap() error {
   114  	return util.ErrAlreadyExist
   115  }
   116  
   117  // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error.
   118  type ErrInvalidCloneAddr struct {
   119  	Host               string
   120  	IsURLError         bool
   121  	IsInvalidPath      bool
   122  	IsProtocolInvalid  bool
   123  	IsPermissionDenied bool
   124  	LocalPath          bool
   125  }
   126  
   127  // IsErrInvalidCloneAddr checks if an error is a ErrInvalidCloneAddr.
   128  func IsErrInvalidCloneAddr(err error) bool {
   129  	_, ok := err.(*ErrInvalidCloneAddr)
   130  	return ok
   131  }
   132  
   133  func (err *ErrInvalidCloneAddr) Error() string {
   134  	if err.IsInvalidPath {
   135  		return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided path is invalid", err.Host)
   136  	}
   137  	if err.IsProtocolInvalid {
   138  		return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url protocol is not allowed", err.Host)
   139  	}
   140  	if err.IsPermissionDenied {
   141  		return fmt.Sprintf("migration/cloning from '%s' is not allowed.", err.Host)
   142  	}
   143  	if err.IsURLError {
   144  		return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url is invalid", err.Host)
   145  	}
   146  
   147  	return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host)
   148  }
   149  
   150  func (err *ErrInvalidCloneAddr) Unwrap() error {
   151  	return util.ErrInvalidArgument
   152  }
   153  
   154  // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error.
   155  type ErrUpdateTaskNotExist struct {
   156  	UUID string
   157  }
   158  
   159  // IsErrUpdateTaskNotExist checks if an error is a ErrUpdateTaskNotExist.
   160  func IsErrUpdateTaskNotExist(err error) bool {
   161  	_, ok := err.(ErrUpdateTaskNotExist)
   162  	return ok
   163  }
   164  
   165  func (err ErrUpdateTaskNotExist) Error() string {
   166  	return fmt.Sprintf("update task does not exist [uuid: %s]", err.UUID)
   167  }
   168  
   169  func (err ErrUpdateTaskNotExist) Unwrap() error {
   170  	return util.ErrNotExist
   171  }
   172  
   173  // ErrInvalidTagName represents a "InvalidTagName" kind of error.
   174  type ErrInvalidTagName struct {
   175  	TagName string
   176  }
   177  
   178  // IsErrInvalidTagName checks if an error is a ErrInvalidTagName.
   179  func IsErrInvalidTagName(err error) bool {
   180  	_, ok := err.(ErrInvalidTagName)
   181  	return ok
   182  }
   183  
   184  func (err ErrInvalidTagName) Error() string {
   185  	return fmt.Sprintf("release tag name is not valid [tag_name: %s]", err.TagName)
   186  }
   187  
   188  func (err ErrInvalidTagName) Unwrap() error {
   189  	return util.ErrInvalidArgument
   190  }
   191  
   192  // ErrProtectedTagName represents a "ProtectedTagName" kind of error.
   193  type ErrProtectedTagName struct {
   194  	TagName string
   195  }
   196  
   197  // IsErrProtectedTagName checks if an error is a ErrProtectedTagName.
   198  func IsErrProtectedTagName(err error) bool {
   199  	_, ok := err.(ErrProtectedTagName)
   200  	return ok
   201  }
   202  
   203  func (err ErrProtectedTagName) Error() string {
   204  	return fmt.Sprintf("release tag name is protected [tag_name: %s]", err.TagName)
   205  }
   206  
   207  func (err ErrProtectedTagName) Unwrap() error {
   208  	return util.ErrPermissionDenied
   209  }
   210  
   211  // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error.
   212  type ErrRepoFileAlreadyExists struct {
   213  	Path string
   214  }
   215  
   216  // IsErrRepoFileAlreadyExists checks if an error is a ErrRepoFileAlreadyExists.
   217  func IsErrRepoFileAlreadyExists(err error) bool {
   218  	_, ok := err.(ErrRepoFileAlreadyExists)
   219  	return ok
   220  }
   221  
   222  func (err ErrRepoFileAlreadyExists) Error() string {
   223  	return fmt.Sprintf("repository file already exists [path: %s]", err.Path)
   224  }
   225  
   226  func (err ErrRepoFileAlreadyExists) Unwrap() error {
   227  	return util.ErrAlreadyExist
   228  }
   229  
   230  // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error.
   231  type ErrRepoFileDoesNotExist struct {
   232  	Path string
   233  	Name string
   234  }
   235  
   236  // IsErrRepoFileDoesNotExist checks if an error is a ErrRepoDoesNotExist.
   237  func IsErrRepoFileDoesNotExist(err error) bool {
   238  	_, ok := err.(ErrRepoFileDoesNotExist)
   239  	return ok
   240  }
   241  
   242  func (err ErrRepoFileDoesNotExist) Error() string {
   243  	return fmt.Sprintf("repository file does not exist [path: %s]", err.Path)
   244  }
   245  
   246  func (err ErrRepoFileDoesNotExist) Unwrap() error {
   247  	return util.ErrNotExist
   248  }
   249  
   250  // ErrFilenameInvalid represents a "FilenameInvalid" kind of error.
   251  type ErrFilenameInvalid struct {
   252  	Path string
   253  }
   254  
   255  // IsErrFilenameInvalid checks if an error is an ErrFilenameInvalid.
   256  func IsErrFilenameInvalid(err error) bool {
   257  	_, ok := err.(ErrFilenameInvalid)
   258  	return ok
   259  }
   260  
   261  func (err ErrFilenameInvalid) Error() string {
   262  	return fmt.Sprintf("path contains a malformed path component [path: %s]", err.Path)
   263  }
   264  
   265  func (err ErrFilenameInvalid) Unwrap() error {
   266  	return util.ErrInvalidArgument
   267  }
   268  
   269  // ErrUserCannotCommit represents "UserCannotCommit" kind of error.
   270  type ErrUserCannotCommit struct {
   271  	UserName string
   272  }
   273  
   274  // IsErrUserCannotCommit checks if an error is an ErrUserCannotCommit.
   275  func IsErrUserCannotCommit(err error) bool {
   276  	_, ok := err.(ErrUserCannotCommit)
   277  	return ok
   278  }
   279  
   280  func (err ErrUserCannotCommit) Error() string {
   281  	return fmt.Sprintf("user cannot commit to repo [user: %s]", err.UserName)
   282  }
   283  
   284  func (err ErrUserCannotCommit) Unwrap() error {
   285  	return util.ErrPermissionDenied
   286  }
   287  
   288  // ErrFilePathInvalid represents a "FilePathInvalid" kind of error.
   289  type ErrFilePathInvalid struct {
   290  	Message string
   291  	Path    string
   292  	Name    string
   293  	Type    git.EntryMode
   294  }
   295  
   296  // IsErrFilePathInvalid checks if an error is an ErrFilePathInvalid.
   297  func IsErrFilePathInvalid(err error) bool {
   298  	_, ok := err.(ErrFilePathInvalid)
   299  	return ok
   300  }
   301  
   302  func (err ErrFilePathInvalid) Error() string {
   303  	if err.Message != "" {
   304  		return err.Message
   305  	}
   306  	return fmt.Sprintf("path is invalid [path: %s]", err.Path)
   307  }
   308  
   309  func (err ErrFilePathInvalid) Unwrap() error {
   310  	return util.ErrInvalidArgument
   311  }
   312  
   313  // ErrFilePathProtected represents a "FilePathProtected" kind of error.
   314  type ErrFilePathProtected struct {
   315  	Message string
   316  	Path    string
   317  }
   318  
   319  // IsErrFilePathProtected checks if an error is an ErrFilePathProtected.
   320  func IsErrFilePathProtected(err error) bool {
   321  	_, ok := err.(ErrFilePathProtected)
   322  	return ok
   323  }
   324  
   325  func (err ErrFilePathProtected) Error() string {
   326  	if err.Message != "" {
   327  		return err.Message
   328  	}
   329  	return fmt.Sprintf("path is protected and can not be changed [path: %s]", err.Path)
   330  }
   331  
   332  func (err ErrFilePathProtected) Unwrap() error {
   333  	return util.ErrPermissionDenied
   334  }
   335  
   336  // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it.
   337  type ErrDisallowedToMerge struct {
   338  	Reason string
   339  }
   340  
   341  // IsErrDisallowedToMerge checks if an error is an ErrDisallowedToMerge.
   342  func IsErrDisallowedToMerge(err error) bool {
   343  	_, ok := err.(ErrDisallowedToMerge)
   344  	return ok
   345  }
   346  
   347  func (err ErrDisallowedToMerge) Error() string {
   348  	return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason)
   349  }
   350  
   351  func (err ErrDisallowedToMerge) Unwrap() error {
   352  	return util.ErrPermissionDenied
   353  }
   354  
   355  // ErrTagAlreadyExists represents an error that tag with such name already exists.
   356  type ErrTagAlreadyExists struct {
   357  	TagName string
   358  }
   359  
   360  // IsErrTagAlreadyExists checks if an error is an ErrTagAlreadyExists.
   361  func IsErrTagAlreadyExists(err error) bool {
   362  	_, ok := err.(ErrTagAlreadyExists)
   363  	return ok
   364  }
   365  
   366  func (err ErrTagAlreadyExists) Error() string {
   367  	return fmt.Sprintf("tag already exists [name: %s]", err.TagName)
   368  }
   369  
   370  func (err ErrTagAlreadyExists) Unwrap() error {
   371  	return util.ErrAlreadyExist
   372  }
   373  
   374  // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error.
   375  type ErrSHADoesNotMatch struct {
   376  	Path       string
   377  	GivenSHA   string
   378  	CurrentSHA string
   379  }
   380  
   381  // IsErrSHADoesNotMatch checks if an error is a ErrSHADoesNotMatch.
   382  func IsErrSHADoesNotMatch(err error) bool {
   383  	_, ok := err.(ErrSHADoesNotMatch)
   384  	return ok
   385  }
   386  
   387  func (err ErrSHADoesNotMatch) Error() string {
   388  	return fmt.Sprintf("sha does not match [given: %s, expected: %s]", err.GivenSHA, err.CurrentSHA)
   389  }
   390  
   391  // ErrSHANotFound represents a "SHADoesNotMatch" kind of error.
   392  type ErrSHANotFound struct {
   393  	SHA string
   394  }
   395  
   396  // IsErrSHANotFound checks if an error is a ErrSHANotFound.
   397  func IsErrSHANotFound(err error) bool {
   398  	_, ok := err.(ErrSHANotFound)
   399  	return ok
   400  }
   401  
   402  func (err ErrSHANotFound) Error() string {
   403  	return fmt.Sprintf("sha not found [%s]", err.SHA)
   404  }
   405  
   406  func (err ErrSHANotFound) Unwrap() error {
   407  	return util.ErrNotExist
   408  }
   409  
   410  // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error.
   411  type ErrCommitIDDoesNotMatch struct {
   412  	GivenCommitID   string
   413  	CurrentCommitID string
   414  }
   415  
   416  // IsErrCommitIDDoesNotMatch checks if an error is a ErrCommitIDDoesNotMatch.
   417  func IsErrCommitIDDoesNotMatch(err error) bool {
   418  	_, ok := err.(ErrCommitIDDoesNotMatch)
   419  	return ok
   420  }
   421  
   422  func (err ErrCommitIDDoesNotMatch) Error() string {
   423  	return fmt.Sprintf("file CommitID does not match [given: %s, expected: %s]", err.GivenCommitID, err.CurrentCommitID)
   424  }
   425  
   426  // ErrSHAOrCommitIDNotProvided represents a "SHAOrCommitIDNotProvided" kind of error.
   427  type ErrSHAOrCommitIDNotProvided struct{}
   428  
   429  // IsErrSHAOrCommitIDNotProvided checks if an error is a ErrSHAOrCommitIDNotProvided.
   430  func IsErrSHAOrCommitIDNotProvided(err error) bool {
   431  	_, ok := err.(ErrSHAOrCommitIDNotProvided)
   432  	return ok
   433  }
   434  
   435  func (err ErrSHAOrCommitIDNotProvided) Error() string {
   436  	return "a SHA or commit ID must be proved when updating a file"
   437  }
   438  
   439  // ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
   440  type ErrInvalidMergeStyle struct {
   441  	ID    int64
   442  	Style repo_model.MergeStyle
   443  }
   444  
   445  // IsErrInvalidMergeStyle checks if an error is a ErrInvalidMergeStyle.
   446  func IsErrInvalidMergeStyle(err error) bool {
   447  	_, ok := err.(ErrInvalidMergeStyle)
   448  	return ok
   449  }
   450  
   451  func (err ErrInvalidMergeStyle) Error() string {
   452  	return fmt.Sprintf("merge strategy is not allowed or is invalid [repo_id: %d, strategy: %s]",
   453  		err.ID, err.Style)
   454  }
   455  
   456  func (err ErrInvalidMergeStyle) Unwrap() error {
   457  	return util.ErrInvalidArgument
   458  }
   459  
   460  // ErrMergeConflicts represents an error if merging fails with a conflict
   461  type ErrMergeConflicts struct {
   462  	Style  repo_model.MergeStyle
   463  	StdOut string
   464  	StdErr string
   465  	Err    error
   466  }
   467  
   468  // IsErrMergeConflicts checks if an error is a ErrMergeConflicts.
   469  func IsErrMergeConflicts(err error) bool {
   470  	_, ok := err.(ErrMergeConflicts)
   471  	return ok
   472  }
   473  
   474  func (err ErrMergeConflicts) Error() string {
   475  	return fmt.Sprintf("Merge Conflict Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
   476  }
   477  
   478  // ErrMergeUnrelatedHistories represents an error if merging fails due to unrelated histories
   479  type ErrMergeUnrelatedHistories struct {
   480  	Style  repo_model.MergeStyle
   481  	StdOut string
   482  	StdErr string
   483  	Err    error
   484  }
   485  
   486  // IsErrMergeUnrelatedHistories checks if an error is a ErrMergeUnrelatedHistories.
   487  func IsErrMergeUnrelatedHistories(err error) bool {
   488  	_, ok := err.(ErrMergeUnrelatedHistories)
   489  	return ok
   490  }
   491  
   492  func (err ErrMergeUnrelatedHistories) Error() string {
   493  	return fmt.Sprintf("Merge UnrelatedHistories Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
   494  }
   495  
   496  // ErrRebaseConflicts represents an error if rebase fails with a conflict
   497  type ErrRebaseConflicts struct {
   498  	Style     repo_model.MergeStyle
   499  	CommitSHA string
   500  	StdOut    string
   501  	StdErr    string
   502  	Err       error
   503  }
   504  
   505  // IsErrRebaseConflicts checks if an error is a ErrRebaseConflicts.
   506  func IsErrRebaseConflicts(err error) bool {
   507  	_, ok := err.(ErrRebaseConflicts)
   508  	return ok
   509  }
   510  
   511  func (err ErrRebaseConflicts) Error() string {
   512  	return fmt.Sprintf("Rebase Error: %v: Whilst Rebasing: %s\n%s\n%s", err.Err, err.CommitSHA, err.StdErr, err.StdOut)
   513  }
   514  
   515  // ErrPullRequestHasMerged represents a "PullRequestHasMerged"-error
   516  type ErrPullRequestHasMerged struct {
   517  	ID         int64
   518  	IssueID    int64
   519  	HeadRepoID int64
   520  	BaseRepoID int64
   521  	HeadBranch string
   522  	BaseBranch string
   523  }
   524  
   525  // IsErrPullRequestHasMerged checks if an error is a ErrPullRequestHasMerged.
   526  func IsErrPullRequestHasMerged(err error) bool {
   527  	_, ok := err.(ErrPullRequestHasMerged)
   528  	return ok
   529  }
   530  
   531  // Error does pretty-printing :D
   532  func (err ErrPullRequestHasMerged) Error() string {
   533  	return fmt.Sprintf("pull request has merged [id: %d, issue_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]",
   534  		err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch)
   535  }