blob: 42f5d3218ea397e7f82b23da2e8fb5973838e516 [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
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000057 deriving ( Eq, Show, Typeable )
David Reiss0c90f6f2008-02-06 22:18:40 +000058
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000059instance Enum AppExnType where
60 toEnum 0 = AE_UNKNOWN
61 toEnum 1 = AE_UNKNOWN_METHOD
62 toEnum 2 = AE_INVALID_MESSAGE_TYPE
63 toEnum 3 = AE_WRONG_METHOD_NAME
64 toEnum 4 = AE_BAD_SEQUENCE_ID
65 toEnum 5 = AE_MISSING_RESULT
Roger Meier345ecc72011-08-03 09:49:27 +000066 toEnum 6 = AE_INTERNAL_ERROR
67 toEnum 7 = AE_PROTOCOL_ERROR
Bryan Duxburye59a80f2010-09-20 15:21:37 +000068 toEnum t = error $ "Invalid AppExnType " ++ show t
David Reiss0c90f6f2008-02-06 22:18:40 +000069
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000070 fromEnum AE_UNKNOWN = 0
71 fromEnum AE_UNKNOWN_METHOD = 1
72 fromEnum AE_INVALID_MESSAGE_TYPE = 2
73 fromEnum AE_WRONG_METHOD_NAME = 3
74 fromEnum AE_BAD_SEQUENCE_ID = 4
75 fromEnum AE_MISSING_RESULT = 5
Roger Meier345ecc72011-08-03 09:49:27 +000076 fromEnum AE_INTERNAL_ERROR = 6
77 fromEnum AE_PROTOCOL_ERROR = 7
David Reiss0c90f6f2008-02-06 22:18:40 +000078
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000079data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String }
80 deriving ( Show, Typeable )
81instance Exception AppExn
David Reiss0c90f6f2008-02-06 22:18:40 +000082
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000083writeAppExn :: (Protocol p, Transport t) => p t -> AppExn -> IO ()
84writeAppExn pt ae = do
85 writeStructBegin pt "TApplicationException"
David Reiss0c90f6f2008-02-06 22:18:40 +000086
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000087 when (ae_message ae /= "") $ do
88 writeFieldBegin pt ("message", T_STRING , 1)
Roger Meier6849f202012-05-18 07:35:19 +000089 writeString pt (pack $ ae_message ae)
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000090 writeFieldEnd pt
David Reiss0c90f6f2008-02-06 22:18:40 +000091
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000092 writeFieldBegin pt ("type", T_I32, 2);
Bryan Duxbury75a33e82010-09-22 00:48:56 +000093 writeI32 pt (fromIntegral $ fromEnum (ae_type ae))
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000094 writeFieldEnd pt
95 writeFieldStop pt
96 writeStructEnd pt
David Reiss0c90f6f2008-02-06 22:18:40 +000097
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000098readAppExn :: (Protocol p, Transport t) => p t -> IO AppExn
99readAppExn pt = do
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000100 _ <- readStructBegin pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000101 record <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined})
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000102 readStructEnd pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000103 return record
David Reiss0c90f6f2008-02-06 22:18:40 +0000104
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000105readAppExnFields :: forall (a :: * -> *) t. (Protocol a, Transport t) => a t -> AppExn -> IO AppExn
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000106readAppExnFields pt record = do
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000107 (_, ft, tag) <- readFieldBegin pt
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000108 if ft == T_STOP
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000109 then return record
Bryan Duxburye59a80f2010-09-20 15:21:37 +0000110 else case tag of
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000111 1 -> if ft == T_STRING then
112 do s <- readString pt
Roger Meier6849f202012-05-18 07:35:19 +0000113 readAppExnFields pt record{ae_message = unpack s}
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000114 else do skip pt ft
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000115 readAppExnFields pt record
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000116 2 -> if ft == T_I32 then
117 do i <- readI32 pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000118 readAppExnFields pt record{ae_type = (toEnum $ fromIntegral i)}
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000119 else do skip pt ft
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000120 readAppExnFields pt record
Bryan Duxbury0781f2b2009-04-07 23:29:42 +0000121 _ -> do skip pt ft
122 readFieldEnd pt
Anthony F. Molinarodaef1c82010-09-26 04:25:36 +0000123 readAppExnFields pt record
David Reiss0c90f6f2008-02-06 22:18:40 +0000124