client_error.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "fmt"
  16. const (
  17. DefaultClientErrorStatus = 400
  18. DefaultClientErrorCode = "SDK.ClientError"
  19. UnsupportedCredentialErrorCode = "SDK.UnsupportedCredential"
  20. UnsupportedCredentialErrorMessage = "Specified credential (type = %s) is not supported, please check"
  21. r
  22. CanNotResolveEndpointErrorCode = "SDK.CanNotResolveEndpoint"
  23. CanNotResolveEndpointErrorMessage = "Can not resolve endpoint(param = %s), please check your accessKey with secret, and read the user guide\n %s"
  24. UnsupportedParamPositionErrorCode = "SDK.UnsupportedParamPosition"
  25. UnsupportedParamPositionErrorMessage = "Specified param position (%s) is not supported, please upgrade sdk and retry"
  26. AsyncFunctionNotEnabledCode = "SDK.AsyncFunctionNotEnabled"
  27. AsyncFunctionNotEnabledMessage = "Async function is not enabled in client, please invoke 'client.EnableAsync' function"
  28. UnknownRequestTypeErrorCode = "SDK.UnknownRequestType"
  29. UnknownRequestTypeErrorMessage = "Unknown Request Type: %s"
  30. MissingParamErrorCode = "SDK.MissingParam"
  31. InvalidParamErrorCode = "SDK.InvalidParam"
  32. JsonUnmarshalErrorCode = "SDK.JsonUnmarshalError"
  33. JsonUnmarshalErrorMessage = "Failed to unmarshal response, but you can get the data via response.GetHttpStatusCode() and response.GetHttpContentString()"
  34. TimeoutErrorCode = "SDK.TimeoutError"
  35. TimeoutErrorMessage = "The request timed out %s times(%s for retry), perhaps we should have the threshold raised a little?"
  36. )
  37. type ClientError struct {
  38. errorCode string
  39. message string
  40. originError error
  41. }
  42. func NewClientError(errorCode, message string, originErr error) Error {
  43. return &ClientError{
  44. errorCode: errorCode,
  45. message: message,
  46. originError: originErr,
  47. }
  48. }
  49. func (err *ClientError) Error() string {
  50. clientErrMsg := fmt.Sprintf("[%s] %s", err.errorCode, err.message)
  51. if err.originError != nil {
  52. return clientErrMsg + "\ncaused by:\n" + err.originError.Error()
  53. }
  54. return clientErrMsg
  55. }
  56. func (err *ClientError) OriginError() error {
  57. return err.originError
  58. }
  59. func (*ClientError) HttpStatus() int {
  60. return DefaultClientErrorStatus
  61. }
  62. func (err *ClientError) ErrorCode() string {
  63. if err.errorCode == "" {
  64. return DefaultClientErrorCode
  65. } else {
  66. return err.errorCode
  67. }
  68. }
  69. func (err *ClientError) Message() string {
  70. return err.message
  71. }
  72. func (err *ClientError) String() string {
  73. return err.Error()
  74. }