transport_exception.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package thrift
  20. import (
  21. "errors"
  22. "io"
  23. )
  24. type timeoutable interface {
  25. Timeout() bool
  26. }
  27. // Thrift Transport exception
  28. type TTransportException interface {
  29. TException
  30. TypeId() int
  31. Err() error
  32. }
  33. const (
  34. UNKNOWN_TRANSPORT_EXCEPTION = 0
  35. NOT_OPEN = 1
  36. ALREADY_OPEN = 2
  37. TIMED_OUT = 3
  38. END_OF_FILE = 4
  39. )
  40. type tTransportException struct {
  41. typeId int
  42. err error
  43. msg string
  44. }
  45. var _ TTransportException = (*tTransportException)(nil)
  46. func (tTransportException) TExceptionType() TExceptionType {
  47. return TExceptionTypeTransport
  48. }
  49. func (p *tTransportException) TypeId() int {
  50. return p.typeId
  51. }
  52. func (p *tTransportException) Error() string {
  53. return p.msg
  54. }
  55. func (p *tTransportException) Err() error {
  56. return p.err
  57. }
  58. func (p *tTransportException) Unwrap() error {
  59. return p.err
  60. }
  61. func (p *tTransportException) Timeout() bool {
  62. return p.typeId == TIMED_OUT
  63. }
  64. func NewTTransportException(t int, e string) TTransportException {
  65. return &tTransportException{
  66. typeId: t,
  67. err: errors.New(e),
  68. msg: e,
  69. }
  70. }
  71. func NewTTransportExceptionFromError(e error) TTransportException {
  72. if e == nil {
  73. return nil
  74. }
  75. if t, ok := e.(TTransportException); ok {
  76. return t
  77. }
  78. te := &tTransportException{
  79. typeId: UNKNOWN_TRANSPORT_EXCEPTION,
  80. err: e,
  81. msg: e.Error(),
  82. }
  83. if isTimeoutError(e) {
  84. te.typeId = TIMED_OUT
  85. return te
  86. }
  87. if errors.Is(e, io.EOF) {
  88. te.typeId = END_OF_FILE
  89. return te
  90. }
  91. return te
  92. }
  93. func prependTTransportException(prepend string, e TTransportException) TTransportException {
  94. return &tTransportException{
  95. typeId: e.TypeId(),
  96. err: e,
  97. msg: prepend + e.Error(),
  98. }
  99. }
  100. // isTimeoutError returns true when err is an error caused by timeout.
  101. //
  102. // Note that this also includes TTransportException wrapped timeout errors.
  103. func isTimeoutError(err error) bool {
  104. var t timeoutable
  105. if errors.As(err, &t) {
  106. return t.Timeout()
  107. }
  108. return false
  109. }