blob: 65a22080908b1d91edf2b04338832b55df009487 [file] [log] [blame]
Bryan Duxburye59a80f2010-09-20 15:21:37 +00001{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE KindSignatures #-}
Roger Meier6849f202012-05-18 07:35:19 +00003{-# LANGUAGE OverloadedStrings #-}
Bryan Duxburye59a80f2010-09-20 15:21:37 +00004{-# LANGUAGE RankNTypes #-}
David Reissea2cba82009-03-30 21:35:00 +00005--
6-- Licensed to the Apache Software Foundation (ASF) under one
7-- or more contributor license agreements. See the NOTICE file
8-- distributed with this work for additional information
9-- regarding copyright ownership. The ASF licenses this file
10-- to you under the Apache License, Version 2.0 (the
11-- "License"); you may not use this file except in compliance
12-- with the License. You may obtain a copy of the License at
13--
14-- http://www.apache.org/licenses/LICENSE-2.0
15--
16-- Unless required by applicable law or agreed to in writing,
17-- software distributed under the License is distributed on an
18-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19-- KIND, either express or implied. See the License for the
20-- specific language governing permissions and limitations
21-- under the License.
22--
23
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000024module Thrift
25 ( module Thrift.Transport
26 , module Thrift.Protocol
27 , AppExnType(..)
28 , AppExn(..)
29 , readAppExn
30 , writeAppExn
31 , ThriftException(..)
32 ) where
iproctorff8eb922007-07-25 19:06:13 +000033
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000034import Control.Monad ( when )
35import Control.Exception
iproctorff8eb922007-07-25 19:06:13 +000036
Roger Meier6849f202012-05-18 07:35:19 +000037import Data.Text.Lazy ( pack, unpack )
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000038import Data.Typeable ( Typeable )
iproctorff8eb922007-07-25 19:06:13 +000039
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000040import Thrift.Transport
41import Thrift.Protocol
David Reiss0c90f6f2008-02-06 22:18:40 +000042
43
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000044data ThriftException = ThriftException
45 deriving ( Show, Typeable )
46instance Exception ThriftException
David Reiss0c90f6f2008-02-06 22:18:40 +000047
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000048data AppExnType
49 = AE_UNKNOWN
50 | AE_UNKNOWN_METHOD
51 | AE_INVALID_MESSAGE_TYPE
52 | AE_WRONG_METHOD_NAME
53 | AE_BAD_SEQUENCE_ID
54 | AE_MISSING_RESULT
Roger Meier345ecc72011-08-03 09:49:27 +000055 | AE_INTERNAL_ERROR
56 | AE_PROTOCOL_ERROR
Roger Meier01931492012-12-22 21:31:03 +010057 | AE_INVALID_TRANSFORM
58 | AE_INVALID_PROTOCOL
59 | AE_UNSUPPORTED_CLIENT_TYPE
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000060 deriving ( Eq, Show, Typeable )
David Reiss0c90f6f2008-02-06 22:18:40 +000061
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000062instance Enum AppExnType where
63 toEnum 0 = AE_UNKNOWN
64 toEnum 1 = AE_UNKNOWN_METHOD
65 toEnum 2 = AE_INVALID_MESSAGE_TYPE
66 toEnum 3 = AE_WRONG_METHOD_NAME
67 toEnum 4 = AE_BAD_SEQUENCE_ID
68 toEnum 5 = AE_MISSING_RESULT
Roger Meier345ecc72011-08-03 09:49:27 +000069 toEnum 6 = AE_INTERNAL_ERROR
70 toEnum 7 = AE_PROTOCOL_ERROR
Roger Meier01931492012-12-22 21:31:03 +010071 toEnum 8 = AE_INVALID_TRANSFORM
72 toEnum 9 = AE_INVALID_PROTOCOL
73 toEnum 10 = AE_UNSUPPORTED_CLIENT_TYPE
Bryan Duxburye59a80f2010-09-20 15:21:37 +000074 toEnum t = error $ "Invalid AppExnType " ++ show t
David Reiss0c90f6f2008-02-06 22:18:40 +000075
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000076 fromEnum AE_UNKNOWN = 0
77 fromEnum AE_UNKNOWN_METHOD = 1
78 fromEnum AE_INVALID_MESSAGE_TYPE = 2
79 fromEnum AE_WRONG_METHOD_NAME = 3
80 fromEnum AE_BAD_SEQUENCE_ID = 4
81 fromEnum AE_MISSING_RESULT = 5
Roger Meier345ecc72011-08-03 09:49:27 +000082 fromEnum AE_INTERNAL_ERROR = 6
83 fromEnum AE_PROTOCOL_ERROR = 7
Roger Meier01931492012-12-22 21:31:03 +010084 fromEnum AE_INVALID_TRANSFORM = 8
85 fromEnum AE_INVALID_PROTOCOL = 9
86 fromEnum AE_UNSUPPORTED_CLIENT_TYPE = 10
David Reiss0c90f6f2008-02-06 22:18:40 +000087
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000088data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String }
89 deriving ( Show, Typeable )
90instance Exception AppExn
David Reiss0c90f6f2008-02-06 22:18:40 +000091
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000092writeAppExn :: (Protocol p, Transport t) => p t -> AppExn -> IO ()
93writeAppExn pt ae = do
94 writeStructBegin pt "TApplicationException"
David Reiss0c90f6f2008-02-06 22:18:40 +000095
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000096 when (ae_message ae /= "") $ do
97 writeFieldBegin pt ("message", T_STRING , 1)
Roger Meier6849f202012-05-18 07:35:19 +000098 writeString pt (pack $ ae_message ae)
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000099 writeFieldEnd pt
David Reiss0c90f6f2008-02-06 22:18:40 +0000100
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000101 writeFieldBegin pt ("type", T_I32, 2);
Bryan Duxbury75a33e82010-09-22 00:48:56 +0000102 writeI32 pt (fromIntegral $ fromEnum (ae_type ae))
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000103 writeFieldEnd pt
104 writeFieldStop pt
105 writeStructEnd pt
David Reiss0c90f6f2008-02-06 22:18:40 +0000106
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000107readAppExn :: (Protocol p, Transport t) => p t -> IO AppExn
108readAppExn pt = do
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000109 _ <- readStructBegin pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000110 record <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined})
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000111 readStructEnd pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000112 return record
David Reiss0c90f6f2008-02-06 22:18:40 +0000113
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000114readAppExnFields :: forall (a :: * -> *) t. (Protocol a, Transport t) => a t -> AppExn -> IO AppExn
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000115readAppExnFields pt record = do
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000116 (_, ft, tag) <- readFieldBegin pt
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000117 if ft == T_STOP
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000118 then return record
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000119 else case tag of
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000120 1 -> if ft == T_STRING then
121 do s <- readString pt
Roger Meier6849f202012-05-18 07:35:19 +0000122 readAppExnFields pt record{ae_message = unpack s}
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000123 else do skip pt ft
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000124 readAppExnFields pt record
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000125 2 -> if ft == T_I32 then
126 do i <- readI32 pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000127 readAppExnFields pt record{ae_type = (toEnum $ fromIntegral i)}
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000128 else do skip pt ft
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000129 readAppExnFields pt record
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000130 _ -> do skip pt ft
131 readFieldEnd pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000132 readAppExnFields pt record
David Reiss0c90f6f2008-02-06 22:18:40 +0000133