error.go 823 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package httpclient
  2. var _ ReplyErr = (*replyErr)(nil)
  3. // ReplyErr 错误响应,当 resp.StatusCode != http.StatusOK 时用来包装返回的 httpcode 和 body 。
  4. type ReplyErr interface {
  5. error
  6. StatusCode() int
  7. Body() []byte
  8. }
  9. type replyErr struct {
  10. err error
  11. statusCode int
  12. body []byte
  13. }
  14. func (r *replyErr) Error() string {
  15. return r.err.Error()
  16. }
  17. func (r *replyErr) StatusCode() int {
  18. return r.statusCode
  19. }
  20. func (r *replyErr) Body() []byte {
  21. return r.body
  22. }
  23. func newReplyErr(statusCode int, body []byte, err error) ReplyErr {
  24. return &replyErr{
  25. statusCode: statusCode,
  26. body: body,
  27. err: err,
  28. }
  29. }
  30. // ToReplyErr 尝试将 err 转换为 ReplyErr
  31. func ToReplyErr(err error) (ReplyErr, bool) {
  32. if err == nil {
  33. return nil, false
  34. }
  35. e, ok := err.(ReplyErr)
  36. return e, ok
  37. }