Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catching decompression errors #12

Open
mayeranalytics opened this issue Apr 15, 2018 · 1 comment
Open

Catching decompression errors #12

mayeranalytics opened this issue Apr 15, 2018 · 1 comment

Comments

@mayeranalytics
Copy link

mayeranalytics commented Apr 15, 2018

I can't quite figure out how to catch decompression errors. I'd expect the usual mechanisms, try or catch, to work. But LzmaRet Exceptions thrown by decompress cannot be caught. Here's my example (create a jumbled.xz file with silly content):

{-# LANGUAGE OverloadedStrings #-}
module Main where

import qualified Codec.Compression.Lzma as Lzma
import           Control.Exception      (catch)
import qualified Data.ByteString.Lazy   as BSL

readXz :: FilePath -> IO BSL.ByteString
readXz f = do
    let params = Lzma.defaultDecompressParams {Lzma.decompressMemLimit = maxBound}
    Lzma.decompressWith params <$> BSL.readFile f

handleErr :: Lzma.LzmaRet -> IO BSL.ByteString
handleErr e = do
    print e
    return ""

main :: IO ()
main = do
    a <- catch (readXz "jumbled.xz") handleErr
    print a

This will not fail gracefully. What am I missing, here?

@hvr
Copy link
Collaborator

hvr commented Apr 15, 2018

A transformer acting on lazy bytestrings and returning a lazy bytestring simply has no ability to signal errors in a well-defined way. This is a fundamental issue w/ lazy ByteStrings. Also BSL.readFile suffers from this issue: some I/O errors may result in exceptions being injected into lazy bytestring chunks.

If you need the ability to handle errors in a well-defined way you should use the incremental api, e.g. compressIO or compressST; or alternatively take a look at the streaming framework bindings mentioned at http://hackage.haskell.org/package/lzma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants