Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 1 | {-# LANGUAGE DeriveDataTypeable #-} |
Roger Meier | 6849f20 | 2012-05-18 07:35:19 +0000 | [diff] [blame] | 2 | {-# LANGUAGE OverloadedStrings #-} |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 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.Exception |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 34 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 35 | import Data.Int |
| 36 | import Data.Text.Lazy ( Text, pack, unpack ) |
| 37 | import Data.Text.Lazy.Encoding |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 38 | import Data.Typeable ( Typeable ) |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 39 | import qualified Data.HashMap.Strict as Map |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 40 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 41 | import Thrift.Protocol |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 42 | import Thrift.Transport |
| 43 | import Thrift.Types |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 44 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 45 | data ThriftException = ThriftException |
| 46 | deriving ( Show, Typeable ) |
| 47 | instance Exception ThriftException |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 48 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 49 | data AppExnType |
| 50 | = AE_UNKNOWN |
| 51 | | AE_UNKNOWN_METHOD |
| 52 | | AE_INVALID_MESSAGE_TYPE |
| 53 | | AE_WRONG_METHOD_NAME |
| 54 | | AE_BAD_SEQUENCE_ID |
| 55 | | AE_MISSING_RESULT |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame] | 56 | | AE_INTERNAL_ERROR |
| 57 | | AE_PROTOCOL_ERROR |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 58 | | AE_INVALID_TRANSFORM |
| 59 | | AE_INVALID_PROTOCOL |
| 60 | | AE_UNSUPPORTED_CLIENT_TYPE |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 61 | deriving ( Eq, Show, Typeable ) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 62 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 63 | instance Enum AppExnType where |
| 64 | toEnum 0 = AE_UNKNOWN |
| 65 | toEnum 1 = AE_UNKNOWN_METHOD |
| 66 | toEnum 2 = AE_INVALID_MESSAGE_TYPE |
| 67 | toEnum 3 = AE_WRONG_METHOD_NAME |
| 68 | toEnum 4 = AE_BAD_SEQUENCE_ID |
| 69 | toEnum 5 = AE_MISSING_RESULT |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame] | 70 | toEnum 6 = AE_INTERNAL_ERROR |
| 71 | toEnum 7 = AE_PROTOCOL_ERROR |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 72 | toEnum 8 = AE_INVALID_TRANSFORM |
| 73 | toEnum 9 = AE_INVALID_PROTOCOL |
| 74 | toEnum 10 = AE_UNSUPPORTED_CLIENT_TYPE |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 75 | toEnum t = error $ "Invalid AppExnType " ++ show t |
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 | fromEnum AE_UNKNOWN = 0 |
| 78 | fromEnum AE_UNKNOWN_METHOD = 1 |
| 79 | fromEnum AE_INVALID_MESSAGE_TYPE = 2 |
| 80 | fromEnum AE_WRONG_METHOD_NAME = 3 |
| 81 | fromEnum AE_BAD_SEQUENCE_ID = 4 |
| 82 | fromEnum AE_MISSING_RESULT = 5 |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame] | 83 | fromEnum AE_INTERNAL_ERROR = 6 |
| 84 | fromEnum AE_PROTOCOL_ERROR = 7 |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 85 | fromEnum AE_INVALID_TRANSFORM = 8 |
| 86 | fromEnum AE_INVALID_PROTOCOL = 9 |
| 87 | fromEnum AE_UNSUPPORTED_CLIENT_TYPE = 10 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 88 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 89 | data AppExn = AppExn { ae_type :: AppExnType, ae_message :: String } |
| 90 | deriving ( Show, Typeable ) |
| 91 | instance Exception AppExn |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 92 | |
Nobuaki Sukegawa | 3c42007 | 2016-01-24 04:01:27 +0900 | [diff] [blame] | 93 | writeAppExn :: Protocol p => p -> AppExn -> IO () |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 94 | writeAppExn pt ae = writeVal pt $ TStruct $ Map.fromList |
| 95 | [ (1, ("message", TString $ encodeUtf8 $ pack $ ae_message ae)) |
| 96 | , (2, ("type", TI32 $ fromIntegral $ fromEnum (ae_type ae))) |
| 97 | ] |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 98 | |
Nobuaki Sukegawa | 3c42007 | 2016-01-24 04:01:27 +0900 | [diff] [blame] | 99 | readAppExn :: Protocol p => p -> IO AppExn |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 100 | readAppExn pt = do |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 101 | let typemap = Map.fromList [(1,("message",T_STRING)),(2,("type",T_I32))] |
| 102 | TStruct fields <- readVal pt $ T_STRUCT typemap |
| 103 | return $ readAppExnFields fields |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 104 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 105 | readAppExnFields :: Map.HashMap Int16 (Text, ThriftVal) -> AppExn |
| 106 | readAppExnFields fields = AppExn{ |
| 107 | ae_message = maybe undefined unwrapMessage $ Map.lookup 1 fields, |
| 108 | ae_type = maybe undefined unwrapType $ Map.lookup 2 fields |
| 109 | } |
| 110 | where |
| 111 | unwrapMessage (_, TString s) = unpack $ decodeUtf8 s |
| 112 | unwrapMessage _ = undefined |
| 113 | unwrapType (_, TI32 i) = toEnum $ fromIntegral i |
| 114 | unwrapType _ = undefined |