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 |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame^] | 53 | | AE_INTERNAL_ERROR |
| 54 | | AE_PROTOCOL_ERROR |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 55 | deriving ( Eq, Show, Typeable ) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 56 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 57 | instance Enum AppExnType where |
| 58 | toEnum 0 = AE_UNKNOWN |
| 59 | toEnum 1 = AE_UNKNOWN_METHOD |
| 60 | toEnum 2 = AE_INVALID_MESSAGE_TYPE |
| 61 | toEnum 3 = AE_WRONG_METHOD_NAME |
| 62 | toEnum 4 = AE_BAD_SEQUENCE_ID |
| 63 | toEnum 5 = AE_MISSING_RESULT |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame^] | 64 | toEnum 6 = AE_INTERNAL_ERROR |
| 65 | toEnum 7 = AE_PROTOCOL_ERROR |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 66 | toEnum t = error $ "Invalid AppExnType " ++ show t |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 68 | fromEnum AE_UNKNOWN = 0 |
| 69 | fromEnum AE_UNKNOWN_METHOD = 1 |
| 70 | fromEnum AE_INVALID_MESSAGE_TYPE = 2 |
| 71 | fromEnum AE_WRONG_METHOD_NAME = 3 |
| 72 | fromEnum AE_BAD_SEQUENCE_ID = 4 |
| 73 | fromEnum AE_MISSING_RESULT = 5 |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame^] | 74 | fromEnum AE_INTERNAL_ERROR = 6 |
| 75 | fromEnum AE_PROTOCOL_ERROR = 7 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 76 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 77 | data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String } |
| 78 | deriving ( Show, Typeable ) |
| 79 | instance Exception AppExn |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 80 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 81 | writeAppExn :: (Protocol p, Transport t) => p t -> AppExn -> IO () |
| 82 | writeAppExn pt ae = do |
| 83 | writeStructBegin pt "TApplicationException" |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 84 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 85 | when (ae_message ae /= "") $ do |
| 86 | writeFieldBegin pt ("message", T_STRING , 1) |
| 87 | writeString pt (ae_message ae) |
| 88 | writeFieldEnd 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 | writeFieldBegin pt ("type", T_I32, 2); |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 91 | writeI32 pt (fromIntegral $ fromEnum (ae_type ae)) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 92 | writeFieldEnd pt |
| 93 | writeFieldStop pt |
| 94 | writeStructEnd pt |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 95 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 96 | readAppExn :: (Protocol p, Transport t) => p t -> IO AppExn |
| 97 | readAppExn pt = do |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 98 | _ <- readStructBegin pt |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 99 | record <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined}) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 100 | readStructEnd pt |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 101 | return record |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 102 | |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 103 | readAppExnFields :: forall (a :: * -> *) t. (Protocol a, Transport t) => a t -> AppExn -> IO AppExn |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 104 | readAppExnFields pt record = do |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 105 | (_, ft, tag) <- readFieldBegin pt |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 106 | if ft == T_STOP |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 107 | then return record |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 108 | else case tag of |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 109 | 1 -> if ft == T_STRING then |
| 110 | do s <- readString pt |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 111 | readAppExnFields pt record{ae_message = s} |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 112 | else do skip pt ft |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 113 | readAppExnFields pt record |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 114 | 2 -> if ft == T_I32 then |
| 115 | do i <- readI32 pt |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 116 | readAppExnFields pt record{ae_type = (toEnum $ fromIntegral i)} |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 117 | else do skip pt ft |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 118 | readAppExnFields pt record |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 119 | _ -> do skip pt ft |
| 120 | readFieldEnd pt |
Anthony F. Molinaro | daef1c8 | 2010-09-26 04:25:36 +0000 | [diff] [blame] | 121 | readAppExnFields pt record |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 122 | |