{-# LANGUAGE CPP, DeriveDataTypeable #-}#if __GLASGOW_HASKELL__ >= 702{-# LANGUAGE Trustworthy #-}#endif-- |-- Module : Data.Text.Encoding.Error-- Copyright : (c) Bryan O'Sullivan 2009---- License : BSD-style-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,-- duncan@haskell.org-- Stability : experimental-- Portability : GHC---- Types and functions for dealing with encoding and decoding errors-- in Unicode text.---- The standard functions for encoding and decoding text are strict,-- which is to say that they throw exceptions on invalid input. This-- is often unhelpful on real world input, so alternative functions-- exist that accept custom handlers for dealing with invalid inputs.-- These 'OnError' handlers are normal Haskell functions. You can use-- one of the presupplied functions in this module, or you can write a-- custom handler of your own.moduleData.Text.Encoding.Error(-- * Error handling typesUnicodeException(..),OnError,OnDecodeError,OnEncodeError-- * Useful error handling functions,lenientDecode,strictDecode,strictEncode,ignore,replace)whereimportControl.DeepSeq(NFData(..))#if __GLASGOW_HASKELL__ >= 610importControl.Exception(Exception,throw)#elseimportControl.Exception.Extensible(Exception,throw)#endifimportData.Typeable(Typeable)importData.Word(Word8)importNumeric(showHex)-- | Function type for handling a coding error. It is supplied with-- two inputs:---- * A 'String' that describes the error.---- * The input value that caused the error. If the error arose-- because the end of input was reached or could not be identified-- precisely, this value will be 'Nothing'.---- If the handler returns a value wrapped with 'Just', that value will-- be used in the output as the replacement for the invalid input. If-- it returns 'Nothing', no value will be used in the output.---- Should the handler need to abort processing, it should use 'error'-- or 'throw' an exception (preferably a 'UnicodeException'). It may-- use the description provided to construct a more helpful error-- report.typeOnErrorab=String->Maybea->Maybeb-- | A handler for a decoding error.typeOnDecodeError=OnErrorWord8Char-- | A handler for an encoding error.typeOnEncodeError=OnErrorCharWord8-- | An exception type for representing Unicode encoding errors.dataUnicodeException=DecodeErrorString(MaybeWord8)-- ^ Could not decode a byte sequence because it was invalid under-- the given encoding, or ran out of input in mid-decode.|EncodeErrorString(MaybeChar)-- ^ Tried to encode a character that could not be represented-- under the given encoding, or ran out of input in mid-encode.deriving(Eq,Typeable)showUnicodeException::UnicodeException->StringshowUnicodeException(DecodeErrordesc(Justw))="Cannot decode byte '\\x"++showHexw("': "++desc)showUnicodeException(DecodeErrordescNothing)="Cannot decode input: "++descshowUnicodeException(EncodeErrordesc(Justc))="Cannot encode character '\\x"++showHex(fromEnumc)("': "++desc)showUnicodeException(EncodeErrordescNothing)="Cannot encode input: "++descinstanceShowUnicodeExceptionwhereshow=showUnicodeExceptioninstanceExceptionUnicodeExceptioninstanceNFDataUnicodeExceptionwherernf(DecodeErrordescw)=rnfdesc`seq`rnfw`seq`()rnf(EncodeErrordescc)=rnfdesc`seq`rnfc`seq`()-- | Throw a 'UnicodeException' if decoding fails.strictDecode::OnDecodeErrorstrictDecodedescc=throw(DecodeErrordescc)-- | Replace an invalid input byte with the Unicode replacement-- character U+FFFD.lenientDecode::OnDecodeErrorlenientDecode__=Just'\xfffd'-- | Throw a 'UnicodeException' if encoding fails.strictEncode::OnEncodeErrorstrictEncodedescc=throw(EncodeErrordescc)-- | Ignore an invalid input, substituting nothing in the output.ignore::OnErrorabignore__=Nothing-- | Replace an invalid input with a valid output.replace::b->OnErrorabreplacec__=Justc