blob: 0a3f07646d35128c01078a3453d51733b93b23a5 [file] [log] [blame]
Rohan Agrawalc32d9932020-06-15 11:01:47 +00001/*
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
20package thrift
21
22import (
23 "errors"
24 "io"
25)
26
27type timeoutable interface {
28 Timeout() bool
29}
30
31// Thrift Transport exception
32type TTransportException interface {
33 TException
34 TypeId() int
35 Err() error
36}
37
38const (
39 UNKNOWN_TRANSPORT_EXCEPTION = 0
40 NOT_OPEN = 1
41 ALREADY_OPEN = 2
42 TIMED_OUT = 3
43 END_OF_FILE = 4
44)
45
46type tTransportException struct {
47 typeId int
48 err error
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000049 msg string
50}
51
52var _ TTransportException = (*tTransportException)(nil)
53
54func (tTransportException) TExceptionType() TExceptionType {
55 return TExceptionTypeTransport
Rohan Agrawalc32d9932020-06-15 11:01:47 +000056}
57
58func (p *tTransportException) TypeId() int {
59 return p.typeId
60}
61
62func (p *tTransportException) Error() string {
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000063 return p.msg
Rohan Agrawalc32d9932020-06-15 11:01:47 +000064}
65
66func (p *tTransportException) Err() error {
67 return p.err
68}
69
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000070func (p *tTransportException) Unwrap() error {
71 return p.err
72}
73
74func (p *tTransportException) Timeout() bool {
75 return p.typeId == TIMED_OUT
76}
77
Rohan Agrawalc32d9932020-06-15 11:01:47 +000078func NewTTransportException(t int, e string) TTransportException {
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000079 return &tTransportException{
80 typeId: t,
81 err: errors.New(e),
82 msg: e,
83 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000084}
85
86func 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
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000095 te := &tTransportException{
96 typeId: UNKNOWN_TRANSPORT_EXCEPTION,
97 err: e,
98 msg: e.Error(),
Rohan Agrawalc32d9932020-06-15 11:01:47 +000099 }
100
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +0000101 if isTimeoutError(e) {
102 te.typeId = TIMED_OUT
103 return te
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000104 }
105
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +0000106 if errors.Is(e, io.EOF) {
107 te.typeId = END_OF_FILE
108 return te
109 }
110
111 return te
112}
113
114func 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.
125func isTimeoutError(err error) bool {
126 var t timeoutable
127 if errors.As(err, &t) {
128 return t.Timeout()
129 }
130 return false
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000131}