1{-# LANGUAGE DeriveDataTypeable #-}
3-- Module : Data.Text.Encoding.Error
4-- Copyright : (c) Bryan O'Sullivan 2009
7-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
9-- Stability : experimental
12-- Types and functions for dealing with encoding and decoding errors
15-- The standard functions for encoding and decoding text are strict,
16-- which is to say that they throw exceptions on invalid input. This
17-- is often unhelpful on real world input, so alternative functions
18-- exist that accept custom handlers for dealing with invalid inputs.
19-- These 'OnError' handlers are normal Haskell functions. You can use
20-- one of the presupplied functions in this module, or you can write a
21-- custom handler of your own.
23module Data.Text.Encoding.Error
25 -- * Error handling types
30 -- * Useful error handling functions
38import Control.Exception (Exception, throw)
39import Data.Typeable (Typeable)
40import Data.Word (Word8)
41import Numeric (showHex)
43-- | Function type for handling a coding error. It is supplied with
46-- * A 'String' that describes the error.
48-- * The input value that caused the error. If the error arose
49-- because the end of input was reached or could not be identified
50-- precisely, this value will be 'Nothing'.
52-- If the handler returns a value wrapped with 'Just', that value will
53-- be used in the output as the replacement for the invalid input. If
54-- it returns 'Nothing', no value will be used in the output.
56-- Should the handler need to abort processing, it should use 'error'
57-- or 'throw' an exception (preferably a 'UnicodeException'). It may
58-- use the description provided to construct a more helpful error
60type OnError a b = String -> Maybe a -> Maybe b
61type OnDecodeError = OnError Word8 Char
62type OnEncodeError = OnError Char Word8
64-- | An exception type for representing Unicode encoding errors.
65data UnicodeException =
66 DecodeError String (Maybe Word8)
67 -- ^ Could not decode a byte sequence because it was invalid under
68 -- the given encoding, or ran out of input in mid-decode.
69 | EncodeError String (Maybe Char)
70 -- ^ Tried to encode a character that could not be represented
71 -- under the given encoding, or ran out of input in mid-encode.
74showUnicodeException :: UnicodeException -> String
75showUnicodeException (DecodeError desc (Just w))
76 = "Cannot decode byte '\\x" ++ showHex w ("': " ++ desc)
77showUnicodeException (DecodeError desc Nothing)
78 = "Cannot decode input: " ++ desc
79showUnicodeException (EncodeError desc (Just c))
80 = "Cannot encode character '\\x" ++ showHex (fromEnum c) ("': " ++ desc)
81showUnicodeException (EncodeError desc Nothing)
82 = "Cannot encode input: " ++ desc
84instance Show UnicodeException where
85 show = showUnicodeException
87instance Exception UnicodeException
89-- | Throw a 'UnicodeException' if decoding fails.
90strictDecode :: OnError Word8 Char
91strictDecode desc c = throw (DecodeError desc c)
93-- | Replace an invalid input byte with the Unicode replacement
95lenientDecode :: OnError Word8 Char
96lenientDecode _ _ = Just '\xfffd'
98-- | Throw a 'UnicodeException' if encoding fails.
99strictEncode :: OnError Char Word8
100strictEncode desc c = throw (EncodeError desc c)
102-- | Ignore an invalid input, substituting nothing in the output.
106-- | Replace an invalid input with a valid output.
107replace :: b -> OnError a b
108replace c _ _ = Just c
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list