Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 1 | {-# LANGUAGE DeriveDataTypeable #-} |
| 2 | {-# LANGUAGE KindSignatures #-} |
| 3 | {-# LANGUAGE RankNTypes #-} |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 4 | -- |
| 5 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 6 | -- or more contributor license agreements. See the NOTICE file |
| 7 | -- distributed with this work for additional information |
| 8 | -- regarding copyright ownership. The ASF licenses this file |
| 9 | -- to you under the Apache License, Version 2.0 (the |
| 10 | -- "License"); you may not use this file except in compliance |
| 11 | -- with the License. You may obtain a copy of the License at |
| 12 | -- |
| 13 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | -- |
| 15 | -- Unless required by applicable law or agreed to in writing, |
| 16 | -- software distributed under the License is distributed on an |
| 17 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | -- KIND, either express or implied. See the License for the |
| 19 | -- specific language governing permissions and limitations |
| 20 | -- under the License. |
| 21 | -- |
| 22 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 23 | module Thrift |
| 24 | ( module Thrift.Transport |
| 25 | , module Thrift.Protocol |
| 26 | , AppExnType(..) |
| 27 | , AppExn(..) |
| 28 | , readAppExn |
| 29 | , writeAppExn |
| 30 | , ThriftException(..) |
| 31 | ) where |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 32 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 33 | import Control.Monad ( when ) |
| 34 | import Control.Exception |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 35 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 36 | import Data.Typeable ( Typeable ) |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 37 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 38 | import Thrift.Transport |
| 39 | import Thrift.Protocol |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 40 | |
| 41 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 42 | data ThriftException = ThriftException |
| 43 | deriving ( Show, Typeable ) |
| 44 | instance Exception ThriftException |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 46 | data AppExnType |
| 47 | = AE_UNKNOWN |
| 48 | | AE_UNKNOWN_METHOD |
| 49 | | AE_INVALID_MESSAGE_TYPE |
| 50 | | AE_WRONG_METHOD_NAME |
| 51 | | AE_BAD_SEQUENCE_ID |
| 52 | | AE_MISSING_RESULT |
| 53 | deriving ( Eq, Show, Typeable ) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 54 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 55 | instance Enum AppExnType where |
| 56 | toEnum 0 = AE_UNKNOWN |
| 57 | toEnum 1 = AE_UNKNOWN_METHOD |
| 58 | toEnum 2 = AE_INVALID_MESSAGE_TYPE |
| 59 | toEnum 3 = AE_WRONG_METHOD_NAME |
| 60 | toEnum 4 = AE_BAD_SEQUENCE_ID |
| 61 | toEnum 5 = AE_MISSING_RESULT |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 62 | toEnum t = error $ "Invalid AppExnType " ++ show t |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 63 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 64 | fromEnum AE_UNKNOWN = 0 |
| 65 | fromEnum AE_UNKNOWN_METHOD = 1 |
| 66 | fromEnum AE_INVALID_MESSAGE_TYPE = 2 |
| 67 | fromEnum AE_WRONG_METHOD_NAME = 3 |
| 68 | fromEnum AE_BAD_SEQUENCE_ID = 4 |
| 69 | fromEnum AE_MISSING_RESULT = 5 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 70 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 71 | data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String } |
| 72 | deriving ( Show, Typeable ) |
| 73 | instance Exception AppExn |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 74 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 75 | writeAppExn :: (Protocol p, Transport t) => p t -> AppExn -> IO () |
| 76 | writeAppExn pt ae = do |
| 77 | writeStructBegin pt "TApplicationException" |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 78 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 79 | when (ae_message ae /= "") $ do |
| 80 | writeFieldBegin pt ("message", T_STRING , 1) |
| 81 | writeString pt (ae_message ae) |
| 82 | writeFieldEnd pt |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 83 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 84 | writeFieldBegin pt ("type", T_I32, 2); |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame^] | 85 | writeI32 pt (fromIntegral $ fromEnum (ae_type ae)) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 86 | writeFieldEnd pt |
| 87 | writeFieldStop pt |
| 88 | writeStructEnd pt |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 89 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 90 | readAppExn :: (Protocol p, Transport t) => p t -> IO AppExn |
| 91 | readAppExn pt = do |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 92 | _ <- readStructBegin pt |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 93 | rec <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined}) |
| 94 | readStructEnd pt |
| 95 | return rec |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 96 | |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 97 | readAppExnFields :: forall (a :: * -> *) t. (Protocol a, Transport t) => a t -> AppExn -> IO AppExn |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 98 | readAppExnFields pt rec = do |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 99 | (_, ft, tag) <- readFieldBegin pt |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 100 | if ft == T_STOP |
| 101 | then return rec |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 102 | else case tag of |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 103 | 1 -> if ft == T_STRING then |
| 104 | do s <- readString pt |
| 105 | readAppExnFields pt rec{ae_message = s} |
| 106 | else do skip pt ft |
| 107 | readAppExnFields pt rec |
| 108 | 2 -> if ft == T_I32 then |
| 109 | do i <- readI32 pt |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame^] | 110 | readAppExnFields pt rec{ae_type = (toEnum $ fromIntegral i)} |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 111 | else do skip pt ft |
| 112 | readAppExnFields pt rec |
| 113 | _ -> do skip pt ft |
| 114 | readFieldEnd pt |
| 115 | readAppExnFields pt rec |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 116 | |