gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db
  4. import (
  5. "fmt"
  6. "code.gitea.io/gitea/modules/util"
  7. )
  8. // ErrCancelled represents an error due to context cancellation
  9. type ErrCancelled struct {
  10. Message string
  11. }
  12. // IsErrCancelled checks if an error is a ErrCancelled.
  13. func IsErrCancelled(err error) bool {
  14. _, ok := err.(ErrCancelled)
  15. return ok
  16. }
  17. func (err ErrCancelled) Error() string {
  18. return "Cancelled: " + err.Message
  19. }
  20. // ErrCancelledf returns an ErrCancelled for the provided format and args
  21. func ErrCancelledf(format string, args ...any) error {
  22. return ErrCancelled{
  23. fmt.Sprintf(format, args...),
  24. }
  25. }
  26. // ErrSSHDisabled represents an "SSH disabled" error.
  27. type ErrSSHDisabled struct{}
  28. // IsErrSSHDisabled checks if an error is a ErrSSHDisabled.
  29. func IsErrSSHDisabled(err error) bool {
  30. _, ok := err.(ErrSSHDisabled)
  31. return ok
  32. }
  33. func (err ErrSSHDisabled) Error() string {
  34. return "SSH is disabled"
  35. }
  36. // ErrNotExist represents a non-exist error.
  37. type ErrNotExist struct {
  38. Resource string
  39. ID int64
  40. }
  41. // IsErrNotExist checks if an error is an ErrNotExist
  42. func IsErrNotExist(err error) bool {
  43. _, ok := err.(ErrNotExist)
  44. return ok
  45. }
  46. func (err ErrNotExist) Error() string {
  47. name := "record"
  48. if err.Resource != "" {
  49. name = err.Resource
  50. }
  51. if err.ID != 0 {
  52. return fmt.Sprintf("%s does not exist [id: %d]", name, err.ID)
  53. }
  54. return name + " does not exist"
  55. }
  56. // Unwrap unwraps this as a ErrNotExist err
  57. func (err ErrNotExist) Unwrap() error {
  58. return util.ErrNotExist
  59. }