Refactor: Improve URL Validation and Error Handling in Client Initialization
Currently, when initializing a client with a custom baseURL, the provided value can be almost any string:
// this obviously incorrect url won't cause any error here
git, err := gitlab.NewClient("yourtokengoeshere", gitlab.WithBaseURL("git.mydomain.com/api/v4"))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
The setBaseURL client method have only minimal validation here, which allows invalid values (e.g., missing or incorrect scheme/protocol, malformed host) to pass through silently.
However, these issues occur later during API requests, often resulting in unexpected runtime errors such as dial tcp failures or unsupported protocol scheme messages. These errors can be confusing for users.
I believe we need stricter validation in setBaseURL: validate at least critical components like scheme (protocol), and host during client initialization. It also would be great to add custom error types (e.g., ErrInvalidBaseURL) that can be used with errors.Is() or errors.As() to allow better error handling in user code.
And, it would be useful to improve documentation and examples, provide cases of how users can handle common connection errors gracefully.