mpagenko | af80163 | 2020-07-03 10:00:42 +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 | "errors" |
| 24 | "io" |
| 25 | ) |
| 26 | |
| 27 | type timeoutable interface { |
| 28 | Timeout() bool |
| 29 | } |
| 30 | |
| 31 | // Thrift Transport exception |
| 32 | type TTransportException interface { |
| 33 | TException |
| 34 | TypeId() int |
| 35 | Err() error |
| 36 | } |
| 37 | |
| 38 | const ( |
| 39 | UNKNOWN_TRANSPORT_EXCEPTION = 0 |
| 40 | NOT_OPEN = 1 |
| 41 | ALREADY_OPEN = 2 |
| 42 | TIMED_OUT = 3 |
| 43 | END_OF_FILE = 4 |
| 44 | ) |
| 45 | |
| 46 | type tTransportException struct { |
| 47 | typeId int |
| 48 | err error |
| 49 | } |
| 50 | |
| 51 | func (p *tTransportException) TypeId() int { |
| 52 | return p.typeId |
| 53 | } |
| 54 | |
| 55 | func (p *tTransportException) Error() string { |
| 56 | return p.err.Error() |
| 57 | } |
| 58 | |
| 59 | func (p *tTransportException) Err() error { |
| 60 | return p.err |
| 61 | } |
| 62 | |
| 63 | func NewTTransportException(t int, e string) TTransportException { |
| 64 | return &tTransportException{typeId: t, err: errors.New(e)} |
| 65 | } |
| 66 | |
| 67 | func NewTTransportExceptionFromError(e error) TTransportException { |
| 68 | if e == nil { |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | if t, ok := e.(TTransportException); ok { |
| 73 | return t |
| 74 | } |
| 75 | |
| 76 | switch v := e.(type) { |
| 77 | case TTransportException: |
| 78 | return v |
| 79 | case timeoutable: |
| 80 | if v.Timeout() { |
| 81 | return &tTransportException{typeId: TIMED_OUT, err: e} |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if e == io.EOF { |
| 86 | return &tTransportException{typeId: END_OF_FILE, err: e} |
| 87 | } |
| 88 | |
| 89 | return &tTransportException{typeId: UNKNOWN_TRANSPORT_EXCEPTION, err: e} |
| 90 | } |