blob: 0023c57cf1e0d381ad892a344baa867cf06086ed [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001/*
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
20package thrift
21
22const (
23 UNKNOWN_APPLICATION_EXCEPTION = 0
24 UNKNOWN_METHOD = 1
25 INVALID_MESSAGE_TYPE_EXCEPTION = 2
26 WRONG_METHOD_NAME = 3
27 BAD_SEQUENCE_ID = 4
28 MISSING_RESULT = 5
29 INTERNAL_ERROR = 6
30 PROTOCOL_ERROR = 7
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080031 INVALID_TRANSFORM = 8
32 INVALID_PROTOCOL = 9
33 UNSUPPORTED_CLIENT_TYPE = 10
Jens Geyer0e87c462013-06-18 22:25:07 +020034)
35
damnever1b20b182017-09-05 13:14:06 +080036var 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",
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080045 INVALID_TRANSFORM: "Invalid transform",
46 INVALID_PROTOCOL: "Invalid protocol",
47 UNSUPPORTED_CLIENT_TYPE: "Unsupported client type",
damnever1b20b182017-09-05 13:14:06 +080048}
49
Jens Geyer0e87c462013-06-18 22:25:07 +020050// Application level Thrift exception
51type TApplicationException interface {
52 TException
53 TypeId() int32
D. Can Celasun4f77ab82017-09-21 15:21:00 +020054 Read(iprot TProtocol) error
Jens Geyer0e87c462013-06-18 22:25:07 +020055 Write(oprot TProtocol) error
56}
57
58type tApplicationException struct {
59 message string
60 type_ int32
61}
62
63func (e tApplicationException) Error() string {
damnever1b20b182017-09-05 13:14:06 +080064 if e.message != "" {
65 return e.message
66 }
67 return defaultApplicationExceptionMessage[e.type_]
Jens Geyer0e87c462013-06-18 22:25:07 +020068}
69
70func NewTApplicationException(type_ int32, message string) TApplicationException {
71 return &tApplicationException{message, type_}
72}
73
74func (p *tApplicationException) TypeId() int32 {
75 return p.type_
76}
77
D. Can Celasun4f77ab82017-09-21 15:21:00 +020078func (p *tApplicationException) Read(iprot TProtocol) error {
79 // TODO: this should really be generated by the compiler
Jens Geyer0e87c462013-06-18 22:25:07 +020080 _, err := iprot.ReadStructBegin()
81 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020082 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020083 }
84
85 message := ""
86 type_ := int32(UNKNOWN_APPLICATION_EXCEPTION)
87
88 for {
89 _, ttype, id, err := iprot.ReadFieldBegin()
90 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020091 return err
Jens Geyer0e87c462013-06-18 22:25:07 +020092 }
93 if ttype == STOP {
94 break
95 }
96 switch id {
97 case 1:
98 if ttype == STRING {
99 if message, err = iprot.ReadString(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200100 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200101 }
102 } else {
103 if err = SkipDefaultDepth(iprot, ttype); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200104 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200105 }
106 }
107 case 2:
108 if ttype == I32 {
109 if type_, err = iprot.ReadI32(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200110 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200111 }
112 } else {
113 if err = SkipDefaultDepth(iprot, ttype); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200114 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200115 }
116 }
117 default:
118 if err = SkipDefaultDepth(iprot, ttype); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200119 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200120 }
121 }
122 if err = iprot.ReadFieldEnd(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200123 return err
Jens Geyer0e87c462013-06-18 22:25:07 +0200124 }
125 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200126 if err := iprot.ReadStructEnd(); err != nil {
127 return err
128 }
129
130 p.message = message
131 p.type_ = type_
132
133 return nil
Jens Geyer0e87c462013-06-18 22:25:07 +0200134}
135
136func (p *tApplicationException) Write(oprot TProtocol) (err error) {
137 err = oprot.WriteStructBegin("TApplicationException")
138 if len(p.Error()) > 0 {
139 err = oprot.WriteFieldBegin("message", STRING, 1)
140 if err != nil {
141 return
142 }
143 err = oprot.WriteString(p.Error())
144 if err != nil {
145 return
146 }
147 err = oprot.WriteFieldEnd()
148 if err != nil {
149 return
150 }
151 }
152 err = oprot.WriteFieldBegin("type", I32, 2)
153 if err != nil {
154 return
155 }
156 err = oprot.WriteI32(p.type_)
157 if err != nil {
158 return
159 }
160 err = oprot.WriteFieldEnd()
161 if err != nil {
162 return
163 }
164 err = oprot.WriteFieldStop()
165 if err != nil {
166 return
167 }
168 err = oprot.WriteStructEnd()
169 return
170}