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