You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.go 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 fmt.Sprintf("%s does not exist", name)
  55. }
  56. // Unwrap unwraps this as a ErrNotExist err
  57. func (err ErrNotExist) Unwrap() error {
  58. return util.ErrNotExist
  59. }
  60. // ErrConditionRequired represents an error which require condition.
  61. type ErrConditionRequired struct{}
  62. // IsErrConditionRequired checks if an error is an ErrConditionRequired
  63. func IsErrConditionRequired(err error) bool {
  64. _, ok := err.(ErrConditionRequired)
  65. return ok
  66. }
  67. func (err ErrConditionRequired) Error() string {
  68. return "condition is required"
  69. }
  70. // Unwrap unwraps this as a ErrNotExist err
  71. func (err ErrConditionRequired) Unwrap() error {
  72. return util.ErrInvalidArgument
  73. }