Skip to content

Commit 970b413

Browse files
committed
Improve error message for retrieval of Inlines, Blocks
1 parent e7cc2cd commit 970b413

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
`pandoc-lua-marshal` uses [PVP Versioning][].
44

5+
## 0.1.6.1
6+
7+
Released 2022-06-10.
8+
9+
- Provide better error messages when fuzzy retrieval of Inlines
10+
or Blocks fails.
11+
12+
- Relax upper bound for text, allow text-2.0.
13+
514
## 0.1.6
615

716
Released 2022-06-03.

src/Text/Pandoc/Lua/Marshal/Block.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,22 @@ pushBlocks xs = do
9090
-- bare strings as @Str@ values.
9191
peekBlockFuzzy :: LuaError e
9292
=> Peeker e Block
93-
peekBlockFuzzy = choice
94-
[ peekBlock
95-
, \idx -> Plain <$!> peekInlinesFuzzy idx
96-
]
93+
peekBlockFuzzy idx =
94+
peekBlock idx
95+
<|> (Plain <$!> peekInlinesFuzzy idx)
96+
<|> (failPeek =<<
97+
typeMismatchMessage "Block or list of Inlines" idx)
9798
{-# INLINABLE peekBlockFuzzy #-}
9899

99100
-- | Try extra-hard to return the value at the given index as a list of
100101
-- inlines.
101102
peekBlocksFuzzy :: LuaError e
102103
=> Peeker e [Block]
103-
peekBlocksFuzzy = choice
104-
[ peekList peekBlockFuzzy
105-
, (<$!>) pure . peekBlockFuzzy
106-
]
104+
peekBlocksFuzzy idx =
105+
peekList peekBlockFuzzy idx
106+
<|> (pure <$!> peekBlockFuzzy idx)
107+
<|> (failPeek =<<
108+
typeMismatchMessage "Block, list of Blocks, or compatible element" idx)
107109
{-# INLINABLE peekBlocksFuzzy #-}
108110

109111
-- | Block object type.

src/Text/Pandoc/Lua/Marshal/Inline.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Text.Pandoc.Lua.Marshal.Inline
2525
, walkInlinesStraight
2626
) where
2727

28-
import Control.Applicative (optional)
28+
import Control.Applicative ((<|>), optional)
2929
import Control.Monad.Catch (throwM)
3030
import Control.Monad ((<$!>))
3131
import Data.Data (showConstr, toConstr)
@@ -93,10 +93,10 @@ peekInlinesFuzzy :: LuaError e
9393
=> Peeker e [Inline]
9494
peekInlinesFuzzy idx = liftLua (ltype idx) >>= \case
9595
TypeString -> B.toList . B.text <$> peekText idx
96-
_ -> choice
97-
[ peekList peekInlineFuzzy
98-
, fmap pure . peekInlineFuzzy
99-
] idx
96+
_ -> peekList peekInlineFuzzy idx
97+
<|> (pure <$> peekInlineFuzzy idx)
98+
<|> (failPeek =<<
99+
typeMismatchMessage "Inline, list of Inlines, or string" idx)
100100
{-# INLINABLE peekInlinesFuzzy #-}
101101

102102
-- | Inline object type.

test/test-block.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ return {
349349
{'Header', 'CodeBlock'}
350350
)
351351
end),
352+
test('gives sensible error message', function ()
353+
assert.error_matches(
354+
function() Blocks(nil) end,
355+
'Block, list of Blocks, or compatible element expected'
356+
)
357+
end)
352358
},
353359
group 'walk' {
354360
test('modifies Inline subelements', function ()

test/test-inline.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ return {
318318
{'Str', 'Space', 'Str'}
319319
)
320320
end),
321+
test('gives sensible error message', function ()
322+
assert.error_matches(
323+
function() Inlines(nil) end,
324+
"Inline, list of Inlines, or string"
325+
)
326+
end)
321327
},
322328
group 'walk' {
323329
test('modifies Inline subelements', function ()

0 commit comments

Comments
 (0)