module Network.ABCI.Server.App.DecodeError where

import qualified Data.ByteString         as BS
import qualified Data.ByteString.Base16  as BS16
import qualified Data.ProtoLens          as PL
import           Data.String.Conversions (cs)

data DecodeError
  = CanNotDecodeRequest
      { DecodeError -> ByteString
canNotDecodeRequestRequest :: BS.ByteString
      , DecodeError -> String
canNotDecodeRequestError   :: String
      }
  | NoValueInRequest
      { DecodeError -> ByteString
noValueInRequestRequest       :: BS.ByteString
      , DecodeError -> FieldSet
noValueInRequestUnknownFields :: PL.FieldSet
      }
  | ProtoLensParseError
      { DecodeError -> ByteString
protoLensParseErrorRequest :: BS.ByteString
      , DecodeError -> String
protoLensParseErrorError   :: String
      }
  | InvalidPrefix
      { DecodeError -> ByteString
invalidPrefixMsg    :: BS.ByteString
      , DecodeError -> ByteString
invalidPrefixPrefix :: BS.ByteString
      }

print :: DecodeError -> String
print :: DecodeError -> String
print e :: DecodeError
e =
  case DecodeError
e of
    CanNotDecodeRequest {ByteString
canNotDecodeRequestRequest :: ByteString
canNotDecodeRequestRequest :: DecodeError -> ByteString
canNotDecodeRequestRequest, String
canNotDecodeRequestError :: String
canNotDecodeRequestError :: DecodeError -> String
canNotDecodeRequestError} ->
      "Got decoding error: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
canNotDecodeRequestError
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> " for request: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
showBS ByteString
canNotDecodeRequestRequest
    NoValueInRequest {ByteString
noValueInRequestRequest :: ByteString
noValueInRequestRequest :: DecodeError -> ByteString
noValueInRequestRequest, FieldSet
noValueInRequestUnknownFields :: FieldSet
noValueInRequestUnknownFields :: DecodeError -> FieldSet
noValueInRequestUnknownFields} ->
      "Got empty request: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
showBS ByteString
noValueInRequestRequest
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> " with unknown fields: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [String] -> String
forall a. Show a => a -> String
show ((TaggedValue -> String) -> FieldSet -> [String]
forall a b. (a -> b) -> [a] -> [b]
map TaggedValue -> String
showFields FieldSet
noValueInRequestUnknownFields)
    ProtoLensParseError {ByteString
protoLensParseErrorRequest :: ByteString
protoLensParseErrorRequest :: DecodeError -> ByteString
protoLensParseErrorRequest, String
protoLensParseErrorError :: String
protoLensParseErrorError :: DecodeError -> String
protoLensParseErrorError} ->
      "Got parse error while parsing length prefix: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
protoLensParseErrorError
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> " for request: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
showBS ByteString
protoLensParseErrorRequest
    InvalidPrefix {ByteString
invalidPrefixMsg :: ByteString
invalidPrefixMsg :: DecodeError -> ByteString
invalidPrefixMsg, ByteString
invalidPrefixPrefix :: ByteString
invalidPrefixPrefix :: DecodeError -> ByteString
invalidPrefixPrefix} ->
      "Got Invalid length prefix: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
showBS ByteString
invalidPrefixPrefix
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> " for request: "
        String -> String -> String
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
showBS ByteString
invalidPrefixMsg
  where
    showBS :: ByteString -> String
showBS v :: ByteString
v = String -> String
forall a. Show a => a -> String
show (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ (ByteString -> String
forall a b. ConvertibleStrings a b => a -> b
cs (ByteString -> String)
-> (ByteString -> ByteString) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BS16.encode (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ByteString
v :: String)
    showFields :: TaggedValue -> String
showFields (PL.TaggedValue tag :: Tag
tag _) = Tag -> String
forall a. Show a => a -> String
show Tag
tag