| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 20 | package thrift |
| 21 | |
| 22 | const ( |
| 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' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 31 | INVALID_TRANSFORM = 8 |
| 32 | INVALID_PROTOCOL = 9 |
| 33 | UNSUPPORTED_CLIENT_TYPE = 10 |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 34 | ) |
| 35 | |
| damnever | 1b20b18 | 2017-09-05 13:14:06 +0800 | [diff] [blame] | 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", |
| Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 45 | INVALID_TRANSFORM: "Invalid transform", |
| 46 | INVALID_PROTOCOL: "Invalid protocol", |
| 47 | UNSUPPORTED_CLIENT_TYPE: "Unsupported client type", |
| damnever | 1b20b18 | 2017-09-05 13:14:06 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 50 | // Application level Thrift exception |
| 51 | type TApplicationException interface { |
| 52 | TException |
| 53 | TypeId() int32 |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 54 | Read(iprot TProtocol) error |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 55 | Write(oprot TProtocol) error |
| 56 | } |
| 57 | |
| 58 | type tApplicationException struct { |
| 59 | message string |
| 60 | type_ int32 |
| 61 | } |
| 62 | |
| 63 | func (e tApplicationException) Error() string { |
| damnever | 1b20b18 | 2017-09-05 13:14:06 +0800 | [diff] [blame] | 64 | if e.message != "" { |
| 65 | return e.message |
| 66 | } |
| 67 | return defaultApplicationExceptionMessage[e.type_] |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | func NewTApplicationException(type_ int32, message string) TApplicationException { |
| 71 | return &tApplicationException{message, type_} |
| 72 | } |
| 73 | |
| 74 | func (p *tApplicationException) TypeId() int32 { |
| 75 | return p.type_ |
| 76 | } |
| 77 | |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 78 | func (p *tApplicationException) Read(iprot TProtocol) error { |
| 79 | // TODO: this should really be generated by the compiler |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 80 | _, err := iprot.ReadStructBegin() |
| 81 | if err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 82 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | message := "" |
| 86 | type_ := int32(UNKNOWN_APPLICATION_EXCEPTION) |
| 87 | |
| 88 | for { |
| 89 | _, ttype, id, err := iprot.ReadFieldBegin() |
| 90 | if err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 91 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 92 | } |
| 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 Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 100 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 101 | } |
| 102 | } else { |
| 103 | if err = SkipDefaultDepth(iprot, ttype); err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 104 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | case 2: |
| 108 | if ttype == I32 { |
| 109 | if type_, err = iprot.ReadI32(); err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 110 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 111 | } |
| 112 | } else { |
| 113 | if err = SkipDefaultDepth(iprot, ttype); err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 114 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | default: |
| 118 | if err = SkipDefaultDepth(iprot, ttype); err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 119 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | if err = iprot.ReadFieldEnd(); err != nil { |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 123 | return err |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame] | 126 | if err := iprot.ReadStructEnd(); err != nil { |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | p.message = message |
| 131 | p.type_ = type_ |
| 132 | |
| 133 | return nil |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func (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 | } |