Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 3d42440 | 2007-02-21 04:17:34 +0000 | [diff] [blame] | 7 | package com.facebook.thrift; |
| 8 | |
| 9 | import com.facebook.thrift.protocol.TField; |
| 10 | import com.facebook.thrift.protocol.TProtocol; |
| 11 | import com.facebook.thrift.protocol.TProtocolUtil; |
| 12 | import com.facebook.thrift.protocol.TStruct; |
| 13 | import com.facebook.thrift.protocol.TType; |
| 14 | |
| 15 | /** |
| 16 | * Application level exception |
| 17 | * |
| 18 | * @author Mark Slee <mcslee@facebook.com> |
| 19 | */ |
| 20 | public class TApplicationException extends TException { |
| 21 | |
| 22 | public static final int UNKNOWN = 0; |
| 23 | public static final int UNKNOWN_METHOD = 1; |
| 24 | public static final int INVALID_MESSAGE_TYPE = 2; |
| 25 | public static final int WRONG_METHOD_NAME = 3; |
| 26 | public static final int BAD_SEQUENCE_ID = 4; |
| 27 | public static final int MISSING_RESULT = 5; |
| 28 | |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 29 | protected int type_ = UNKNOWN; |
Mark Slee | 3d42440 | 2007-02-21 04:17:34 +0000 | [diff] [blame] | 30 | |
| 31 | public TApplicationException() { |
| 32 | super(); |
| 33 | } |
| 34 | |
| 35 | public TApplicationException(int type) { |
| 36 | super(); |
| 37 | type_ = type; |
| 38 | } |
| 39 | |
| 40 | public TApplicationException(int type, String message) { |
| 41 | super(message); |
| 42 | type_ = type; |
| 43 | } |
| 44 | |
| 45 | public TApplicationException(String message) { |
| 46 | super(message); |
| 47 | } |
| 48 | |
| 49 | public int getType() { |
| 50 | return type_; |
| 51 | } |
| 52 | |
| 53 | public static TApplicationException read(TProtocol iprot) throws TException { |
| 54 | TField field; |
| 55 | TStruct struct = iprot.readStructBegin(); |
| 56 | |
| 57 | String message = null; |
| 58 | int type = UNKNOWN; |
| 59 | |
| 60 | while (true) { |
| 61 | field = iprot.readFieldBegin(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 62 | if (field.type == TType.STOP) { |
Mark Slee | 3d42440 | 2007-02-21 04:17:34 +0000 | [diff] [blame] | 63 | break; |
| 64 | } |
| 65 | switch (field.id) { |
| 66 | case 1: |
| 67 | if (field.type == TType.STRING) { |
| 68 | message = iprot.readString(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 69 | } else { |
Mark Slee | 3d42440 | 2007-02-21 04:17:34 +0000 | [diff] [blame] | 70 | TProtocolUtil.skip(iprot, field.type); |
| 71 | } |
| 72 | break; |
| 73 | case 2: |
| 74 | if (field.type == TType.I32) { |
| 75 | type = iprot.readI32(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 76 | } else { |
Mark Slee | 3d42440 | 2007-02-21 04:17:34 +0000 | [diff] [blame] | 77 | TProtocolUtil.skip(iprot, field.type); |
| 78 | } |
| 79 | break; |
| 80 | default: |
| 81 | TProtocolUtil.skip(iprot, field.type); |
| 82 | break; |
| 83 | } |
| 84 | iprot.readFieldEnd(); |
| 85 | } |
| 86 | iprot.readStructEnd(); |
| 87 | |
| 88 | return new TApplicationException(type, message); |
| 89 | } |
| 90 | |
| 91 | public void write(TProtocol oprot) throws TException { |
| 92 | TStruct struct = new TStruct("TApplicationException"); |
| 93 | TField field = new TField(); |
| 94 | oprot.writeStructBegin(struct); |
| 95 | if (getMessage() != null) { |
| 96 | field.name = "message"; |
| 97 | field.type = TType.STRING; |
| 98 | field.id = 1; |
| 99 | oprot.writeFieldBegin(field); |
| 100 | oprot.writeString(getMessage()); |
| 101 | oprot.writeFieldEnd(); |
| 102 | } |
| 103 | field.name = "type"; |
| 104 | field.type = TType.I32; |
| 105 | field.id = 2; |
| 106 | oprot.writeFieldBegin(field); |
| 107 | oprot.writeI32(type_); |
| 108 | oprot.writeFieldEnd(); |
| 109 | oprot.writeFieldStop(); |
| 110 | oprot.writeStructEnd(); |
| 111 | |
| 112 | } |
| 113 | } |