client_error.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. const (
  16. DefaultClientErrorStatus = 400
  17. DefaultClientErrorCode = "SDK.ClientError"
  18. UnsupportedCredentialCode = "SDK.UnsupportedCredential"
  19. UnsupportedCredentialMessage = "Specified credential (type = %s) is not supported, please check"
  20. CanNotResolveEndpointCode = "SDK.CanNotResolveEndpoint"
  21. CanNotResolveEndpointMessage = "Can not resolve endpoint(param = %s), please check the user guide\n %s"
  22. UnsupportedParamPositionCode = "SDK.UnsupportedParamPosition"
  23. UnsupportedParamPositionMessage = "Specified param position (%s) is not supported, please upgrade sdk and retry"
  24. AsyncFunctionNotEnabledCode = "SDK.AsyncFunctionNotEnabled"
  25. AsyncFunctionNotEnabledMessage = "Async function is not enabled in client, please invoke 'client.EnableAsync' function"
  26. UnknownRequestTypeCode = "SDK.UnknownRequestType"
  27. UnknownRequestTypeMessage = "Unknown Request Type: %s"
  28. MissingParamCode = "SDK.MissingParam"
  29. InvalidParamCode = "SDK.InvalidParam"
  30. )
  31. type ClientError struct {
  32. errorCode string
  33. message string
  34. originError error
  35. }
  36. func NewClientError(errorCode, message string, originErr error) Error {
  37. return &ClientError{
  38. errorCode: errorCode,
  39. message: message,
  40. originError: originErr,
  41. }
  42. }
  43. func (err *ClientError) Error() string {
  44. if err.originError != nil {
  45. return err.originError.Error()
  46. } else {
  47. return ""
  48. }
  49. }
  50. func (err *ClientError) OriginError() error {
  51. return err.originError
  52. }
  53. func (*ClientError) HttpStatus() int {
  54. return DefaultClientErrorStatus
  55. }
  56. func (err *ClientError) ErrorCode() string {
  57. if err.errorCode == "" {
  58. return DefaultClientErrorCode
  59. } else {
  60. return err.errorCode
  61. }
  62. }
  63. func (err *ClientError) Message() string {
  64. return err.message
  65. }