parseError
    :: Stderr
    -> DockerEx
parseError (Stderr stderr) =
    let err = T.dropAround isSpace . T.decodeUtf8 $ stderr
    in fromMaybe (error $ "Failed to parse a docker error: " <> T.unpack err) (isNoSuchImage err <|> isUnknownError err)
  where
    isUnknownError err = Just (DockerError err)
    isNoSuchImage err =
        if T.isInfixOf "No such image:" err
        then case T.splitOn ":" (T.takeWhileEnd (/= ' ') err) of
                 [name, tag] -> Just $ NoSuchImage (Image name (Tag tag))
                 _           -> Nothing
        else Nothing

-- | Invoke the docker command line application.
parseError
    :: Stderr
    -> DockerEx
parseError (Stderr stderr) =
    let err = T.dropAround isSpace . T.decodeUtf8 $ stderr
    in fromMaybe (error $ "Failed to parse a docker error: " <> T.unpack err) (isNoSuchImage err <|> isUnknownError err)
  where
    isUnknownError err = Just (DockerError err)
    isNoSuchImage err =
        if T.isInfixOf "No such image:" err
        then case T.splitOn ":" (T.takeWhileEnd (/= ' ') err) of
                 [name, tag] -> Just $ NoSuchImage (Image name (Tag tag))
                 _           -> Nothing
        else Nothing

-- | Invoke the docker command line application.
parseError (Stderr stderr) =
    let err = T.dropAround isSpace . T.decodeUtf8 $ stderr
    in fromMaybe (error $ "Failed to parse a docker error: " <> T.unpack err) (isNoSuchImage err <|> isUnknownError err)
  where
    isUnknownError err = Just (DockerError err)
    isNoSuchImage err =
        if T.isInfixOf "No such image:" err
        then case T.splitOn ":" (T.takeWhileEnd (/= ' ') err) of
                 [name, tag] -> Just $ NoSuchImage (Image name (Tag tag))
                 _           -> Nothing
        else Nothing

-- | Invoke the docker command line application.