protocol_exception.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "encoding/base64"
  22. )
  23. // Thrift Protocol exception
  24. type TProtocolException interface {
  25. TException
  26. TypeId() int
  27. }
  28. const (
  29. UNKNOWN_PROTOCOL_EXCEPTION = 0
  30. INVALID_DATA = 1
  31. NEGATIVE_SIZE = 2
  32. SIZE_LIMIT = 3
  33. BAD_VERSION = 4
  34. NOT_IMPLEMENTED = 5
  35. DEPTH_LIMIT = 6
  36. )
  37. type tProtocolException struct {
  38. typeId int
  39. message string
  40. }
  41. func (p *tProtocolException) TypeId() int {
  42. return p.typeId
  43. }
  44. func (p *tProtocolException) String() string {
  45. return p.message
  46. }
  47. func (p *tProtocolException) Error() string {
  48. return p.message
  49. }
  50. func NewTProtocolException(err error) TProtocolException {
  51. if err == nil {
  52. return nil
  53. }
  54. if e,ok := err.(TProtocolException); ok {
  55. return e
  56. }
  57. if _, ok := err.(base64.CorruptInputError); ok {
  58. return &tProtocolException{INVALID_DATA, err.Error()}
  59. }
  60. return &tProtocolException{UNKNOWN_PROTOCOL_EXCEPTION, err.Error()}
  61. }
  62. func NewTProtocolExceptionWithType(errType int, err error) TProtocolException {
  63. if err == nil {
  64. return nil
  65. }
  66. return &tProtocolException{errType, err.Error()}
  67. }