yesodweb / yesodweb/persistent

Improving error messages

Open
#849 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

A common problem my coworker learning Haskell/Yesod/Persistent is that he's missing a runDB in a Yesod Handler function, which gives this kind of error:

    • Couldn't match type 'ReaderT backend0 m0' with 'HandlerFor App'
      Expected type: HandlerFor App (Entity Organization)
        Actual type: ReaderT backend0 m0 (Entity Organization)

His main complaint, which I think is reasonable, is that nothing says "this is related to Persistent" or "this is related to the database". Could Persistent do anything to give better error messages in this case? Maybe renaming usages of backend to databaseBackend? Perhaps a type synonym for ReaderT backend m used in Persistent would help?

insertUniqueEntity
    :: (MonadIO m
       ,PersistRecordBackend record backend
       ,PersistUniqueWrite backend)
    => record -> ReaderT backend m (Maybe (Entity record))

I haven't used them before, but it looked like GHC's TypeError wouldn't work for this? I tried this which I didn't really think would work (and didn't, but also gave lots of warnings), but I'm really fumbling in the dark with that feature—couldn't find very good documentation on it.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

import qualified GHC.TypeLits as GHC 

instance GHC.TypeError (GHC.Text "Yesod handlers cannot call database functions directly, use runDB.")
        => HasPersistBackend (HandlerFor site a) where
  persistBackend = error "unreachable"

instance GHC.TypeError (GHC.Text "Yesod handlers cannot call database functions directly, use runDB.")
        => IsPersistBackend (HandlerFor site a) where

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the insertUniqueEntity type signature and the shown HandlerFor/ReaderT mismatch, then investigate the attempted GHC.TypeError instances and Persistent backend constraints. No files or tests are named; done would require a concrete, feasible approach that makes this missing-runDB error identify Persistent or the database and guides users toward runDB.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.