khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [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 | import ( |
| 23 | "encoding/base64" |
| 24 | ) |
| 25 | |
| 26 | // Thrift Protocol exception |
| 27 | type TProtocolException interface { |
| 28 | TException |
| 29 | TypeId() int |
| 30 | } |
| 31 | |
| 32 | const ( |
| 33 | UNKNOWN_PROTOCOL_EXCEPTION = 0 |
| 34 | INVALID_DATA = 1 |
| 35 | NEGATIVE_SIZE = 2 |
| 36 | SIZE_LIMIT = 3 |
| 37 | BAD_VERSION = 4 |
| 38 | NOT_IMPLEMENTED = 5 |
| 39 | DEPTH_LIMIT = 6 |
| 40 | ) |
| 41 | |
| 42 | type tProtocolException struct { |
| 43 | typeId int |
| 44 | message string |
| 45 | } |
| 46 | |
| 47 | func (p *tProtocolException) TypeId() int { |
| 48 | return p.typeId |
| 49 | } |
| 50 | |
| 51 | func (p *tProtocolException) String() string { |
| 52 | return p.message |
| 53 | } |
| 54 | |
| 55 | func (p *tProtocolException) Error() string { |
| 56 | return p.message |
| 57 | } |
| 58 | |
| 59 | func NewTProtocolException(err error) TProtocolException { |
| 60 | if err == nil { |
| 61 | return nil |
| 62 | } |
| 63 | if e,ok := err.(TProtocolException); ok { |
| 64 | return e |
| 65 | } |
| 66 | if _, ok := err.(base64.CorruptInputError); ok { |
| 67 | return &tProtocolException{INVALID_DATA, err.Error()} |
| 68 | } |
| 69 | return &tProtocolException{UNKNOWN_PROTOCOL_EXCEPTION, err.Error()} |
| 70 | } |
| 71 | |
| 72 | func NewTProtocolExceptionWithType(errType int, err error) TProtocolException { |
| 73 | if err == nil { |
| 74 | return nil |
| 75 | } |
| 76 | return &tProtocolException{errType, err.Error()} |
| 77 | } |
| 78 | |