Giving better error messages to the user


(Steve) #1

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


(George Calianu) #2

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)
}

(Steve) #3

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


(George Calianu) #4

I missed this detail. Please ignore the response.