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

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package db
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"code.gitea.io/gitea/modules/util"
    10  )
    11  
    12  // ErrCancelled represents an error due to context cancellation
    13  type ErrCancelled struct {
    14  	Message string
    15  }
    16  
    17  // IsErrCancelled checks if an error is a ErrCancelled.
    18  func IsErrCancelled(err error) bool {
    19  	_, ok := err.(ErrCancelled)
    20  	return ok
    21  }
    22  
    23  func (err ErrCancelled) Error() string {
    24  	return "Cancelled: " + err.Message
    25  }
    26  
    27  // ErrCancelledf returns an ErrCancelled for the provided format and args
    28  func ErrCancelledf(format string, args ...any) error {
    29  	return ErrCancelled{
    30  		fmt.Sprintf(format, args...),
    31  	}
    32  }
    33  
    34  // ErrSSHDisabled represents an "SSH disabled" error.
    35  type ErrSSHDisabled struct{}
    36  
    37  // IsErrSSHDisabled checks if an error is a ErrSSHDisabled.
    38  func IsErrSSHDisabled(err error) bool {
    39  	_, ok := err.(ErrSSHDisabled)
    40  	return ok
    41  }
    42  
    43  func (err ErrSSHDisabled) Error() string {
    44  	return "SSH is disabled"
    45  }
    46  
    47  // ErrNotExist represents a non-exist error.
    48  type ErrNotExist struct {
    49  	Resource string
    50  	ID       int64
    51  }
    52  
    53  // IsErrNotExist checks if an error is an ErrNotExist
    54  func IsErrNotExist(err error) bool {
    55  	_, ok := err.(ErrNotExist)
    56  	return ok
    57  }
    58  
    59  func (err ErrNotExist) Error() string {
    60  	name := "record"
    61  	if err.Resource != "" {
    62  		name = err.Resource
    63  	}
    64  
    65  	if err.ID != 0 {
    66  		return fmt.Sprintf("%s does not exist [id: %d]", name, err.ID)
    67  	}
    68  	return fmt.Sprintf("%s does not exist", name)
    69  }
    70  
    71  // Unwrap unwraps this as a ErrNotExist err
    72  func (err ErrNotExist) Unwrap() error {
    73  	return util.ErrNotExist
    74  }
    75  
    76  // ErrConditionRequired represents an error which require condition.
    77  type ErrConditionRequired struct{}
    78  
    79  // IsErrConditionRequired checks if an error is an ErrConditionRequired
    80  func IsErrConditionRequired(err error) bool {
    81  	_, ok := err.(ErrConditionRequired)
    82  	return ok
    83  }
    84  
    85  func (err ErrConditionRequired) Error() string {
    86  	return "condition is required"
    87  }
    88  
    89  // Unwrap unwraps this as a ErrNotExist err
    90  func (err ErrConditionRequired) Unwrap() error {
    91  	return util.ErrInvalidArgument
    92  }