2013-02-06 101 views
0
多的錯誤響應

我使用這個代碼:golang:讀取smtp.SendMail

err := smtp.SendMail(
    smtpHostPort, 
    auth, 
    sender, 
    []string{recipient}, 
    []byte(message), 
) 
if err != nil { 
    log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n")) 
} 

然而,多行錯誤的反應似乎截斷:

2013/02/06 11:54:41 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at"] 

我怎樣才能得到充分的多錯誤響應?

回答

0

錯誤不是多行字符串。

package main 

import (
    "errors" 
    "log" 
    "strings" 
) 

func main() { 
    err := errors.New("530 5.5.1 Authentication Required. Learn more at") 
    log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n")) 
    err = errors.New("530 5.5.1 Authentication Required. Learn more at\nstackoverflow.com") 
    log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n")) 
} 

輸出:

2013/02/06 13:30:19 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at"] 
2013/02/06 13:30:19 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at" "stackoverflow.com"] 
+1

所以它看起來像你正在創建的錯誤對象,而不是直接監聽返回錯誤字符串。是不是可能從函數中獲得多行錯誤? – Xeoncross

+0

據我可以從源頭追蹤,smtp.SendMail最終調用textproto.ReadResponse讀取多行響應: smtp.SendMail: http://golang.org/src/pkg/net/smtp/smtp。去 textproto.ReadResponse: http://golang.org/src/pkg/net/textproto/reader.go // ReadResponse讀取形式的多行響應: // // 代碼\t - 消息行1 // \t代碼消息行2 // \t ... // \t代碼消息行n 這可能是一個錯誤? – Everton