errors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2019-2022 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. ErrNotFound = NewRecordNotFoundError("")
  26. )
  27. // ValidationError raised if input data is not valid
  28. type ValidationError struct {
  29. err string
  30. }
  31. // Validation error details
  32. func (e *ValidationError) Error() string {
  33. return fmt.Sprintf("Validation error: %s", e.err)
  34. }
  35. // GetErrorString returns the unmodified error string
  36. func (e *ValidationError) GetErrorString() string {
  37. return e.err
  38. }
  39. // NewValidationError returns a validation errors
  40. func NewValidationError(error string) *ValidationError {
  41. return &ValidationError{
  42. err: error,
  43. }
  44. }
  45. // RecordNotFoundError raised if a requested object is not found
  46. type RecordNotFoundError struct {
  47. err string
  48. }
  49. func (e *RecordNotFoundError) Error() string {
  50. return fmt.Sprintf("not found: %s", e.err)
  51. }
  52. // Is reports if target matches
  53. func (e *RecordNotFoundError) Is(target error) bool {
  54. _, ok := target.(*RecordNotFoundError)
  55. return ok
  56. }
  57. // NewRecordNotFoundError returns a not found error
  58. func NewRecordNotFoundError(error string) *RecordNotFoundError {
  59. return &RecordNotFoundError{
  60. err: error,
  61. }
  62. }
  63. // MethodDisabledError raised if a method is disabled in config file.
  64. // For example, if user management is disabled, this error is raised
  65. // every time a user operation is done using the REST API
  66. type MethodDisabledError struct {
  67. err string
  68. }
  69. // Method disabled error details
  70. func (e *MethodDisabledError) Error() string {
  71. return fmt.Sprintf("Method disabled error: %s", e.err)
  72. }
  73. // NewMethodDisabledError returns a method disabled error
  74. func NewMethodDisabledError(error string) *MethodDisabledError {
  75. return &MethodDisabledError{
  76. err: error,
  77. }
  78. }
  79. // GenericError raised for not well categorized error
  80. type GenericError struct {
  81. err string
  82. }
  83. func (e *GenericError) Error() string {
  84. return e.err
  85. }
  86. // NewGenericError returns a generic error
  87. func NewGenericError(error string) *GenericError {
  88. return &GenericError{
  89. err: error,
  90. }
  91. }