blob: a51510ed9eb94a0696ada5e24f30570ba741ed9e [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001/*
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 (
Jens Geyer54a66362014-01-30 20:57:08 +010023 "errors"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "io"
25)
26
Jens Geyer54a66362014-01-30 20:57:08 +010027type timeoutable interface {
28 Timeout() bool
29}
30
Jens Geyer0e87c462013-06-18 22:25:07 +020031// Thrift Transport exception
32type TTransportException interface {
33 TException
34 TypeId() int
Jens Geyer54a66362014-01-30 20:57:08 +010035 Err() error
Jens Geyer0e87c462013-06-18 22:25:07 +020036}
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 {
Jens Geyer54a66362014-01-30 20:57:08 +010047 typeId int
48 err error
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -080049 msg string
Jens Geyer0e87c462013-06-18 22:25:07 +020050}
51
Yuxuan 'fishy' Wangd8312302020-12-22 09:53:58 -080052var _ TTransportException = (*tTransportException)(nil)
53
54func (tTransportException) TExceptionType() TExceptionType {
55 return TExceptionTypeTransport
56}
57
Jens Geyer0e87c462013-06-18 22:25:07 +020058func (p *tTransportException) TypeId() int {
59 return p.typeId
60}
61
62func (p *tTransportException) Error() string {
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -080063 return p.msg
Jens Geyer0e87c462013-06-18 22:25:07 +020064}
65
Jens Geyer54a66362014-01-30 20:57:08 +010066func (p *tTransportException) Err() error {
67 return p.err
68}
69
Yuxuan 'fishy' Wangbe3f7322020-05-14 00:28:44 -070070func (p *tTransportException) Unwrap() error {
71 return p.err
72}
73
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -070074func (p *tTransportException) Timeout() bool {
Yuxuan 'fishy' Wangfe3f8a12021-04-27 19:56:58 -070075 return p.typeId == TIMED_OUT || isTimeoutError(p.err)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -070076}
77
Jens Geyer54a66362014-01-30 20:57:08 +010078func NewTTransportException(t int, e string) TTransportException {
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -080079 return &tTransportException{
80 typeId: t,
81 err: errors.New(e),
82 msg: e,
83 }
Jens Geyer0e87c462013-06-18 22:25:07 +020084}
85
86func NewTTransportExceptionFromError(e error) TTransportException {
87 if e == nil {
88 return nil
89 }
Jens Geyer54a66362014-01-30 20:57:08 +010090
Jens Geyer0e87c462013-06-18 22:25:07 +020091 if t, ok := e.(TTransportException); ok {
92 return t
93 }
Jens Geyer54a66362014-01-30 20:57:08 +010094
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -080095 te := &tTransportException{
96 typeId: UNKNOWN_TRANSPORT_EXCEPTION,
97 err: e,
98 msg: e.Error(),
Jens Geyer0e87c462013-06-18 22:25:07 +020099 }
Jens Geyer54a66362014-01-30 20:57:08 +0100100
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -0800101 if isTimeoutError(e) {
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -0800102 te.typeId = TIMED_OUT
103 return te
Jens Geyer54a66362014-01-30 20:57:08 +0100104 }
105
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -0800106 if errors.Is(e, io.EOF) {
107 te.typeId = END_OF_FILE
108 return te
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -0800109 }
110
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -0800111 return te
Yuxuan 'fishy' Wang0e68e8c2021-01-19 09:14:36 -0800112}
113
114func prependTTransportException(prepend string, e TTransportException) TTransportException {
115 return &tTransportException{
116 typeId: e.TypeId(),
117 err: e,
118 msg: prepend + e.Error(),
119 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200120}
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700121
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -0800122// isTimeoutError returns true when err is an error caused by timeout.
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700123//
124// Note that this also includes TTransportException wrapped timeout errors.
125func isTimeoutError(err error) bool {
Yuxuan 'fishy' Wange27e82c2021-01-19 11:07:58 -0800126 var t timeoutable
127 if errors.As(err, &t) {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700128 return t.Timeout()
129 }
130 return false
131}