transport_exception.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }
  44. func (p *tTransportException) TypeId() int {
  45. return p.typeId
  46. }
  47. func (p *tTransportException) Error() string {
  48. return p.err.Error()
  49. }
  50. func (p *tTransportException) Err() error {
  51. return p.err
  52. }
  53. func NewTTransportException(t int, e string) TTransportException {
  54. return &tTransportException{typeId: t, err: errors.New(e)}
  55. }
  56. func NewTTransportExceptionFromError(e error) TTransportException {
  57. if e == nil {
  58. return nil
  59. }
  60. if t, ok := e.(TTransportException); ok {
  61. return t
  62. }
  63. switch v := e.(type) {
  64. case TTransportException:
  65. return v
  66. case timeoutable:
  67. if v.Timeout() {
  68. return &tTransportException{typeId: TIMED_OUT, err: e}
  69. }
  70. }
  71. if e == io.EOF {
  72. return &tTransportException{typeId: END_OF_FILE, err: e}
  73. }
  74. return &tTransportException{typeId: UNKNOWN_TRANSPORT_EXCEPTION, err: e}
  75. }