utils.go 528 B

12345678910111213141516171819202122232425262728293031
  1. package adaptor
  2. import (
  3. "fmt"
  4. "github.com/bytedance/sonic"
  5. )
  6. type BasicError[T any] struct {
  7. error T
  8. statusCode int
  9. }
  10. func (e BasicError[T]) MarshalJSON() ([]byte, error) {
  11. return sonic.Marshal(e.error)
  12. }
  13. func (e BasicError[T]) StatusCode() int {
  14. return e.statusCode
  15. }
  16. func (e BasicError[T]) Error() string {
  17. return fmt.Sprintf("status code: %d, error: %v", e.statusCode, e.error)
  18. }
  19. func NewError[T any](statusCode int, err T) Error {
  20. return BasicError[T]{
  21. error: err,
  22. statusCode: statusCode,
  23. }
  24. }