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

Improve Invoke-WebRequest xml and json errors format #18837

Merged
merged 31 commits into from
Feb 9, 2023

Conversation

CarloToso
Copy link
Contributor

@CarloToso CarloToso commented Dec 21, 2022

PR Summary

Currently Invoke-WebRequest completely mangles XML error responses:
0

After this PR it recognizes XML and Json and throws better errors:
4x

No changes for text and html errors.

PR Context

Fixes #13251

@CarloToso CarloToso changed the title [WIP] Invoke-WebRequest fix xml error Invoke-WebRequest fix xml and json errors Dec 21, 2022
@iSazonov
Copy link
Collaborator

Formally LGTM. It would great to see output examples for all cases (XML, HTML, Json and Text).

@CarloToso
Copy link
Contributor Author

CarloToso commented Dec 22, 2022

3

I'm completely satisfied with XML errors
Should I try to remove brackets in JSON errors? (Probably with regex *)
Should I try to parse html errors as XML?
How can I distingush Html from text errors?
Where should I move FormatErrorMessage? (StreamHelper.cs?)

*example powershell regex as a starting point

($jsonerror -replace "\s*{|\s*}|\s*\[|\s*\]|`"|`,","" -split"`n")|?{$_}

output

error:
    errors:
        domain: global
        reason: notFound
        message: Not Found
    code: 404
    message: Not Found

@iSazonov
Copy link
Collaborator

iSazonov commented Dec 22, 2022

It's just an error message so I wouldn't waste too much effort on it.


I personally would not remove HTML tags, but since this has already been given preference, let it remain so.
Same for XML.
As for Json, removing brackets would turn it into YAML, which is seem incorrect.


I'd say improving the readability of the error message is good, but we're not preserving the original information, which surprises me.

@CarloToso
Copy link
Contributor Author

@iSazonov We could stop removing HTML tags (it generates ugly errors with far too much white space in many cases); and format XML without removing tags.

@iSazonov
Copy link
Collaborator

@iSazonov We could stop removing HTML tags (it generates ugly errors with far too much white space in many cases); and format XML without removing tags.

Yes, it looks ugly. I don't know why this was done. If there is simple way (like for XML or JSON) to re-format HTML you could add this too.

@CarloToso
Copy link
Contributor Author

Unfortunately I dont think there is an easy way to re-format HTML without using a third party library (ex. AngleSharp).
Where should I move FormatErrorMessage? (StreamHelper.cs? some other place?)

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Please update the PR description with latest screenshots.

@ghost ghost added the Review - Needed The PR is being reviewed label Jan 2, 2023
@ghost
Copy link

ghost commented Jan 2, 2023

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@CarloToso CarloToso requested review from iSazonov and removed request for SteveL-MSFT and PaulHigin February 2, 2023 20:19
@iSazonov
Copy link
Collaborator

iSazonov commented Feb 3, 2023

@iSazonov I fixed the errors, the test helper service returns plain text but says it's json. This resulted in an error when parsing the json. Solved by adding a try-catch

I'd expect the helper returns right Json so that we could test the formatting code.

@CarloToso Please clarify what the helper service returns and how tests work now. If it returns non-json how we check json scenario?

@CarloToso
Copy link
Contributor Author

@iSazonov The helper service now returns:

  1. Plain text, but declares it's application/json (old test, unchanged) -> error caught by try-catch -> old behaviour
  2. Json, application/json (new test) -> the text is formatted
  3. Xml, application/xml (new test) -> the text is formatted

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 8, 2023
@iSazonov
Copy link
Collaborator

iSazonov commented Feb 8, 2023

@CarloToso Please update the PR description and update to actual screenshots.

@pull-request-quantifier-deprecated

This PR has 54 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +52 -2
Percentile : 21.6%

Total files changed: 2

Change summary by file extension:
.cs : +31 -2
.ps1 : +21 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov iSazonov assigned iSazonov and unassigned TravisEz13 Feb 9, 2023
@iSazonov iSazonov changed the title Invoke-WebRequest fix xml and json errors Improve Invoke-WebRequest xml and json errors format Feb 9, 2023
@iSazonov iSazonov merged commit 359f5c3 into PowerShell:master Feb 9, 2023
@CarloToso CarloToso deleted the Invoke-WebRequest-fix-xml-error branch February 9, 2023 08:31
@ghost
Copy link

ghost commented Mar 14, 2023

🎉v7.4.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invoke-WebRequest: The error message is damaged beyond repair
3 participants