code.gitea.io/gitea@v1.19.3/modules/git/error.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	"code.gitea.io/gitea/modules/util"
    12  )
    13  
    14  // ErrExecTimeout error when exec timed out
    15  type ErrExecTimeout struct {
    16  	Duration time.Duration
    17  }
    18  
    19  // IsErrExecTimeout if some error is ErrExecTimeout
    20  func IsErrExecTimeout(err error) bool {
    21  	_, ok := err.(ErrExecTimeout)
    22  	return ok
    23  }
    24  
    25  func (err ErrExecTimeout) Error() string {
    26  	return fmt.Sprintf("execution is timeout [duration: %v]", err.Duration)
    27  }
    28  
    29  // ErrNotExist commit not exist error
    30  type ErrNotExist struct {
    31  	ID      string
    32  	RelPath string
    33  }
    34  
    35  // IsErrNotExist if some error is ErrNotExist
    36  func IsErrNotExist(err error) bool {
    37  	_, ok := err.(ErrNotExist)
    38  	return ok
    39  }
    40  
    41  func (err ErrNotExist) Error() string {
    42  	return fmt.Sprintf("object does not exist [id: %s, rel_path: %s]", err.ID, err.RelPath)
    43  }
    44  
    45  func (err ErrNotExist) Unwrap() error {
    46  	return util.ErrNotExist
    47  }
    48  
    49  // ErrBadLink entry.FollowLink error
    50  type ErrBadLink struct {
    51  	Name    string
    52  	Message string
    53  }
    54  
    55  func (err ErrBadLink) Error() string {
    56  	return fmt.Sprintf("%s: %s", err.Name, err.Message)
    57  }
    58  
    59  // IsErrBadLink if some error is ErrBadLink
    60  func IsErrBadLink(err error) bool {
    61  	_, ok := err.(ErrBadLink)
    62  	return ok
    63  }
    64  
    65  // ErrUnsupportedVersion error when required git version not matched
    66  type ErrUnsupportedVersion struct {
    67  	Required string
    68  }
    69  
    70  // IsErrUnsupportedVersion if some error is ErrUnsupportedVersion
    71  func IsErrUnsupportedVersion(err error) bool {
    72  	_, ok := err.(ErrUnsupportedVersion)
    73  	return ok
    74  }
    75  
    76  func (err ErrUnsupportedVersion) Error() string {
    77  	return fmt.Sprintf("Operation requires higher version [required: %s]", err.Required)
    78  }
    79  
    80  // ErrBranchNotExist represents a "BranchNotExist" kind of error.
    81  type ErrBranchNotExist struct {
    82  	Name string
    83  }
    84  
    85  // IsErrBranchNotExist checks if an error is a ErrBranchNotExist.
    86  func IsErrBranchNotExist(err error) bool {
    87  	_, ok := err.(ErrBranchNotExist)
    88  	return ok
    89  }
    90  
    91  func (err ErrBranchNotExist) Error() string {
    92  	return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
    93  }
    94  
    95  func (err ErrBranchNotExist) Unwrap() error {
    96  	return util.ErrNotExist
    97  }
    98  
    99  // ErrPushOutOfDate represents an error if merging fails due to unrelated histories
   100  type ErrPushOutOfDate struct {
   101  	StdOut string
   102  	StdErr string
   103  	Err    error
   104  }
   105  
   106  // IsErrPushOutOfDate checks if an error is a ErrPushOutOfDate.
   107  func IsErrPushOutOfDate(err error) bool {
   108  	_, ok := err.(*ErrPushOutOfDate)
   109  	return ok
   110  }
   111  
   112  func (err *ErrPushOutOfDate) Error() string {
   113  	return fmt.Sprintf("PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
   114  }
   115  
   116  // Unwrap unwraps the underlying error
   117  func (err *ErrPushOutOfDate) Unwrap() error {
   118  	return fmt.Errorf("%w - %s", err.Err, err.StdErr)
   119  }
   120  
   121  // ErrPushRejected represents an error if merging fails due to rejection from a hook
   122  type ErrPushRejected struct {
   123  	Message string
   124  	StdOut  string
   125  	StdErr  string
   126  	Err     error
   127  }
   128  
   129  // IsErrPushRejected checks if an error is a ErrPushRejected.
   130  func IsErrPushRejected(err error) bool {
   131  	_, ok := err.(*ErrPushRejected)
   132  	return ok
   133  }
   134  
   135  func (err *ErrPushRejected) Error() string {
   136  	return fmt.Sprintf("PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
   137  }
   138  
   139  // Unwrap unwraps the underlying error
   140  func (err *ErrPushRejected) Unwrap() error {
   141  	return fmt.Errorf("%w - %s", err.Err, err.StdErr)
   142  }
   143  
   144  // GenerateMessage generates the remote message from the stderr
   145  func (err *ErrPushRejected) GenerateMessage() {
   146  	messageBuilder := &strings.Builder{}
   147  	i := strings.Index(err.StdErr, "remote: ")
   148  	if i < 0 {
   149  		err.Message = ""
   150  		return
   151  	}
   152  	for {
   153  		if len(err.StdErr) <= i+8 {
   154  			break
   155  		}
   156  		if err.StdErr[i:i+8] != "remote: " {
   157  			break
   158  		}
   159  		i += 8
   160  		nl := strings.IndexByte(err.StdErr[i:], '\n')
   161  		if nl >= 0 {
   162  			messageBuilder.WriteString(err.StdErr[i : i+nl+1])
   163  			i = i + nl + 1
   164  		} else {
   165  			messageBuilder.WriteString(err.StdErr[i:])
   166  			i = len(err.StdErr)
   167  		}
   168  	}
   169  	err.Message = strings.TrimSpace(messageBuilder.String())
   170  }
   171  
   172  // ErrMoreThanOne represents an error if pull request fails when there are more than one sources (branch, tag) with the same name
   173  type ErrMoreThanOne struct {
   174  	StdOut string
   175  	StdErr string
   176  	Err    error
   177  }
   178  
   179  // IsErrMoreThanOne checks if an error is a ErrMoreThanOne
   180  func IsErrMoreThanOne(err error) bool {
   181  	_, ok := err.(*ErrMoreThanOne)
   182  	return ok
   183  }
   184  
   185  func (err *ErrMoreThanOne) Error() string {
   186  	return fmt.Sprintf("ErrMoreThanOne Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
   187  }