errors.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2019-2023 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. "fmt"
  17. )
  18. const (
  19. templateLoadErrorHints = "Try setting the absolute templates path in your configuration file " +
  20. "or specifying the config directory adding the `-c` flag to the serve options. For example: " +
  21. "sftpgo serve -c \"<path to dir containing the default config file and templates directory>\""
  22. )
  23. // errors definitions
  24. var (
  25. ErrValidation = NewValidationError("")
  26. ErrNotFound = NewRecordNotFoundError("")
  27. ErrMethodDisabled = NewMethodDisabledError("")
  28. ErrGeneric = NewGenericError("")
  29. )
  30. // ValidationError raised if input data is not valid
  31. type ValidationError struct {
  32. err string
  33. }
  34. // Validation error details
  35. func (e *ValidationError) Error() string {
  36. return fmt.Sprintf("Validation error: %s", e.err)
  37. }
  38. // GetErrorString returns the unmodified error string
  39. func (e *ValidationError) GetErrorString() string {
  40. return e.err
  41. }
  42. // Is reports if target matches
  43. func (e *ValidationError) Is(target error) bool {
  44. _, ok := target.(*ValidationError)
  45. return ok
  46. }
  47. // NewValidationError returns a validation errors
  48. func NewValidationError(error string) *ValidationError {
  49. return &ValidationError{
  50. err: error,
  51. }
  52. }
  53. // RecordNotFoundError raised if a requested object is not found
  54. type RecordNotFoundError struct {
  55. err string
  56. }
  57. func (e *RecordNotFoundError) Error() string {
  58. return fmt.Sprintf("not found: %s", e.err)
  59. }
  60. // Is reports if target matches
  61. func (e *RecordNotFoundError) Is(target error) bool {
  62. _, ok := target.(*RecordNotFoundError)
  63. return ok
  64. }
  65. // NewRecordNotFoundError returns a not found error
  66. func NewRecordNotFoundError(error string) *RecordNotFoundError {
  67. return &RecordNotFoundError{
  68. err: error,
  69. }
  70. }
  71. // MethodDisabledError raised if a method is disabled in config file.
  72. // For example, if user management is disabled, this error is raised
  73. // every time a user operation is done using the REST API
  74. type MethodDisabledError struct {
  75. err string
  76. }
  77. // Method disabled error details
  78. func (e *MethodDisabledError) Error() string {
  79. return fmt.Sprintf("Method disabled error: %s", e.err)
  80. }
  81. // Is reports if target matches
  82. func (e *MethodDisabledError) Is(target error) bool {
  83. _, ok := target.(*MethodDisabledError)
  84. return ok
  85. }
  86. // NewMethodDisabledError returns a method disabled error
  87. func NewMethodDisabledError(error string) *MethodDisabledError {
  88. return &MethodDisabledError{
  89. err: error,
  90. }
  91. }
  92. // GenericError raised for not well categorized error
  93. type GenericError struct {
  94. err string
  95. }
  96. func (e *GenericError) Error() string {
  97. return e.err
  98. }
  99. // Is reports if target matches
  100. func (e *GenericError) Is(target error) bool {
  101. _, ok := target.(*GenericError)
  102. return ok
  103. }
  104. // NewGenericError returns a generic error
  105. func NewGenericError(error string) *GenericError {
  106. return &GenericError{
  107. err: error,
  108. }
  109. }