Christian Lavoie | afc6d8f | 2011-02-20 02:39:19 +0000 | [diff] [blame] | 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 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "encoding/base64" |
| 24 | "os" |
| 25 | ) |
| 26 | |
| 27 | /** |
| 28 | * Protocol exceptions. |
| 29 | * |
| 30 | */ |
| 31 | type TProtocolException interface { |
| 32 | TException |
| 33 | TypeId() int |
| 34 | } |
| 35 | |
| 36 | const ( |
| 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 | |
| 45 | type tProtocolException struct { |
| 46 | typeId int |
| 47 | message string |
| 48 | } |
| 49 | |
| 50 | func (p *tProtocolException) TypeId() int { |
| 51 | return p.typeId |
| 52 | } |
| 53 | |
| 54 | func (p *tProtocolException) String() string { |
| 55 | return p.message |
| 56 | } |
| 57 | |
| 58 | func NewTProtocolExceptionDefault() TProtocolException { |
| 59 | return NewTProtocolExceptionDefaultType(UNKNOWN_PROTOCOL_EXCEPTION) |
| 60 | } |
| 61 | |
| 62 | func NewTProtocolExceptionDefaultType(t int) TProtocolException { |
| 63 | return NewTProtocolException(t, "") |
| 64 | } |
| 65 | |
| 66 | func NewTProtocolExceptionDefaultString(m string) TProtocolException { |
| 67 | return NewTProtocolException(UNKNOWN_PROTOCOL_EXCEPTION, m) |
| 68 | } |
| 69 | |
| 70 | func NewTProtocolException(t int, m string) TProtocolException { |
| 71 | return &tProtocolException{typeId: t, message: m} |
| 72 | } |
| 73 | |
| 74 | func 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 | |
| 82 | func 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 | |
| 90 | func 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 | |
| 98 | func 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 | |
| 106 | func 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 | |
| 122 | func 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 | } |