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 org.apache.thrift; 21 22 import org.apache.thrift.protocol.TField; 23 import org.apache.thrift.protocol.TProtocol; 24 import org.apache.thrift.protocol.TProtocolUtil; 25 import org.apache.thrift.protocol.TStruct; 26 import org.apache.thrift.protocol.TType; 27 28 /** 29 * Application level exception 30 * 31 */ 32 public class TApplicationException extends TException implements TSerializable { 33 34 private static final TStruct TAPPLICATION_EXCEPTION_STRUCT = new TStruct("TApplicationException"); 35 private static final TField MESSAGE_FIELD = new TField("message", TType.STRING, (short)1); 36 private static final TField TYPE_FIELD = new TField("type", TType.I32, (short)2); 37 38 private static final long serialVersionUID = 1L; 39 40 public static final int UNKNOWN = 0; 41 public static final int UNKNOWN_METHOD = 1; 42 public static final int INVALID_MESSAGE_TYPE = 2; 43 public static final int WRONG_METHOD_NAME = 3; 44 public static final int BAD_SEQUENCE_ID = 4; 45 public static final int MISSING_RESULT = 5; 46 public static final int INTERNAL_ERROR = 6; 47 public static final int PROTOCOL_ERROR = 7; 48 public static final int INVALID_TRANSFORM = 8; 49 public static final int INVALID_PROTOCOL = 9; 50 public static final int UNSUPPORTED_CLIENT_TYPE = 10; 51 52 protected int type_ = UNKNOWN; 53 private String message_ = null; 54 TApplicationException()55 public TApplicationException() { 56 super(); 57 } 58 TApplicationException(int type)59 public TApplicationException(int type) { 60 super(); 61 type_ = type; 62 } 63 TApplicationException(int type, String message)64 public TApplicationException(int type, String message) { 65 super(message); 66 type_ = type; 67 } 68 TApplicationException(String message)69 public TApplicationException(String message) { 70 super(message); 71 } 72 getType()73 public int getType() { 74 return type_; 75 } 76 77 @Override getMessage()78 public String getMessage() { 79 if (message_ == null) { 80 return super.getMessage(); 81 } 82 else { 83 return message_; 84 } 85 } 86 read(TProtocol iprot)87 public void read(TProtocol iprot) throws TException 88 { 89 TField field; 90 iprot.readStructBegin(); 91 92 String message = null; 93 int type = UNKNOWN; 94 95 while (true) { 96 field = iprot.readFieldBegin(); 97 if (field.type == TType.STOP) { 98 break; 99 } 100 switch (field.id) { 101 case 1: 102 if (field.type == TType.STRING) { 103 message = iprot.readString(); 104 } else { 105 TProtocolUtil.skip(iprot, field.type); 106 } 107 break; 108 case 2: 109 if (field.type == TType.I32) { 110 type = iprot.readI32(); 111 } else { 112 TProtocolUtil.skip(iprot, field.type); 113 } 114 break; 115 default: 116 TProtocolUtil.skip(iprot, field.type); 117 break; 118 } 119 iprot.readFieldEnd(); 120 } 121 iprot.readStructEnd(); 122 type_ = type; 123 message_ = message; 124 } 125 126 /** 127 * Convenience factory method for constructing a TApplicationException given a TProtocol input 128 */ readFrom(TProtocol iprot)129 public static TApplicationException readFrom(TProtocol iprot) throws TException 130 { 131 TApplicationException result = new TApplicationException(); 132 result.read(iprot); 133 return result; 134 } 135 write(TProtocol oprot)136 public void write(TProtocol oprot) throws TException 137 { 138 oprot.writeStructBegin(TAPPLICATION_EXCEPTION_STRUCT); 139 if (getMessage() != null) { 140 oprot.writeFieldBegin(MESSAGE_FIELD); 141 oprot.writeString(getMessage()); 142 oprot.writeFieldEnd(); 143 } 144 oprot.writeFieldBegin(TYPE_FIELD); 145 oprot.writeI32(type_); 146 oprot.writeFieldEnd(); 147 oprot.writeFieldStop(); 148 oprot.writeStructEnd(); 149 } 150 } 151