{-# OPTIONS_GHC -Wall #-}

-- | A single failure during METAR lookup, tagged with the source (which BOM
-- state or which upstream API) and either a connection failure or a parse-level
-- reason.
module Data.Aviation.Metar.METARError (
  METARError (..),
  connError,
  parseError,
) where

import Network.Stream (ConnError)

-- $setup
-- >>> import Data.Aviation.Metar.METARError
-- >>> import Network.Stream

-- | Description of one thing that went wrong at one source.
--
-- >>> connError "NOAA" (ErrorMisc "boom")
-- ConnErrorAt "NOAA" (ErrorMisc "boom")
-- >>> parseError "New-South-Wales" "not found"
-- ParseErrorAt "New-South-Wales" "not found"
data METARError
  = -- | Network failure at the named source.
    ConnErrorAt String ConnError
  | -- | Non-network failure at the named source, e.g. HTTP 404 or ICAO
    -- absent from the response.
    ParseErrorAt String String
  deriving (METARError -> METARError -> Bool
(METARError -> METARError -> Bool)
-> (METARError -> METARError -> Bool) -> Eq METARError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: METARError -> METARError -> Bool
== :: METARError -> METARError -> Bool
$c/= :: METARError -> METARError -> Bool
/= :: METARError -> METARError -> Bool
Eq, Int -> METARError -> ShowS
[METARError] -> ShowS
METARError -> String
(Int -> METARError -> ShowS)
-> (METARError -> String)
-> ([METARError] -> ShowS)
-> Show METARError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> METARError -> ShowS
showsPrec :: Int -> METARError -> ShowS
$cshow :: METARError -> String
show :: METARError -> String
$cshowList :: [METARError] -> ShowS
showList :: [METARError] -> ShowS
Show)

-- | Build a 'ConnErrorAt'.
--
-- >>> connError "BOM" (ErrorMisc "response timeout")
-- ConnErrorAt "BOM" (ErrorMisc "response timeout")
connError ::
  String ->
  ConnError ->
  METARError
connError :: String -> ConnError -> METARError
connError =
  String -> ConnError -> METARError
ConnErrorAt

-- | Build a 'ParseErrorAt'.
--
-- >>> parseError "NOAA" "HTTP 404"
-- ParseErrorAt "NOAA" "HTTP 404"
parseError ::
  String ->
  String ->
  METARError
parseError :: String -> String -> METARError
parseError =
  String -> String -> METARError
ParseErrorAt