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 1.6KB

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