Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +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 | |
| 22 | import ( |
| 23 | "encoding/base64" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 24 | "errors" |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // Thrift Protocol exception |
| 28 | type TProtocolException interface { |
| 29 | TException |
| 30 | TypeId() int |
| 31 | } |
| 32 | |
| 33 | const ( |
| 34 | UNKNOWN_PROTOCOL_EXCEPTION = 0 |
| 35 | INVALID_DATA = 1 |
| 36 | NEGATIVE_SIZE = 2 |
| 37 | SIZE_LIMIT = 3 |
| 38 | BAD_VERSION = 4 |
| 39 | NOT_IMPLEMENTED = 5 |
| 40 | DEPTH_LIMIT = 6 |
| 41 | ) |
| 42 | |
| 43 | type tProtocolException struct { |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 44 | typeId int |
| 45 | err error |
| 46 | msg string |
| 47 | } |
| 48 | |
| 49 | var _ TProtocolException = (*tProtocolException)(nil) |
| 50 | |
| 51 | func (tProtocolException) TExceptionType() TExceptionType { |
| 52 | return TExceptionTypeProtocol |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func (p *tProtocolException) TypeId() int { |
| 56 | return p.typeId |
| 57 | } |
| 58 | |
| 59 | func (p *tProtocolException) String() string { |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 60 | return p.msg |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (p *tProtocolException) Error() string { |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 64 | return p.msg |
| 65 | } |
| 66 | |
| 67 | func (p *tProtocolException) Unwrap() error { |
| 68 | return p.err |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func NewTProtocolException(err error) TProtocolException { |
| 72 | if err == nil { |
| 73 | return nil |
| 74 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 75 | |
| 76 | if e, ok := err.(TProtocolException); ok { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 77 | return e |
| 78 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 79 | |
| 80 | if errors.As(err, new(base64.CorruptInputError)) { |
| 81 | return NewTProtocolExceptionWithType(INVALID_DATA, err) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 82 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 83 | |
| 84 | return NewTProtocolExceptionWithType(UNKNOWN_PROTOCOL_EXCEPTION, err) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func NewTProtocolExceptionWithType(errType int, err error) TProtocolException { |
| 88 | if err == nil { |
| 89 | return nil |
| 90 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 91 | return &tProtocolException{ |
| 92 | typeId: errType, |
| 93 | err: err, |
| 94 | msg: err.Error(), |
| 95 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 96 | } |
| 97 | |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 98 | func prependTProtocolException(prepend string, err TProtocolException) TProtocolException { |
| 99 | return &tProtocolException{ |
| 100 | typeId: err.TypeId(), |
| 101 | err: err, |
| 102 | msg: prepend + err.Error(), |
| 103 | } |
| 104 | } |