Giving better error messages to the user

Hello,

I am writing a script that is connecting to a HTTPS server. This server may have a self-signed certificate in which case I want to output a “friendly” error and allow the user to ignore this with a command line option.

The code I currently have looks like this:

    if err != nil {
            if strings.Contains(err.Error(), "cannot validate certificate") {
                    fmt.Println("Certicate verification failed. Use --insecure if you have a self-signed cert.")
                    os.Exit(6)
            }

I feel like there should be a better way to do this, but so far my Google searches have not yielded any useful results. Maybe I am using the wrong keywords?

Thanks,
Lordy

I guess you can simply use something like this (eventually with your custom error message)

if err := http.ListenAndServeTLS(":8080", "server.crt", "server.key", nil); err != nil {
   log.Fatalln(err)
}

But I am writing a client and I have no access to the server key.

I missed this detail. Please ignore the response.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.