blob: 2dac97e22b4ac4f2f0850c831c1166102744782d [file] [log] [blame]
Christian Lavoieafc6d8f2011-02-20 02:39:19 +00001/*
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 (
23 "encoding/base64"
24 "os"
25)
26
27/**
28 * Protocol exceptions.
29 *
30 */
31type TProtocolException interface {
32 TException
33 TypeId() int
34}
35
36const (
37 UNKNOWN_PROTOCOL_EXCEPTION = 0
38 INVALID_DATA = 1
39 NEGATIVE_SIZE = 2
40 SIZE_LIMIT = 3
41 BAD_VERSION = 4
42 NOT_IMPLEMENTED = 5
43)
44
45type tProtocolException struct {
46 typeId int
47 message string
48}
49
50func (p *tProtocolException) TypeId() int {
51 return p.typeId
52}
53
54func (p *tProtocolException) String() string {
55 return p.message
56}
57
58func NewTProtocolExceptionDefault() TProtocolException {
59 return NewTProtocolExceptionDefaultType(UNKNOWN_PROTOCOL_EXCEPTION)
60}
61
62func NewTProtocolExceptionDefaultType(t int) TProtocolException {
63 return NewTProtocolException(t, "")
64}
65
66func NewTProtocolExceptionDefaultString(m string) TProtocolException {
67 return NewTProtocolException(UNKNOWN_PROTOCOL_EXCEPTION, m)
68}
69
70func NewTProtocolException(t int, m string) TProtocolException {
71 return &tProtocolException{typeId: t, message: m}
72}
73
74func NewTProtocolExceptionReadField(fieldId int, fieldName string, structName string, e TProtocolException) TProtocolException {
75 t := e.TypeId()
76 if t == UNKNOWN_PROTOCOL_EXCEPTION {
77 t = INVALID_DATA
78 }
79 return NewTProtocolException(t, "Unable to read field "+string(fieldId)+" ("+fieldName+") in "+structName+" due to: "+e.String())
80}
81
82func NewTProtocolExceptionWriteField(fieldId int, fieldName string, structName string, e TProtocolException) TProtocolException {
83 t := e.TypeId()
84 if t == UNKNOWN_PROTOCOL_EXCEPTION {
85 t = INVALID_DATA
86 }
87 return NewTProtocolException(t, "Unable to write field "+string(fieldId)+" ("+fieldName+") in "+structName+" due to: "+e.String())
88}
89
90func NewTProtocolExceptionReadStruct(structName string, e TProtocolException) TProtocolException {
91 t := e.TypeId()
92 if t == UNKNOWN_PROTOCOL_EXCEPTION {
93 t = INVALID_DATA
94 }
95 return NewTProtocolException(t, "Unable to read struct "+structName+" due to: "+e.String())
96}
97
98func NewTProtocolExceptionWriteStruct(structName string, e TProtocolException) TProtocolException {
99 t := e.TypeId()
100 if t == UNKNOWN_PROTOCOL_EXCEPTION {
101 t = INVALID_DATA
102 }
103 return NewTProtocolException(t, "Unable to write struct "+structName+" due to: "+e.String())
104}
105
106func NewTProtocolExceptionFromOsError(e os.Error) TProtocolException {
107 if e == nil {
108 return nil
109 }
110 if t, ok := e.(TProtocolException); ok {
111 return t
112 }
113 if te, ok := e.(TTransportException); ok {
114 return NewTProtocolExceptionFromTransportException(te)
115 }
116 if _, ok := e.(base64.CorruptInputError); ok {
117 return NewTProtocolException(INVALID_DATA, e.String())
118 }
119 return NewTProtocolExceptionDefaultString(e.String())
120}
121
122func NewTProtocolExceptionFromTransportException(e TTransportException) TProtocolException {
123 if e == nil {
124 return nil
125 }
126 if t, ok := e.(TProtocolException); ok {
127 return t
128 }
129 return NewTProtocolExceptionDefaultString(e.String())
130}