blob: 893a38961c0afc505ee5399dca213013d540530a [file] [log] [blame]
Mark Slee7eb0d632007-03-01 00:00:27 +00001// 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 Slee3d424402007-02-21 04:17:34 +00007package com.facebook.thrift;
8
9import com.facebook.thrift.protocol.TField;
10import com.facebook.thrift.protocol.TProtocol;
11import com.facebook.thrift.protocol.TProtocolUtil;
12import com.facebook.thrift.protocol.TStruct;
13import com.facebook.thrift.protocol.TType;
14
15/**
16 * Application level exception
17 *
18 * @author Mark Slee <mcslee@facebook.com>
19 */
20public 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 Sleeb46c0412007-02-21 04:54:38 +000029 protected int type_ = UNKNOWN;
Mark Slee3d424402007-02-21 04:17:34 +000030
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 Reiss0c90f6f2008-02-06 22:18:40 +000062 if (field.type == TType.STOP) {
Mark Slee3d424402007-02-21 04:17:34 +000063 break;
64 }
65 switch (field.id) {
66 case 1:
67 if (field.type == TType.STRING) {
68 message = iprot.readString();
David Reiss0c90f6f2008-02-06 22:18:40 +000069 } else {
Mark Slee3d424402007-02-21 04:17:34 +000070 TProtocolUtil.skip(iprot, field.type);
71 }
72 break;
73 case 2:
74 if (field.type == TType.I32) {
75 type = iprot.readI32();
David Reiss0c90f6f2008-02-06 22:18:40 +000076 } else {
Mark Slee3d424402007-02-21 04:17:34 +000077 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}