Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [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 | msg string |
| 50 | } |
| 51 | |
| 52 | var _ TTransportException = (*tTransportException)(nil) |
| 53 | |
| 54 | func (tTransportException) TExceptionType() TExceptionType { |
| 55 | return TExceptionTypeTransport |
| 56 | } |
| 57 | |
| 58 | func (p *tTransportException) TypeId() int { |
| 59 | return p.typeId |
| 60 | } |
| 61 | |
| 62 | func (p *tTransportException) Error() string { |
| 63 | return p.msg |
| 64 | } |
| 65 | |
| 66 | func (p *tTransportException) Err() error { |
| 67 | return p.err |
| 68 | } |
| 69 | |
| 70 | func (p *tTransportException) Unwrap() error { |
| 71 | return p.err |
| 72 | } |
| 73 | |
| 74 | func (p *tTransportException) Timeout() bool { |
| 75 | return p.typeId == TIMED_OUT |
| 76 | } |
| 77 | |
| 78 | func NewTTransportException(t int, e string) TTransportException { |
| 79 | return &tTransportException{ |
| 80 | typeId: t, |
| 81 | err: errors.New(e), |
| 82 | msg: e, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func NewTTransportExceptionFromError(e error) TTransportException { |
| 87 | if e == nil { |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | if t, ok := e.(TTransportException); ok { |
| 92 | return t |
| 93 | } |
| 94 | |
| 95 | te := &tTransportException{ |
| 96 | typeId: UNKNOWN_TRANSPORT_EXCEPTION, |
| 97 | err: e, |
| 98 | msg: e.Error(), |
| 99 | } |
| 100 | |
| 101 | if isTimeoutError(e) { |
| 102 | te.typeId = TIMED_OUT |
| 103 | return te |
| 104 | } |
| 105 | |
| 106 | if errors.Is(e, io.EOF) { |
| 107 | te.typeId = END_OF_FILE |
| 108 | return te |
| 109 | } |
| 110 | |
| 111 | return te |
| 112 | } |
| 113 | |
| 114 | func prependTTransportException(prepend string, e TTransportException) TTransportException { |
| 115 | return &tTransportException{ |
| 116 | typeId: e.TypeId(), |
| 117 | err: e, |
| 118 | msg: prepend + e.Error(), |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // isTimeoutError returns true when err is an error caused by timeout. |
| 123 | // |
| 124 | // Note that this also includes TTransportException wrapped timeout errors. |
| 125 | func isTimeoutError(err error) bool { |
| 126 | var t timeoutable |
| 127 | if errors.As(err, &t) { |
| 128 | return t.Timeout() |
| 129 | } |
| 130 | return false |
| 131 | } |