user2879704
user2879704

Reputation:

Wildcard character asterisk in route path throws error

A simple yesod server code with a single handler for all GET requests i wrote as:

{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses,
TemplateHaskell, OverloadedStrings #-}
import Yesod
data Links = Links
mkYesod "Links" [parseRoutes|
/* HomeR GET
|]
instance Yesod Links
getHomeR = defaultLayout [whamlet|Go to page 1!|]
main = warpDebug 3000 Links

It throws a cryptic error as,

 Exception when trying to run compile-time code:
      Invalid type: ""
    Code: mkYesod
            "Links"
            ([Yesod.Routes.TH.Types.ResourceLeaf
                (Yesod.Routes.TH.Types.Resource
                   "HomeR"
                   []
                   (Yesod.Routes.TH.Types.Methods (Data.Maybe.Just []) ["GET"])
                   []
                   GHC.Types.True)])

I copied the working code from a blog and removed all but one route, added wildcard character * to the single routepath as [parseRoutes| /* HomeR GET \] to make it the handler for all urls. And, the code no longer works.

Upvotes: 0

Views: 166

Answers (1)

Random Dev
Random Dev

Reputation: 52290

you have to add an type that is an instance of PathMultiPiece after the * like this:

/*Texts HomeR GET

(Texts, which is just a synonym for [Text] is an instance because Text is an instance of PathPiece) and of course you have to add it as an argument to your handler:

getHomeR :: [Text] -> Handler Html
getHomeR parts = defaultLayout [whamlet|Go to page 1!|]

you can read more about this in the yesod documentation

Upvotes: 2

Related Questions