Skip to content

Commit 4866a5b

Browse files
committed
Action/Server: fall back to the original target for result
Sometimes all targets in the target lists are filtered out by the mapMaybe in `showFromsLogic`, in that case we just fall back to the head of the original result.
1 parent 56dd792 commit 4866a5b

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/Action/Server.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import Data.Function ((&))
5555
import Data.Functor ((<&>))
5656
import Data.Traversable (for)
5757
import Control.Category ((>>>))
58+
import Data.List.NonEmpty (nonEmpty)
59+
import qualified Data.List.NonEmpty as NonEmpty
5860

5961
actionServer :: CmdLine -> IO ()
6062
actionServer cmd@Server{..} = do
@@ -220,8 +222,14 @@ showResults urlOpts links args query results = do
220222
when (null results) $ H.p "No results found"
221223
forM_ results $ \result -> do
222224
let dat = showFromsLogic result
223-
-- TODO: this crashes if there’s no targets
224-
let Target{..} : _ = dat <&> showsFromFirstTarget
225+
let Target{..} =
226+
((dat <&> showsFromFirstTarget)
227+
-- In case showsFromLogic filters out all targets because they are missing fields,
228+
-- fall back to the original first target in the target list.
229+
<|> result)
230+
& nonEmpty & \case
231+
Nothing -> error "showResults: The search result had an empty target list, this should not happen."
232+
Just tgt -> NonEmpty.head tgt
225233
H.div ! H.class_ "result" $ do
226234
H.div ! H.class_ "ans" $ do
227235
H.a ! H.href (H.stringValue $ showURL urlOpts targetURL) $

0 commit comments

Comments
 (0)