gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "errors"
  6. "fmt"
  7. )
  8. // Common Errors forming the base of our error system
  9. //
  10. // Many Errors returned by Gitea can be tested against these errors using "errors.Is".
  11. var (
  12. ErrInvalidArgument = errors.New("invalid argument") // also implies HTTP 400
  13. ErrPermissionDenied = errors.New("permission denied") // also implies HTTP 403
  14. ErrNotExist = errors.New("resource does not exist") // also implies HTTP 404
  15. ErrAlreadyExist = errors.New("resource already exists") // also implies HTTP 409
  16. // ErrUnprocessableContent implies HTTP 422, the syntax of the request content is correct,
  17. // but the server is unable to process the contained instructions
  18. ErrUnprocessableContent = errors.New("unprocessable content")
  19. )
  20. // errorWrapper provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
  21. // Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist'
  22. type errorWrapper struct {
  23. Message string
  24. Err error
  25. }
  26. // Error returns the message
  27. func (w errorWrapper) Error() string {
  28. return w.Message
  29. }
  30. // Unwrap returns the underlying error
  31. func (w errorWrapper) Unwrap() error {
  32. return w.Err
  33. }
  34. type LocaleWrapper struct {
  35. err error
  36. TrKey string
  37. TrArgs []any
  38. }
  39. // Error returns the message
  40. func (w LocaleWrapper) Error() string {
  41. return w.err.Error()
  42. }
  43. // Unwrap returns the underlying error
  44. func (w LocaleWrapper) Unwrap() error {
  45. return w.err
  46. }
  47. // ErrorWrap returns an error that formats as the given text but unwraps as the provided error
  48. func ErrorWrap(unwrap error, message string, args ...any) error {
  49. if len(args) == 0 {
  50. return errorWrapper{Message: message, Err: unwrap}
  51. }
  52. return errorWrapper{Message: fmt.Sprintf(message, args...), Err: unwrap}
  53. }
  54. // NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument
  55. func NewInvalidArgumentErrorf(message string, args ...any) error {
  56. return ErrorWrap(ErrInvalidArgument, message, args...)
  57. }
  58. // NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied
  59. func NewPermissionDeniedErrorf(message string, args ...any) error {
  60. return ErrorWrap(ErrPermissionDenied, message, args...)
  61. }
  62. // NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist
  63. func NewAlreadyExistErrorf(message string, args ...any) error {
  64. return ErrorWrap(ErrAlreadyExist, message, args...)
  65. }
  66. // NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist
  67. func NewNotExistErrorf(message string, args ...any) error {
  68. return ErrorWrap(ErrNotExist, message, args...)
  69. }
  70. // ErrorWrapLocale wraps an err with a translation key and arguments
  71. func ErrorWrapLocale(err error, trKey string, trArgs ...any) error {
  72. return LocaleWrapper{err: err, TrKey: trKey, TrArgs: trArgs}
  73. }
  74. func ErrorAsLocale(err error) *LocaleWrapper {
  75. var e LocaleWrapper
  76. if errors.As(err, &e) {
  77. return &e
  78. }
  79. return nil
  80. }