Skip to content

Commit 7ee31dd

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 a1ccb7d commit 7ee31dd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Action/Server.hs

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

6062
actionServer :: CmdLine -> IO ()
6163
actionServer cmd@Server{..} = do
@@ -211,8 +213,14 @@ showResults urlOpts links args query results = do
211213
when (null results) $ H.p "No results found"
212214
forM_ results $ \result -> do
213215
let dat = showFromsLogic result
214-
-- TODO: this crashes if there’s no targets
215-
let Target{..} : _ = dat <&> showsFromFirstTarget
216+
let Target{..} =
217+
((dat <&> showsFromFirstTarget)
218+
-- In case showsFromLogic filters out all targets because they are missing fields,
219+
-- fall back to the original first target in the target list.
220+
<|> result)
221+
& nonEmpty & \case
222+
Nothing -> error "showResults: The search result had an empty target list, this should not happen."
223+
Just tgt -> NonEmpty.head tgt
216224
H.div ! H.class_ "result" $ do
217225
H.div ! H.class_ "ans" $ do
218226
H.a ! H.href (H.stringValue $ showURL urlOpts targetURL) $

0 commit comments

Comments
 (0)