Skip to content

Commit 202da6d

Browse files
authored
Merge pull request #550 from phadej/stuhv
Add parserThrowError and parserCatchError
2 parents 8f246ac + f0097f6 commit 202da6d

13 files changed

+67
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
dist
22
dist-newstyle
3+
.ghc.environment.*
34
.cabal-sandbox/
45
cabal.sandbox.config
56
.stack-work/

Data/Aeson/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module Data.Aeson.Types
3636
, ToJSON(..)
3737
, KeyValue(..)
3838
, modifyFailure
39+
, parserThrowError
40+
, parserCatchError
3941

4042
-- ** Keys for maps
4143
, ToJSONKey(..)

Data/Aeson/Types/Internal.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module Data.Aeson.Types.Internal
4444
, parseEither
4545
, parseMaybe
4646
, modifyFailure
47+
, parserThrowError
48+
, parserCatchError
4749
, formatError
4850
, (<?>)
4951
-- * Constructors and accessors
@@ -507,7 +509,22 @@ p <?> pathElem = Parser $ \path kf ks -> runParser p (pathElem:path) kf ks
507509
--
508510
-- Since 0.6.2.0
509511
modifyFailure :: (String -> String) -> Parser a -> Parser a
510-
modifyFailure f (Parser p) = Parser $ \path kf ks -> p path (\p' m -> kf p' (f m)) ks
512+
modifyFailure f (Parser p) = Parser $ \path kf ks ->
513+
p path (\p' m -> kf p' (f m)) ks
514+
515+
-- | Throw a parser error with an additional path.
516+
--
517+
-- @since 1.2.1.0
518+
parserThrowError :: JSONPath -> String -> Parser a
519+
parserThrowError path' msg = Parser $ \path kf _ks ->
520+
kf (reverse path ++ path') msg
521+
522+
-- | A handler function to handle previous errors and return to normal execution.
523+
--
524+
-- @since 1.2.1.0
525+
parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
526+
parserCatchError (Parser p) handler = Parser $ \path kf ks ->
527+
p path (\e msg -> runParser (handler e msg) path kf ks) ks
511528

512529
--------------------------------------------------------------------------------
513530
-- Generic and TH encoding configuration

aeson.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ test-suite tests
225225
unordered-containers,
226226
uuid-types,
227227
vector,
228-
quickcheck-instances >=0.3.12
228+
quickcheck-instances >=0.3.14
229229

230230
if flag(bytestring-builder)
231231
build-depends: bytestring >= 0.9 && < 0.10.4,

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
For the latest version of this document, please see [https://github.com/bos/aeson/blob/master/changelog.md](https://github.com/bos/aeson/blob/master/changelog.md).
22

3+
## 1.2.1.0
4+
5+
* Add `parserThrowError` and `parserCatchError` combinators.
6+
37
## 1.2.0.0
48

59
* `tagSingleConstructors`, an option to encode single-constructor types as tagged sums was added to `Options`. It is disabled by default for backward compatibility.

stack-bench.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ resolver: lts-8.5
22
packages:
33
- '.'
44
- benchmarks
5+
extra-deps:
6+
- quickcheck-instances-0.3.14

stack-lts6.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ packages:
33
- '.'
44
- attoparsec-iso8601
55
extra-deps:
6-
- semigroups-0.18.2
76
- integer-logarithms-1
7+
- QuickCheck-2.9.2
8+
- quickcheck-instances-0.3.14
9+
- semigroups-0.18.2
10+
- tagged-0.8.5
11+
- transformers-compat-0.5.1.4
12+
flags:
813
flags:
914
aeson:
1015
fast: true

stack-lts7.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ packages:
44
- attoparsec-iso8601
55
extra-deps:
66
- integer-logarithms-1
7+
- quickcheck-instances-0.3.14
78
flags:
89
aeson:
910
fast: true

stack-lts8.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ resolver: lts-8.1
22
packages:
33
- '.'
44
- attoparsec-iso8601
5+
extra-deps:
6+
- quickcheck-instances-0.3.14
57
flags:
68
aeson:
79
fast: true

stack-nightly.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ resolver: nightly-2017-03-25
22
packages:
33
- '.'
44
- attoparsec-iso8601
5+
extra-deps:
6+
- quickcheck-instances-0.3.14
57
flags:
68
aeson:
79
fast: true

0 commit comments

Comments
 (0)