Home > Back-end >  How to return the errors in golang?
How to return the errors in golang?

Time:10-27

In this code, I am taking the data from the environment variable and using it to send the email to the given address. It only works if I exclude the errors from the code. I returned the errors and used them in middleware because I don't want to use log.Fatal(err) every time. But it gives me two errors.

1. cannot use "" (untyped string constant) as [4]string value in return statement

2. code can not execute unreachable return statement

func LoadEnvVariable(key string) (string, error) {
    viper.SetConfigFile(".env")
    err := viper.ReadInConfig()
    return "", err

    value, ok := viper.Get(key).(string)
    if !ok {
        return "", err
    }
    return value, nil
}

func Email(value [4]string) ([4]string, error) {
    mail := gomail.NewMessage()

    myEmail, err := LoadEnvVariable("EMAIL")
    return "", err
    appPassword, err := LoadEnvVariable("APP_PASSWORD")
    return "", err

    mail.SetHeader("From", myEmail)
    mail.SetHeader("To", myEmail)
    mail.SetHeader("Reply-To", value[1])
    mail.SetHeader("subject", value[2])
    mail.SetBody("text/plain", value[3])

    a := gomail.NewDialer("smtp.gmail.com", 587, myEmail, appPassword)
    err = a.DialAndSend(mail)
    return "", err

    return value, nil
}

CodePudding user response:

use this to check error

   if err != nil {  
       return "", err 
    }

func LoadEnvVariable(key string) (string, error) {
        viper.SetConfigFile(".env")
        err := viper.ReadInConfig()
        if err != nil {
            return "", err
        }
    
        value, ok := viper.Get(key).(string)
        if !ok {
            return "", err
        }
        return value, nil
    }
    
    func Email(value [4]string) ([4]string, error) {
        mail := gomail.NewMessage()
    
        myEmail, err := LoadEnvVariable("EMAIL")
        if err != nil {
            return "", err
        }
    
        appPassword, err := LoadEnvVariable("APP_PASSWORD")
        if err != nil {
            return "", err
        }
    
        mail.SetHeader("From", myEmail)
        mail.SetHeader("To", myEmail)
        mail.SetHeader("Reply-To", value[1])
        mail.SetHeader("subject", value[2])
        mail.SetBody("text/plain", value[3])
    
        a := gomail.NewDialer("smtp.gmail.com", 587, myEmail, appPassword)
        err = a.DialAndSend(mail)
        if err != nil {
            return "", err
        }
    
        return value, nil
    }

CodePudding user response:

Firstly, to error handle, check if err is nil before returning!

So not:

myEmail, err := LoadEnvVariable("EMAIL")
return "", err

but instead:

myEmail, err := LoadEnvVariable("EMAIL")
if err != nil {
    return "", err
}

Secondly, a single string type "" is compatible to a fixed array size of [4]string. It may make your life easier to use named return variables, so you don't need to pass a explicit "zero" value in the event of an error. So define your function signature like so:

func Email(in [4]string) (out [4]string, err error) {

    myEmail, err := LoadEnvVariable("EMAIL")
    if err != nil {
        return // implicitly returns 'out' and 'err'
    }

    // ...

    out = in
    return  // implicitly returns 'out' and nil 'err'
}
  • Related