client_error.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. UnsupportedCredentialCode = "SDK.UnsupportedCredential"
  20. UnsupportedCredentialMessage = "Specified credential (type = %s) is not supported, please check"
  21. CanNotResolveEndpointCode = "SDK.CanNotResolveEndpoint"
  22. CanNotResolveEndpointMessage = "Can not resolve endpoint(param = %s), please check the user guide\n %s"
  23. UnsupportedParamPositionCode = "SDK.UnsupportedParamPosition"
  24. UnsupportedParamPositionMessage = "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. UnknownRequestTypeCode = "SDK.UnknownRequestType"
  28. UnknownRequestTypeMessage = "Unknown Request Type: %s"
  29. MissingParamCode = "SDK.MissingParam"
  30. InvalidParamCode = "SDK.InvalidParam"
  31. JsonUnmarshalCode = "SDK.JsonUnmarshalError"
  32. JsonUnmarshalMessage = "Failed to unmarshal response, but you can get the data via response.GetHttpStatusCode() and response.GetHttpContentString()"
  33. )
  34. type ClientError struct {
  35. errorCode string
  36. message string
  37. originError error
  38. }
  39. func NewClientError(errorCode, message string, originErr error) Error {
  40. return &ClientError{
  41. errorCode: errorCode,
  42. message: message,
  43. originError: originErr,
  44. }
  45. }
  46. func (err *ClientError) Error() string {
  47. clientErrMsg := fmt.Sprintf("[%s] %s", err.errorCode, err.message)
  48. if err.originError != nil {
  49. return clientErrMsg + "\noriginal error:\n" + err.originError.Error()
  50. }
  51. return clientErrMsg
  52. }
  53. func (err *ClientError) OriginError() error {
  54. return err.originError
  55. }
  56. func (*ClientError) HttpStatus() int {
  57. return DefaultClientErrorStatus
  58. }
  59. func (err *ClientError) ErrorCode() string {
  60. if err.errorCode == "" {
  61. return DefaultClientErrorCode
  62. } else {
  63. return err.errorCode
  64. }
  65. }
  66. func (err *ClientError) Message() string {
  67. return err.message
  68. }