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