Skip to content

Improve getElementById error reporting #129

@blitzcode

Description

@blitzcode

getElementById is typed to return a Maybe, but actually never returns a Nothing in case of failure, but throws an exception. I'd suggest to either actually return a Maybe or remove it from the type to make it clear it throws an exception. Also, the exception is unhelpful for understanding why generating the page failed, making it extremely hard to track down the error.

For now, I wrote this wrapper:

getElementByIdSafe :: Window -> String -> UI Element
getElementByIdSafe window elementID =
    (liftIO $ try (runUI window $ getElementById window elementID)) >>= \case
        Left (e :: SomeException) -> do
            traceS TLError $ printf "getElementById for '%s' failed: %s" elementID (show e)
            liftIO . throwIO $ e
        Right Nothing -> do
            traceS TLError $ printf "getElementById for '%s' failed" elementID
            liftIO . throwIO $ userError "getElementById failure"
        Right (Just e) ->
            return e

This gives at least the failure reason and the name of the missing element.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions