egonSchiele / contracts.ruby

Contracts for Ruby.
http://egonschiele.github.com/contracts.ruby
BSD 2-Clause "Simplified" License
1.44k stars 82 forks source link

[enhancement] Better display of errors. #261

Open damaon opened 7 years ago

damaon commented 7 years ago

First thanks for developing great library, but I would like to propose enhancement.

When testing this gem for complex params hashes I found that ParamContractError is almost unreadable. I mean structure of error is neat, but what is displayed in Expected: is hardly readable IMO. Also Actual: would be nice with some coloring or adding Errors: as I propose below.

1) Expected: shows inspected ruby structures instead of how I wrote assertion. Totaly unreadable - I just need to check where type was defined and read it in editor to compare.

2) I propose also adding output coloring for Actual: - so coloring Red, or at least writing "INCORRECT" where you make mistake in recursive type.

Example:

Expected: StrictHash[{ user: StrictHash[{ name: String , id: Num }]  }]
Actual: { user: { name: { foo: "bar" }, id: 123 }
Errors: { user: { name: "INCORRECT", id:  123 }

I would appreciate feedback about my proposal and tips how this could be implemented.

damaon commented 7 years ago

I think there should be 3 colors: Red - incorrect things Yellow - missing things Green - OK

But this can be easily expressed with text interface:

Expected: StrictHash[{ user: StrictHash[{ name: String , id: Num }]  }]
Actual: { user: { name: INCORRECT<{ foo: "bar" }>, id: 123, REDUNDANT<last_name: "foo"> }
egonSchiele commented 7 years ago

Hi Machiaweliczny! I like this idea. The change would have to be made here: https://github.com/egonSchiele/contracts.ruby/blob/master/lib/contracts.rb#L129

Also keep in mind, sometimes it is not appropriate to color. For example, if the output is being written to a file, then the coloring would just add nonsense characters to the output. So you should check if the output is tty before coloring.

cabe56 commented 5 years ago

@egonSchiele is this still relevant? Was looking over issues in the repo to see if there was another ticket about errors.

Dunno about coloring in OP, would just love for the error message to tell me 'this key in the hash did not match the contract'.

Have never contributed but would love to take a stab at this one as it would save me a lot of time debugging.