Skip to content

Idea: improve error messages with compile-time information of the faulty expression #148

@vemv

Description

@vemv

Hi!

First to give a bit of context, I find myself writing something like the following in every other project:

https://github.com/reducecombine/playground/blob/954104e2c673b8e9952c12c4d707dc946570a68c/src/playground/spec_utils.clj#L6-L12

i.e throw an expounded-powered runtime exception if spec/valid? fails. Probably it's a common pattern.

Now, the error messages obtained with this technique are nice, but they could be even better.

For the (check! string? (:foo (do-it 42))) input:

Current

:the/namespace: -- Spec failed --------------------

  nil

should satisfy

  string?
[...]

Desired

:the/namespace: -- Spec failed --------------------

  nil

evaluated from

  (:foo (do-it 42))

should satisfy

  string?
[...]

The "evaluated from" bit tells us what expression (e.g. which function argument) did evaluate to nil. Basically that's what we humans do when debugging these expound errors: we try to find the compile-time expression which caused the nil value.

Note that the check! function I linked to is a function, so I can't access compile-time info.

Now, I could turn it into a macro. Probably pretty easily I could achieve what I want. Now, the only pain point is concatenating my information with expound's information. Manipulating strings is dirty.

So I was thinking, why don't we provide in Expound something akin to my tiny check! helper, allowing for a clean report which includes the desired extra info.

As a nice side-effect, we'd get a "canonical" check! - not ideal having to copy/paste it, or authoring yet another "spec utils library".

WDYT? PR welcome?

Cheers - Victor

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions