Merge pull request #98 from yunify/add-unpacker-reflect-check

add unpacker reflect check
This commit is contained in:
roger 2018-06-22 15:21:47 +08:00 committed by GitHub
commit 8afb238210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 4 deletions

View File

@ -71,6 +71,10 @@ func (u *Unpacker) parseResponse() error {
if err != nil {
return err
}
if !u.output.IsValid() {
return fmt.Errorf("API Gateway output format error")
}
}
}
@ -78,18 +82,23 @@ func (u *Unpacker) parseResponse() error {
}
func (u *Unpacker) parseError() error {
retCodeValue := u.output.Elem().FieldByName("RetCode")
messageValue := u.output.Elem().FieldByName("Message")
if retCodeValue.Elem().IsValid() && retCodeValue.Type().String() == "*int" &&
messageValue.Elem().IsValid() && messageValue.Type().String() == "*string" &&
retCodeValue.Elem().Int() != 0 {
if !retCodeValue.Elem().IsValid() || retCodeValue.Type().String() != "*int" {
return fmt.Errorf("can not get retcode")
}
if retCodeValue.Elem().Int() != 0 {
if !messageValue.Elem().IsValid() || messageValue.Type().String() != "*string" {
return fmt.Errorf("can not get error message")
}
return &errors.QingCloudError{
RetCode: int(retCodeValue.Elem().Int()),
Message: messageValue.Elem().String(),
}
}
return nil
}