application_exception.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import (
  21. "context"
  22. )
  23. const (
  24. UNKNOWN_APPLICATION_EXCEPTION = 0
  25. UNKNOWN_METHOD = 1
  26. INVALID_MESSAGE_TYPE_EXCEPTION = 2
  27. WRONG_METHOD_NAME = 3
  28. BAD_SEQUENCE_ID = 4
  29. MISSING_RESULT = 5
  30. INTERNAL_ERROR = 6
  31. PROTOCOL_ERROR = 7
  32. INVALID_TRANSFORM = 8
  33. INVALID_PROTOCOL = 9
  34. UNSUPPORTED_CLIENT_TYPE = 10
  35. )
  36. var defaultApplicationExceptionMessage = map[int32]string{
  37. UNKNOWN_APPLICATION_EXCEPTION: "unknown application exception",
  38. UNKNOWN_METHOD: "unknown method",
  39. INVALID_MESSAGE_TYPE_EXCEPTION: "invalid message type",
  40. WRONG_METHOD_NAME: "wrong method name",
  41. BAD_SEQUENCE_ID: "bad sequence ID",
  42. MISSING_RESULT: "missing result",
  43. INTERNAL_ERROR: "unknown internal error",
  44. PROTOCOL_ERROR: "unknown protocol error",
  45. INVALID_TRANSFORM: "Invalid transform",
  46. INVALID_PROTOCOL: "Invalid protocol",
  47. UNSUPPORTED_CLIENT_TYPE: "Unsupported client type",
  48. }
  49. // Application level Thrift exception
  50. type TApplicationException interface {
  51. TException
  52. TypeId() int32
  53. Read(ctx context.Context, iprot TProtocol) error
  54. Write(ctx context.Context, oprot TProtocol) error
  55. }
  56. type tApplicationException struct {
  57. message string
  58. type_ int32
  59. }
  60. var _ TApplicationException = (*tApplicationException)(nil)
  61. func (tApplicationException) TExceptionType() TExceptionType {
  62. return TExceptionTypeApplication
  63. }
  64. func (e tApplicationException) Error() string {
  65. if e.message != "" {
  66. return e.message
  67. }
  68. return defaultApplicationExceptionMessage[e.type_]
  69. }
  70. func NewTApplicationException(type_ int32, message string) TApplicationException {
  71. return &tApplicationException{message, type_}
  72. }
  73. func (p *tApplicationException) TypeId() int32 {
  74. return p.type_
  75. }
  76. func (p *tApplicationException) Read(ctx context.Context, iprot TProtocol) error {
  77. // TODO: this should really be generated by the compiler
  78. _, err := iprot.ReadStructBegin(ctx)
  79. if err != nil {
  80. return err
  81. }
  82. message := ""
  83. type_ := int32(UNKNOWN_APPLICATION_EXCEPTION)
  84. for {
  85. _, ttype, id, err := iprot.ReadFieldBegin(ctx)
  86. if err != nil {
  87. return err
  88. }
  89. if ttype == STOP {
  90. break
  91. }
  92. switch id {
  93. case 1:
  94. if ttype == STRING {
  95. if message, err = iprot.ReadString(ctx); err != nil {
  96. return err
  97. }
  98. } else {
  99. if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
  100. return err
  101. }
  102. }
  103. case 2:
  104. if ttype == I32 {
  105. if type_, err = iprot.ReadI32(ctx); err != nil {
  106. return err
  107. }
  108. } else {
  109. if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
  110. return err
  111. }
  112. }
  113. default:
  114. if err = SkipDefaultDepth(ctx, iprot, ttype); err != nil {
  115. return err
  116. }
  117. }
  118. if err = iprot.ReadFieldEnd(ctx); err != nil {
  119. return err
  120. }
  121. }
  122. if err := iprot.ReadStructEnd(ctx); err != nil {
  123. return err
  124. }
  125. p.message = message
  126. p.type_ = type_
  127. return nil
  128. }
  129. func (p *tApplicationException) Write(ctx context.Context, oprot TProtocol) (err error) {
  130. err = oprot.WriteStructBegin(ctx, "TApplicationException")
  131. if len(p.Error()) > 0 {
  132. err = oprot.WriteFieldBegin(ctx, "message", STRING, 1)
  133. if err != nil {
  134. return
  135. }
  136. err = oprot.WriteString(ctx, p.Error())
  137. if err != nil {
  138. return
  139. }
  140. err = oprot.WriteFieldEnd(ctx)
  141. if err != nil {
  142. return
  143. }
  144. }
  145. err = oprot.WriteFieldBegin(ctx, "type", I32, 2)
  146. if err != nil {
  147. return
  148. }
  149. err = oprot.WriteI32(ctx, p.type_)
  150. if err != nil {
  151. return
  152. }
  153. err = oprot.WriteFieldEnd(ctx)
  154. if err != nil {
  155. return
  156. }
  157. err = oprot.WriteFieldStop(ctx)
  158. if err != nil {
  159. return
  160. }
  161. err = oprot.WriteStructEnd(ctx)
  162. return
  163. }