protocol_exception.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "errors"
  23. )
  24. // Thrift Protocol exception
  25. type TProtocolException interface {
  26. TException
  27. TypeId() int
  28. }
  29. const (
  30. UNKNOWN_PROTOCOL_EXCEPTION = 0
  31. INVALID_DATA = 1
  32. NEGATIVE_SIZE = 2
  33. SIZE_LIMIT = 3
  34. BAD_VERSION = 4
  35. NOT_IMPLEMENTED = 5
  36. DEPTH_LIMIT = 6
  37. )
  38. type tProtocolException struct {
  39. typeId int
  40. err error
  41. msg string
  42. }
  43. var _ TProtocolException = (*tProtocolException)(nil)
  44. func (tProtocolException) TExceptionType() TExceptionType {
  45. return TExceptionTypeProtocol
  46. }
  47. func (p *tProtocolException) TypeId() int {
  48. return p.typeId
  49. }
  50. func (p *tProtocolException) String() string {
  51. return p.msg
  52. }
  53. func (p *tProtocolException) Error() string {
  54. return p.msg
  55. }
  56. func (p *tProtocolException) Unwrap() error {
  57. return p.err
  58. }
  59. func NewTProtocolException(err error) TProtocolException {
  60. if err == nil {
  61. return nil
  62. }
  63. if e, ok := err.(TProtocolException); ok {
  64. return e
  65. }
  66. if errors.As(err, new(base64.CorruptInputError)) {
  67. return NewTProtocolExceptionWithType(INVALID_DATA, err)
  68. }
  69. return NewTProtocolExceptionWithType(UNKNOWN_PROTOCOL_EXCEPTION, err)
  70. }
  71. func NewTProtocolExceptionWithType(errType int, err error) TProtocolException {
  72. if err == nil {
  73. return nil
  74. }
  75. return &tProtocolException{
  76. typeId: errType,
  77. err: err,
  78. msg: err.Error(),
  79. }
  80. }
  81. func prependTProtocolException(prepend string, err TProtocolException) TProtocolException {
  82. return &tProtocolException{
  83. typeId: err.TypeId(),
  84. err: err,
  85. msg: prepend + err.Error(),
  86. }
  87. }