client_error.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. )
  32. type ClientError struct {
  33. errorCode string
  34. message string
  35. originError error
  36. }
  37. func NewClientError(errorCode, message string, originErr error) Error {
  38. return &ClientError{
  39. errorCode: errorCode,
  40. message: message,
  41. originError: originErr,
  42. }
  43. }
  44. func (err *ClientError) Error() string {
  45. if err.originError != nil {
  46. return err.originError.Error()
  47. }
  48. return fmt.Sprintf("[%s] %s", err.errorCode, err.message)
  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. }