application_exception.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. const (
  21. UNKNOWN_APPLICATION_EXCEPTION = 0
  22. UNKNOWN_METHOD = 1
  23. INVALID_MESSAGE_TYPE_EXCEPTION = 2
  24. WRONG_METHOD_NAME = 3
  25. BAD_SEQUENCE_ID = 4
  26. MISSING_RESULT = 5
  27. INTERNAL_ERROR = 6
  28. PROTOCOL_ERROR = 7
  29. )
  30. // Application level Thrift exception
  31. type TApplicationException interface {
  32. TException
  33. TypeId() int32
  34. Read(iprot TProtocol) (TApplicationException, error)
  35. Write(oprot TProtocol) error
  36. }
  37. type tApplicationException struct {
  38. message string
  39. type_ int32
  40. }
  41. func (e tApplicationException) Error() string {
  42. return e.message
  43. }
  44. func NewTApplicationException(type_ int32, message string) TApplicationException {
  45. return &tApplicationException{message, type_}
  46. }
  47. func (p *tApplicationException) TypeId() int32 {
  48. return p.type_
  49. }
  50. func (p *tApplicationException) Read(iprot TProtocol) (TApplicationException, error) {
  51. _, err := iprot.ReadStructBegin()
  52. if err != nil {
  53. return nil, err
  54. }
  55. message := ""
  56. type_ := int32(UNKNOWN_APPLICATION_EXCEPTION)
  57. for {
  58. _, ttype, id, err := iprot.ReadFieldBegin()
  59. if err != nil {
  60. return nil, err
  61. }
  62. if ttype == STOP {
  63. break
  64. }
  65. switch id {
  66. case 1:
  67. if ttype == STRING {
  68. if message, err = iprot.ReadString(); err != nil {
  69. return nil, err
  70. }
  71. } else {
  72. if err = SkipDefaultDepth(iprot, ttype); err != nil {
  73. return nil, err
  74. }
  75. }
  76. case 2:
  77. if ttype == I32 {
  78. if type_, err = iprot.ReadI32(); err != nil {
  79. return nil, err
  80. }
  81. } else {
  82. if err = SkipDefaultDepth(iprot, ttype); err != nil {
  83. return nil, err
  84. }
  85. }
  86. default:
  87. if err = SkipDefaultDepth(iprot, ttype); err != nil {
  88. return nil, err
  89. }
  90. }
  91. if err = iprot.ReadFieldEnd(); err != nil {
  92. return nil, err
  93. }
  94. }
  95. return NewTApplicationException(type_, message), iprot.ReadStructEnd()
  96. }
  97. func (p *tApplicationException) Write(oprot TProtocol) (err error) {
  98. err = oprot.WriteStructBegin("TApplicationException")
  99. if len(p.Error()) > 0 {
  100. err = oprot.WriteFieldBegin("message", STRING, 1)
  101. if err != nil {
  102. return
  103. }
  104. err = oprot.WriteString(p.Error())
  105. if err != nil {
  106. return
  107. }
  108. err = oprot.WriteFieldEnd()
  109. if err != nil {
  110. return
  111. }
  112. }
  113. err = oprot.WriteFieldBegin("type", I32, 2)
  114. if err != nil {
  115. return
  116. }
  117. err = oprot.WriteI32(p.type_)
  118. if err != nil {
  119. return
  120. }
  121. err = oprot.WriteFieldEnd()
  122. if err != nil {
  123. return
  124. }
  125. err = oprot.WriteFieldStop()
  126. if err != nil {
  127. return
  128. }
  129. err = oprot.WriteStructEnd()
  130. return
  131. }