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

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package asymkey
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"code.gitea.io/gitea/modules/util"
    10  )
    11  
    12  // ErrKeyUnableVerify represents a "KeyUnableVerify" kind of error.
    13  type ErrKeyUnableVerify struct {
    14  	Result string
    15  }
    16  
    17  // IsErrKeyUnableVerify checks if an error is a ErrKeyUnableVerify.
    18  func IsErrKeyUnableVerify(err error) bool {
    19  	_, ok := err.(ErrKeyUnableVerify)
    20  	return ok
    21  }
    22  
    23  func (err ErrKeyUnableVerify) Error() string {
    24  	return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result)
    25  }
    26  
    27  // ErrKeyIsPrivate is returned when the provided key is a private key not a public key
    28  var ErrKeyIsPrivate = util.NewSilentWrapErrorf(util.ErrInvalidArgument, "the provided key is a private key")
    29  
    30  // ErrKeyNotExist represents a "KeyNotExist" kind of error.
    31  type ErrKeyNotExist struct {
    32  	ID int64
    33  }
    34  
    35  // IsErrKeyNotExist checks if an error is a ErrKeyNotExist.
    36  func IsErrKeyNotExist(err error) bool {
    37  	_, ok := err.(ErrKeyNotExist)
    38  	return ok
    39  }
    40  
    41  func (err ErrKeyNotExist) Error() string {
    42  	return fmt.Sprintf("public key does not exist [id: %d]", err.ID)
    43  }
    44  
    45  func (err ErrKeyNotExist) Unwrap() error {
    46  	return util.ErrNotExist
    47  }
    48  
    49  // ErrKeyAlreadyExist represents a "KeyAlreadyExist" kind of error.
    50  type ErrKeyAlreadyExist struct {
    51  	OwnerID     int64
    52  	Fingerprint string
    53  	Content     string
    54  }
    55  
    56  // IsErrKeyAlreadyExist checks if an error is a ErrKeyAlreadyExist.
    57  func IsErrKeyAlreadyExist(err error) bool {
    58  	_, ok := err.(ErrKeyAlreadyExist)
    59  	return ok
    60  }
    61  
    62  func (err ErrKeyAlreadyExist) Error() string {
    63  	return fmt.Sprintf("public key already exists [owner_id: %d, finger_print: %s, content: %s]",
    64  		err.OwnerID, err.Fingerprint, err.Content)
    65  }
    66  
    67  func (err ErrKeyAlreadyExist) Unwrap() error {
    68  	return util.ErrAlreadyExist
    69  }
    70  
    71  // ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error.
    72  type ErrKeyNameAlreadyUsed struct {
    73  	OwnerID int64
    74  	Name    string
    75  }
    76  
    77  // IsErrKeyNameAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
    78  func IsErrKeyNameAlreadyUsed(err error) bool {
    79  	_, ok := err.(ErrKeyNameAlreadyUsed)
    80  	return ok
    81  }
    82  
    83  func (err ErrKeyNameAlreadyUsed) Error() string {
    84  	return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name)
    85  }
    86  
    87  func (err ErrKeyNameAlreadyUsed) Unwrap() error {
    88  	return util.ErrAlreadyExist
    89  }
    90  
    91  // ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error.
    92  type ErrGPGNoEmailFound struct {
    93  	FailedEmails []string
    94  	ID           string
    95  }
    96  
    97  // IsErrGPGNoEmailFound checks if an error is a ErrGPGNoEmailFound.
    98  func IsErrGPGNoEmailFound(err error) bool {
    99  	_, ok := err.(ErrGPGNoEmailFound)
   100  	return ok
   101  }
   102  
   103  func (err ErrGPGNoEmailFound) Error() string {
   104  	return fmt.Sprintf("none of the emails attached to the GPG key could be found: %v", err.FailedEmails)
   105  }
   106  
   107  // ErrGPGInvalidTokenSignature represents a "ErrGPGInvalidTokenSignature" kind of error.
   108  type ErrGPGInvalidTokenSignature struct {
   109  	Wrapped error
   110  	ID      string
   111  }
   112  
   113  // IsErrGPGInvalidTokenSignature checks if an error is a ErrGPGInvalidTokenSignature.
   114  func IsErrGPGInvalidTokenSignature(err error) bool {
   115  	_, ok := err.(ErrGPGInvalidTokenSignature)
   116  	return ok
   117  }
   118  
   119  func (err ErrGPGInvalidTokenSignature) Error() string {
   120  	return "the provided signature does not sign the token with the provided key"
   121  }
   122  
   123  // ErrGPGKeyParsing represents a "ErrGPGKeyParsing" kind of error.
   124  type ErrGPGKeyParsing struct {
   125  	ParseError error
   126  }
   127  
   128  // IsErrGPGKeyParsing checks if an error is a ErrGPGKeyParsing.
   129  func IsErrGPGKeyParsing(err error) bool {
   130  	_, ok := err.(ErrGPGKeyParsing)
   131  	return ok
   132  }
   133  
   134  func (err ErrGPGKeyParsing) Error() string {
   135  	return fmt.Sprintf("failed to parse gpg key %s", err.ParseError.Error())
   136  }
   137  
   138  // ErrGPGKeyNotExist represents a "GPGKeyNotExist" kind of error.
   139  type ErrGPGKeyNotExist struct {
   140  	ID int64
   141  }
   142  
   143  // IsErrGPGKeyNotExist checks if an error is a ErrGPGKeyNotExist.
   144  func IsErrGPGKeyNotExist(err error) bool {
   145  	_, ok := err.(ErrGPGKeyNotExist)
   146  	return ok
   147  }
   148  
   149  func (err ErrGPGKeyNotExist) Error() string {
   150  	return fmt.Sprintf("public gpg key does not exist [id: %d]", err.ID)
   151  }
   152  
   153  func (err ErrGPGKeyNotExist) Unwrap() error {
   154  	return util.ErrNotExist
   155  }
   156  
   157  // ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error.
   158  type ErrGPGKeyImportNotExist struct {
   159  	ID string
   160  }
   161  
   162  // IsErrGPGKeyImportNotExist checks if an error is a ErrGPGKeyImportNotExist.
   163  func IsErrGPGKeyImportNotExist(err error) bool {
   164  	_, ok := err.(ErrGPGKeyImportNotExist)
   165  	return ok
   166  }
   167  
   168  func (err ErrGPGKeyImportNotExist) Error() string {
   169  	return fmt.Sprintf("public gpg key import does not exist [id: %s]", err.ID)
   170  }
   171  
   172  func (err ErrGPGKeyImportNotExist) Unwrap() error {
   173  	return util.ErrNotExist
   174  }
   175  
   176  // ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error.
   177  type ErrGPGKeyIDAlreadyUsed struct {
   178  	KeyID string
   179  }
   180  
   181  // IsErrGPGKeyIDAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
   182  func IsErrGPGKeyIDAlreadyUsed(err error) bool {
   183  	_, ok := err.(ErrGPGKeyIDAlreadyUsed)
   184  	return ok
   185  }
   186  
   187  func (err ErrGPGKeyIDAlreadyUsed) Error() string {
   188  	return fmt.Sprintf("public key already exists [key_id: %s]", err.KeyID)
   189  }
   190  
   191  func (err ErrGPGKeyIDAlreadyUsed) Unwrap() error {
   192  	return util.ErrAlreadyExist
   193  }
   194  
   195  // ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error.
   196  type ErrGPGKeyAccessDenied struct {
   197  	UserID int64
   198  	KeyID  int64
   199  }
   200  
   201  // IsErrGPGKeyAccessDenied checks if an error is a ErrGPGKeyAccessDenied.
   202  func IsErrGPGKeyAccessDenied(err error) bool {
   203  	_, ok := err.(ErrGPGKeyAccessDenied)
   204  	return ok
   205  }
   206  
   207  // Error pretty-prints an error of type ErrGPGKeyAccessDenied.
   208  func (err ErrGPGKeyAccessDenied) Error() string {
   209  	return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d]",
   210  		err.UserID, err.KeyID)
   211  }
   212  
   213  func (err ErrGPGKeyAccessDenied) Unwrap() error {
   214  	return util.ErrPermissionDenied
   215  }
   216  
   217  // ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error.
   218  type ErrKeyAccessDenied struct {
   219  	UserID int64
   220  	KeyID  int64
   221  	Note   string
   222  }
   223  
   224  // IsErrKeyAccessDenied checks if an error is a ErrKeyAccessDenied.
   225  func IsErrKeyAccessDenied(err error) bool {
   226  	_, ok := err.(ErrKeyAccessDenied)
   227  	return ok
   228  }
   229  
   230  func (err ErrKeyAccessDenied) Error() string {
   231  	return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d, note: %s]",
   232  		err.UserID, err.KeyID, err.Note)
   233  }
   234  
   235  func (err ErrKeyAccessDenied) Unwrap() error {
   236  	return util.ErrPermissionDenied
   237  }
   238  
   239  // ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error.
   240  type ErrDeployKeyNotExist struct {
   241  	ID     int64
   242  	KeyID  int64
   243  	RepoID int64
   244  }
   245  
   246  // IsErrDeployKeyNotExist checks if an error is a ErrDeployKeyNotExist.
   247  func IsErrDeployKeyNotExist(err error) bool {
   248  	_, ok := err.(ErrDeployKeyNotExist)
   249  	return ok
   250  }
   251  
   252  func (err ErrDeployKeyNotExist) Error() string {
   253  	return fmt.Sprintf("Deploy key does not exist [id: %d, key_id: %d, repo_id: %d]", err.ID, err.KeyID, err.RepoID)
   254  }
   255  
   256  func (err ErrDeployKeyNotExist) Unwrap() error {
   257  	return util.ErrNotExist
   258  }
   259  
   260  // ErrDeployKeyAlreadyExist represents a "DeployKeyAlreadyExist" kind of error.
   261  type ErrDeployKeyAlreadyExist struct {
   262  	KeyID  int64
   263  	RepoID int64
   264  }
   265  
   266  // IsErrDeployKeyAlreadyExist checks if an error is a ErrDeployKeyAlreadyExist.
   267  func IsErrDeployKeyAlreadyExist(err error) bool {
   268  	_, ok := err.(ErrDeployKeyAlreadyExist)
   269  	return ok
   270  }
   271  
   272  func (err ErrDeployKeyAlreadyExist) Error() string {
   273  	return fmt.Sprintf("public key already exists [key_id: %d, repo_id: %d]", err.KeyID, err.RepoID)
   274  }
   275  
   276  func (err ErrDeployKeyAlreadyExist) Unwrap() error {
   277  	return util.ErrAlreadyExist
   278  }
   279  
   280  // ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error.
   281  type ErrDeployKeyNameAlreadyUsed struct {
   282  	RepoID int64
   283  	Name   string
   284  }
   285  
   286  // IsErrDeployKeyNameAlreadyUsed checks if an error is a ErrDeployKeyNameAlreadyUsed.
   287  func IsErrDeployKeyNameAlreadyUsed(err error) bool {
   288  	_, ok := err.(ErrDeployKeyNameAlreadyUsed)
   289  	return ok
   290  }
   291  
   292  func (err ErrDeployKeyNameAlreadyUsed) Error() string {
   293  	return fmt.Sprintf("public key with name already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
   294  }
   295  
   296  func (err ErrDeployKeyNameAlreadyUsed) Unwrap() error {
   297  	return util.ErrNotExist
   298  }
   299  
   300  // ErrSSHInvalidTokenSignature represents a "ErrSSHInvalidTokenSignature" kind of error.
   301  type ErrSSHInvalidTokenSignature struct {
   302  	Wrapped     error
   303  	Fingerprint string
   304  }
   305  
   306  // IsErrSSHInvalidTokenSignature checks if an error is a ErrSSHInvalidTokenSignature.
   307  func IsErrSSHInvalidTokenSignature(err error) bool {
   308  	_, ok := err.(ErrSSHInvalidTokenSignature)
   309  	return ok
   310  }
   311  
   312  func (err ErrSSHInvalidTokenSignature) Error() string {
   313  	return "the provided signature does not sign the token with the provided key"
   314  }
   315  
   316  func (err ErrSSHInvalidTokenSignature) Unwrap() error {
   317  	return util.ErrInvalidArgument
   318  }