server_error.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package errors
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/jmespath/go-jmespath"
  19. )
  20. var wrapperList = []ServerErrorWrapper{
  21. &SignatureDostNotMatchWrapper{},
  22. }
  23. type ServerError struct {
  24. httpStatus int
  25. requestId string
  26. hostId string
  27. errorCode string
  28. recommend string
  29. message string
  30. comment string
  31. }
  32. type ServerErrorWrapper interface {
  33. tryWrap(error *ServerError, wrapInfo map[string]string) (bool, *ServerError)
  34. }
  35. func (err *ServerError) Error() string {
  36. return fmt.Sprintf("SDK.ServerError\nErrorCode: %s\nRecommend: %s\nRequestId: %s\nMessage: %s",
  37. err.errorCode, err.comment+err.recommend, err.requestId, err.message)
  38. }
  39. func NewServerError(httpStatus int, responseContent, comment string) Error {
  40. result := &ServerError{
  41. httpStatus: httpStatus,
  42. message: responseContent,
  43. comment: comment,
  44. }
  45. var data interface{}
  46. err := json.Unmarshal([]byte(responseContent), &data)
  47. if err == nil {
  48. requestId, _ := jmespath.Search("RequestId", data)
  49. hostId, _ := jmespath.Search("HostId", data)
  50. errorCode, _ := jmespath.Search("Code", data)
  51. recommend, _ := jmespath.Search("Recommend", data)
  52. message, _ := jmespath.Search("Message", data)
  53. if requestId != nil {
  54. result.requestId = requestId.(string)
  55. }
  56. if hostId != nil {
  57. result.hostId = hostId.(string)
  58. }
  59. if errorCode != nil {
  60. result.errorCode = errorCode.(string)
  61. }
  62. if recommend != nil {
  63. result.recommend = recommend.(string)
  64. }
  65. if message != nil {
  66. result.message = message.(string)
  67. }
  68. }
  69. return result
  70. }
  71. func WrapServerError(originError *ServerError, wrapInfo map[string]string) *ServerError {
  72. for _, wrapper := range wrapperList {
  73. ok, newError := wrapper.tryWrap(originError, wrapInfo)
  74. if ok {
  75. return newError
  76. }
  77. }
  78. return originError
  79. }
  80. func (err *ServerError) HttpStatus() int {
  81. return err.httpStatus
  82. }
  83. func (err *ServerError) ErrorCode() string {
  84. return err.errorCode
  85. }
  86. func (err *ServerError) Message() string {
  87. return err.message
  88. }
  89. func (err *ServerError) OriginError() error {
  90. return nil
  91. }
  92. func (err *ServerError) HostId() string {
  93. return err.hostId
  94. }
  95. func (err *ServerError) RequestId() string {
  96. return err.requestId
  97. }
  98. func (err *ServerError) Recommend() string {
  99. return err.recommend
  100. }
  101. func (err *ServerError) Comment() string {
  102. return err.comment
  103. }