Preserve URL encoding in error messages
What does this MR do and why?
This MR fixes a bug where error messages were displaying incorrectly unescaped URLs, making debugging GitLab API calls difficult as the reported URLs didn't match the actual URLs being sent.
Previously, the Error() method in ErrorResponse would unescape the URL path with url.QueryUnescape(). This change preserves URL encoding by using URL.RawPath (with URL.Path fallback) to maintain the original encoding in error messages.
Since the project has moved from the GitHub xanzy/go-gitlab repository to the GitLab Community gitlab-org/api/client-go repository, ensuring that error messages accurately represent API calls is even more important for users migrating and debugging their code.
References
- Fixes issue #22 - Incorrect URL in error messages
Example of the issue
Before the fix:
// Running this code with gitlab-corp/engineering/homebrew-gitlab as projectID
// and Formula/invalid.rb as fileName produces this error:
// ERROR: GET https://gitlab.com/api/v4/projects/gitlab-corp/engineering/homebrew-gitlab/repository/files/Formula/invalid.rb: 404 {message: 404 File Not Found}
After the fix:
// Now the error shows the actual URL used in the API request, with proper escaping:
// ERROR: GET https://gitlab.com/api/v4/projects/gitlab-corp%2Fengineering%2Fhomebrew-gitlab/repository/files/Formula%2Finvalid%2Erb: 404 {message: 404 File Not Found}
Note how slashes (/) are now properly encoded as %2F and the period in .rb is encoded as %2E, matching exactly what is sent to the API.
How to validate
The unit test TestErrorResponsePreservesURLEncoding validates this behavior by:
- Creating a request with a path containing characters that need escaping
- Verifying that the error message contains the properly escaped URL path
- Confirming it doesn't contain the unescaped version