Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improverbosity by InvalidBody showing the root cause and the reason. #184

Merged

Conversation

DEVizzent
Copy link
Contributor

@DEVizzent DEVizzent commented Jan 26, 2023

Most of times we I use this lib for testing, validating request and responses according documentation y got InvalidBody message. So I have to debug when this exception is thrown to see the previous one and come across the root cause of the error. I used a try catch on my side to add more context to this error, but I think it can be usefull for all.

Body does not match schema for content-type "application/json" for Response [put /post/1]

Don't provide any context about why it failed. With this change you will see:

Body does not match schema for content-type "application/json" for Response [put /post/1]. [Value expected to be 'integer', 'string' given in User -> id]

Whith this message we can identify the root cause "User -> id" and the reason.

@DEVizzent DEVizzent force-pushed the improve-invalid-body-error-verbosity branch from aa61adc to 95aad7b Compare January 28, 2023 07:23
$contentType,
$addr,
substr($prev->getMessage(), 0, -1),
implode('->', $prev->dataBreadCrumb()->buildChain())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure, this will not clutter the message for really complex schemas. Can you please provide use case for these messages?

IMO since the validation is exception-based one can wrap validation in try..catch clause and do proper logging of any needed info in any needed format. And for thing like oneOf or allOf this really can be messy as a raw string

Copy link
Contributor Author

@DEVizzent DEVizzent Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of times we I use this lib for testing, validating request and responses according documentation y got InvalidBody message. So I have to debug when this exception is thrown to see the previous one and come across the root cause of the error. I used a try catch on my side to add more context to this error, but I think it can be usefull for all.

Body does not match schema for content-type "application/json" for Response [put /post/1]

Don't provide any context about why it failed. With this change you will see:

Body does not match schema for content-type "application/json" for Response [put /post/1]. [Value expected to be 'integer', 'string' given in User -> id]

Whith this message we can identify the root cause "User -> id" and the reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got it. Testing doesn't have that much tooling indeed. What do you think about introducing some kind of global toggle like Validator/Debug::enable() (or maybe Verbose::enable() ) in order to make it configurable ? I really want to keep generic flow simple since these messages are not machine readable in any case and format (as I specified for oneOf and allOf) can be very debatable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand add a Verbose::enable() which it's only used in one exception it's not a great option. Also as you said this info can be gotten by who need it with a try catch.
So, if you like this Verbose option, and you think it will be used in the future for other purposes I will add it. If not we can close the MR.
I wait for your comment and thank you so much for your fast answers and review :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other solution I can suggest is to add getVerboseMessage on this exception class, so one can just call it instead of getMessage in order to see some additional info as a plain text.

How other libs deal this? LIke symfony validator and others ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really want to keep generic flow simple since these messages are not machine readable in any case and format (as I specified for oneOf and allOf) can be very debatable

Why do you want keep it readable for machines? IMO Machines can have enough with the exception type/name, but developers need a message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these messages are not machine readable

The point is not keep (or make) these machine readable. I'm saying opposite here, these messages (currently and in the future) are not machine readable and not ment to be. These messages are visible in some kinds of logging solutions and, if you take for example symfony debug exception page or sentry as an exception handler - you already can get all the needed info there having chained exceptions on hand.
And adding more info to the messages will pollute simple logging solutions like ELK staks, file logs and so other.

So if we talking about tests here only, it's something about debugging and debugging is usually works in special ways (debug mode for the application with extra wrappers, enabling some debug routines, etc).

That's in my mind here. Not saying this PR is useless or bad, just want to be sure we are not doing worse in other places making one places prettier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @scaytrase, I missunderstand you. Now I understand completly your point :)
I also see, that my last message can seem so rude, it wasn't my intention.

I know you didn't said this PR is useless or bad. But I feel that maybe the case I though it isn't common and can be handled getting the exception information. I think maybe have no sense add this getVerboseMessage only to this exception.

But, if you feel it can provide value I'll be happy to do the changes and contribute.

Copy link
Member

@scaytrase scaytrase Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also see, that my last message can seem so rude, it wasn't my intention.

Ah, it's ok, i'm not native english speaker, so I expect some miscommunications )

I think maybe have no sense add this getVerboseMessage only to this exception.

Up to you. I this solves yours case - I think in this format it would be great addition. I saw people who might get use of it before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done I placed the method in the parent class. Sorry for the delay, and thank you for the support 💪

@DEVizzent DEVizzent force-pushed the improve-invalid-body-error-verbosity branch from 95aad7b to 7aeffca Compare January 28, 2023 08:02
@DEVizzent
Copy link
Contributor Author

DEVizzent commented Jan 28, 2023 via email

@DEVizzent DEVizzent force-pushed the improve-invalid-body-error-verbosity branch from 7aeffca to 0f8ed5e Compare February 18, 2023 07:13
@scaytrase
Copy link
Member

Can you please use something meaningful in commit author? I'd like to preserve original authorship of commits in general, but have to squash and fix commits of previous PR because they have no name neither linked to github account via email of commit being linked to any GH account

@DEVizzent
Copy link
Contributor Author

Can you please use something meaningful in commit author? I'd like to preserve original authorship of commits in general, but have to squash and fix commits of previous PR because they have no name neither linked to github account via email of commit being linked to any GH account

Sure, I'll do it tomorrow morning. I will check the commit history to find some examples

@DEVizzent DEVizzent force-pushed the improve-invalid-body-error-verbosity branch from 0f8ed5e to 93ac1f3 Compare February 19, 2023 07:24
@DEVizzent DEVizzent force-pushed the improve-invalid-body-error-verbosity branch from 93ac1f3 to 464eb0a Compare February 19, 2023 07:26
@DEVizzent
Copy link
Contributor Author

Can you please use something meaningful in commit author? I'd like to preserve original authorship of commits in general, but have to squash and fix commits of previous PR because they have no name neither linked to github account via email of commit being linked to any GH account

Sure, I'll do it tomorrow morning. I will check the commit history to find some examples

@scaytrase Yesterday I didn't understand what you were saying, because I have properly configured my global user.name and user.email, but I don't know why I wasn't configured in this concret project 🤯
I configured and reset the commit author.

@DEVizzent
Copy link
Contributor Author

@scaytrase Can you confirm commit autor is now correct and merge? Thank you 👍 .

@scaytrase
Copy link
Member

Looks good! Thanks. I'll merge it a bit later, sorry, a bit busy last days

@scaytrase scaytrase merged commit 0e25f98 into thephpleague:master Mar 23, 2023
@scaytrase
Copy link
Member

thanks @DEVizzent !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants