errors.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package util
  15. import (
  16. "errors"
  17. "fmt"
  18. )
  19. const (
  20. templateLoadErrorHints = "Try setting the absolute templates path in your configuration file " +
  21. "or specifying the config directory adding the `-c` flag to the serve options. For example: " +
  22. "sftpgo serve -c \"<path to dir containing the default config file and templates directory>\""
  23. )
  24. // MaxRecursion defines the maximum number of allowed recursions
  25. const MaxRecursion = 1000
  26. // errors definitions
  27. var (
  28. ErrValidation = NewValidationError("")
  29. ErrNotFound = NewRecordNotFoundError("")
  30. ErrMethodDisabled = NewMethodDisabledError("")
  31. ErrGeneric = NewGenericError("")
  32. ErrRecursionTooDeep = errors.New("recursion too deep")
  33. )
  34. // ValidationError raised if input data is not valid
  35. type ValidationError struct {
  36. err string
  37. }
  38. // Validation error details
  39. func (e *ValidationError) Error() string {
  40. return fmt.Sprintf("Validation error: %s", e.err)
  41. }
  42. // GetErrorString returns the unmodified error string
  43. func (e *ValidationError) GetErrorString() string {
  44. return e.err
  45. }
  46. // Is reports if target matches
  47. func (e *ValidationError) Is(target error) bool {
  48. _, ok := target.(*ValidationError)
  49. return ok
  50. }
  51. // NewValidationError returns a validation errors
  52. func NewValidationError(errorString string) *ValidationError {
  53. return &ValidationError{
  54. err: errorString,
  55. }
  56. }
  57. // RecordNotFoundError raised if a requested object is not found
  58. type RecordNotFoundError struct {
  59. err string
  60. }
  61. func (e *RecordNotFoundError) Error() string {
  62. return fmt.Sprintf("not found: %s", e.err)
  63. }
  64. // Is reports if target matches
  65. func (e *RecordNotFoundError) Is(target error) bool {
  66. _, ok := target.(*RecordNotFoundError)
  67. return ok
  68. }
  69. // NewRecordNotFoundError returns a not found error
  70. func NewRecordNotFoundError(errorString string) *RecordNotFoundError {
  71. return &RecordNotFoundError{
  72. err: errorString,
  73. }
  74. }
  75. // MethodDisabledError raised if a method is disabled in config file.
  76. // For example, if user management is disabled, this error is raised
  77. // every time a user operation is done using the REST API
  78. type MethodDisabledError struct {
  79. err string
  80. }
  81. // Method disabled error details
  82. func (e *MethodDisabledError) Error() string {
  83. return fmt.Sprintf("Method disabled error: %s", e.err)
  84. }
  85. // Is reports if target matches
  86. func (e *MethodDisabledError) Is(target error) bool {
  87. _, ok := target.(*MethodDisabledError)
  88. return ok
  89. }
  90. // NewMethodDisabledError returns a method disabled error
  91. func NewMethodDisabledError(errorString string) *MethodDisabledError {
  92. return &MethodDisabledError{
  93. err: errorString,
  94. }
  95. }
  96. // GenericError raised for not well categorized error
  97. type GenericError struct {
  98. err string
  99. }
  100. func (e *GenericError) Error() string {
  101. return e.err
  102. }
  103. // Is reports if target matches
  104. func (e *GenericError) Is(target error) bool {
  105. _, ok := target.(*GenericError)
  106. return ok
  107. }
  108. // NewGenericError returns a generic error
  109. func NewGenericError(errorString string) *GenericError {
  110. return &GenericError{
  111. err: errorString,
  112. }
  113. }