Use `fmt.Errorf()` instead of `errors.New()` to create new error messages.

v0.11
Martin Dosch 4 weeks ago
parent cbb7beb72b
commit d2f758b4f9
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D

@ -3,6 +3,7 @@
## UNRELEASED
### Changed
- Move private Ox key into JID folder in ~/.local/share/go-sendxmpp.
- Use `fmt.Errorf()` instead of `errors.New()` to create new error messages.
### Added
- Add new parameter `--subject`.

@ -10,7 +10,6 @@ import (
"crypto/cipher"
"crypto/rand"
"encoding/gob"
"errors"
"fmt"
"log"
"math/big"
@ -200,7 +199,7 @@ func getDataPath(folder string) (string, error) {
return strError, fmt.Errorf("getDataPath: failed to determine user dir: %w", err)
}
if homeDir == "" {
return strError, errors.New("getDataPath: received empty string for home directory")
return strError, fmt.Errorf("getDataPath: received empty string for home directory")
}
dataDir = homeDir + "/.local/share"
}

@ -7,7 +7,6 @@ package main
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"net/http"
"net/url"
@ -68,7 +67,7 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
}
iqDiscoItemsXMLQuery = iqDiscoItemsXML.SelectElement("query")
if iqDiscoItemsXMLQuery == nil {
return "", errors.New("http-upload: no query element in disco items reply")
return "", fmt.Errorf("http-upload: no query element in disco items reply")
}
iqDiscoItemsXMLItems := iqDiscoItemsXMLQuery.SelectElements("item")
@ -118,7 +117,7 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
}
}
if uploadComponent == "" {
return "", errors.New("http-upload: no http upload component found.")
return "", fmt.Errorf("http-upload: no http upload component found.")
}
iqDiscoInfoXMLX := iqDiscoInfoXMLQuery.SelectElements("x")
for _, r := range iqDiscoInfoXMLX {
@ -143,7 +142,7 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
if prevFieldVal.Text() == nsHTTPUpload {
maxFileSize, err = strconv.ParseInt(curFieldVal.Text(), 10, 64)
if err != nil {
return "", errors.New("http-upload: error while checking server maximum http upload file size.")
return "", fmt.Errorf("http-upload: error while checking server maximum http upload file size.")
}
}
}
@ -154,9 +153,8 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
// the best.
if maxFileSize != 0 {
if fileSize > maxFileSize {
return "", errors.New("http-upload: file size " + strconv.FormatInt(fileSize/1024/1024, 10) +
" MiB is larger than the maximum file size allowed (" +
strconv.FormatInt(maxFileSize/1024/1024, 10) + " MiB).")
return "", fmt.Errorf("http-upload: file size %s MiB is larger than the maximum file size allowed (%s MiB).",
strconv.FormatInt(fileSize/1024/1024, 10), strconv.FormatInt(maxFileSize/1024/1024, 10))
}
}
@ -178,7 +176,7 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
return "", err
}
if uploadSlot.Type != strResult {
return "", errors.New("http-upload: error while requesting upload slot.")
return "", fmt.Errorf("http-upload: error while requesting upload slot.")
}
iqHTTPUploadSlotXML := etree.NewDocument()
err = iqHTTPUploadSlotXML.ReadFromBytes(uploadSlot.Query)
@ -187,18 +185,18 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
}
iqHTTPUploadSlotXMLSlot := iqHTTPUploadSlotXML.SelectElement("slot")
if iqHTTPUploadSlotXMLSlot == nil {
return "", errors.New("http-upload: no slot element")
return "", fmt.Errorf("http-upload: no slot element")
}
iqHTTPUploadSlotXMLPut := iqHTTPUploadSlotXMLSlot.SelectElement("put")
if iqHTTPUploadSlotXMLPut == nil {
return "", errors.New("http-upload: no put element")
return "", fmt.Errorf("http-upload: no put element")
}
iqHTTPUploadSlotXMLPutURL := iqHTTPUploadSlotXMLPut.SelectAttr("url")
if iqHTTPUploadSlotXMLPutURL == nil {
return "", errors.New("http-upload: no url attribute")
return "", fmt.Errorf("http-upload: no url attribute")
}
if !strings.HasPrefix(iqHTTPUploadSlotXMLPutURL.Value, "https://") {
return "", errors.New("http-upload: upload slot does not provide https")
return "", fmt.Errorf("http-upload: upload slot does not provide https")
}
// Upload file
httpTransport := &http.Transport{
@ -237,17 +235,17 @@ func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, jserver string, filePath
}
// Test for http status code "200 OK" or "201 Created"
if resp.StatusCode != 200 && resp.StatusCode != 201 {
return "", errors.New("http-upload: upload failed.")
return "", fmt.Errorf("http-upload: upload failed.")
}
// Return http link
iqHTTPUploadSlotXMLGet := iqHTTPUploadSlotXMLSlot.SelectElement("get")
if iqHTTPUploadSlotXMLGet == nil {
return "", errors.New("http-upload: no get element")
return "", fmt.Errorf("http-upload: no get element")
}
iqHTTPUploadSlotXMLGetURL := iqHTTPUploadSlotXMLGet.SelectAttr("url")
if iqHTTPUploadSlotXMLGetURL == nil {
return "", errors.New("http-upload: no url attribute")
return "", fmt.Errorf("http-upload: no url attribute")
}
err = resp.Body.Close()
if err != nil {

@ -8,7 +8,7 @@
package main
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
@ -35,7 +35,7 @@ func MarshalJID(input string) (string, error) {
} else {
// If the resource part exists, make sure it isn't empty.
if sep == len(s)-1 {
return input, errors.New("Invalid JID" + input + ": The resourcepart must be larger than 0 bytes")
return input, fmt.Errorf("invalid JID %s: the resourcepart must be larger than 0 bytes", input)
}
resourcepart = s[sep+1:]
s = s[:sep]
@ -52,7 +52,7 @@ func MarshalJID(input string) (string, error) {
domainpart = s
case sep == 0:
// The JID starts with an @ sign (invalid empty localpart)
err = errors.New("Invalid JID:" + input)
err = fmt.Errorf("Invalid JID: %s", input)
return input, err
default:
domainpart = s[sep+1:]
@ -73,11 +73,11 @@ func MarshalJID(input string) (string, error) {
var jid string
if !utf8.ValidString(localpart) || !utf8.ValidString(domainpart) || !utf8.ValidString(resourcepart) {
return input, errors.New("Invalid JID: " + input)
return input, fmt.Errorf("invalid JID: %s", input)
}
if domainpart == "" {
return input, errors.New("Invalid JID: " + input)
return input, fmt.Errorf("invalid JID: %s", input)
}
if localpart == "" {

@ -8,7 +8,6 @@ import (
"bufio"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
@ -568,7 +567,7 @@ func main() {
default:
if err != nil {
cancel()
closeAndExit(client, errors.New("failed to read from stdin"))
closeAndExit(client, fmt.Errorf("failed to read from stdin"))
}
}
}

60
ox.go

@ -6,7 +6,6 @@ package main
import (
"encoding/base64"
"errors"
"fmt"
"log"
"os"
@ -39,11 +38,11 @@ func oxDeleteNodes(jid string, client *xmpp.Client, iqc chan xmpp.IQ) error {
}
query = nodeListReply.SelectElement("query")
if query == nil {
return errors.New("error parsing iq reply")
return fmt.Errorf("error parsing iq reply")
}
items := query.SelectElements("item")
if items == nil {
return errors.New("error parsing iq reply")
return fmt.Errorf("error parsing iq reply")
}
for _, item := range items {
node := item.SelectAttr("node")
@ -106,26 +105,26 @@ func oxDecrypt(m xmpp.Chat, client *xmpp.Client, iqc chan xmpp.IQ, user string,
}
signcrypt := doc.SelectElement("signcrypt")
if signcrypt == nil {
return strError, time.Now(), errors.New("ox: no signcrypt element")
return strError, time.Now(), fmt.Errorf("ox: no signcrypt element")
}
to := signcrypt.SelectElement("to")
if to == nil {
return strError, time.Now(), errors.New("ox: no to element")
return strError, time.Now(), fmt.Errorf("ox: no to element")
}
jid := to.SelectAttr("jid")
if jid == nil {
return strError, time.Now(), errors.New("ox: no jid attribute")
return strError, time.Now(), fmt.Errorf("ox: no jid attribute")
}
if strings.Split(jid.Value, "/")[0] != user {
return strError, time.Now(), errors.New("ox: encrypted for wrong user")
return strError, time.Now(), fmt.Errorf("ox: encrypted for wrong user")
}
timestamp := signcrypt.SelectElement("time")
if timestamp == nil {
return strError, time.Now(), errors.New("ox: no time element")
return strError, time.Now(), fmt.Errorf("ox: no time element")
}
stamp := timestamp.SelectAttr("stamp")
if stamp == nil {
return strError, time.Now(), errors.New("ox: no stamp attribute")
return strError, time.Now(), fmt.Errorf("ox: no stamp attribute")
}
msgStamp, err := time.Parse("2006-01-02T15:04:05Z0700", stamp.Value)
if err != nil {
@ -133,7 +132,7 @@ func oxDecrypt(m xmpp.Chat, client *xmpp.Client, iqc chan xmpp.IQ, user string,
}
payload := signcrypt.SelectElement("payload")
if payload == nil {
return strError, time.Now(), errors.New("ox: no payload element")
return strError, time.Now(), fmt.Errorf("ox: no payload element")
}
body := payload.SelectElement("body")
if body == nil {
@ -173,7 +172,7 @@ func oxImportPrivKey(jid string, privKeyLocation string, client *xmpp.Client, iq
}
entity := key.GetEntity()
if entity.Identities[xmppURI] == nil {
return errors.New("Key identity is not " + xmppURI)
return fmt.Errorf("Key identity is not %s", xmppURI)
}
pk, err := key.GetPublicKey()
if err != nil {
@ -262,19 +261,19 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, pubKey *
return fmt.Errorf("oxPublishPubKey: iq failure publishing public key: %w", err)
}
if iqReply.Type != strResult {
return errors.New("error while publishing public key")
return fmt.Errorf("error while publishing public key")
}
ownPubKeyRingFromPubsub, err := oxRecvPublicKeys(client, iqc, jid, fingerprint)
if err != nil {
return errors.New("couldn't successfully verify public key upload")
return fmt.Errorf("couldn't successfully verify public key upload")
}
ownPubKeyFromPubsub := ownPubKeyRingFromPubsub.GetKeys()[0]
ownPubKeyFromPubsubSerialized, err := ownPubKeyFromPubsub.Serialize()
if err != nil {
return errors.New("couldn't successfully verify public key upload")
return fmt.Errorf("couldn't successfully verify public key upload")
}
if pubKeyBase64 != base64.StdEncoding.EncodeToString(ownPubKeyFromPubsubSerialized) {
return errors.New("couldn't successfully verify public key upload")
return fmt.Errorf("couldn't successfully verify public key upload")
}
root = etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
@ -310,7 +309,7 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, pubKey *
return fmt.Errorf("oxPublishPubKey: iq failure publishing public key list: %w", err)
}
if iqReply.Type != strResult {
return errors.New("couldn't publish public key list")
return fmt.Errorf("couldn't publish public key list")
}
return nil
}
@ -392,7 +391,7 @@ func oxGetPrivKey(jid string, passphrase string) (*crypto.Key, error) {
log.Fatal("Ox: private key is locked.")
}
if key.IsExpired() {
return nil, errors.New("Ox: private key is expired: " + key.GetFingerprint())
return nil, fmt.Errorf("Ox: private key is expired: %s", key.GetFingerprint())
}
return key, nil
}
@ -478,7 +477,7 @@ func oxRecvPublicKeys(client *xmpp.Client, iqc chan xmpp.IQ, recipient string, f
return nil, fmt.Errorf("oxRecvPublicKeys: iq error requesting public keys: %w", err)
}
if oxPublicKey.Type != strResult {
return nil, errors.New("error while requesting public key for " +
return nil, fmt.Errorf("error while requesting public key for %s",
recipient)
}
oxPublicKeyXML := etree.NewDocument()
@ -492,18 +491,15 @@ func oxRecvPublicKeys(client *xmpp.Client, iqc chan xmpp.IQ, recipient string, f
}
oxPublicKeyXMLPubsub := oxPublicKeyXML.SelectElement("pubsub")
if oxPublicKeyXMLPubsub == nil {
return nil, errors.New("ox: no pubsub element in reply to public " +
"key request")
return nil, fmt.Errorf("ox: no pubsub element in reply to public key request")
}
oxPublicKeyXMLItems := oxPublicKeyXMLPubsub.SelectElement("items")
if oxPublicKeyXMLItems == nil {
return nil, errors.New("ox: no items element in reply to public " +
"key request")
return nil, fmt.Errorf("ox: no items element in reply to public key request")
}
oxPublicKeyXMLItem := oxPublicKeyXMLItems.SelectElement("item")
if oxPublicKeyXMLItem == nil {
return nil, errors.New("ox: no item element in reply to public " +
"key request")
return nil, fmt.Errorf("ox: no item element in reply to public key request")
}
oxPublicKeyXMLPubkeys := oxPublicKeyXMLItem.SelectElements("pubkey")
for _, r := range oxPublicKeyXMLPubkeys {
@ -520,7 +516,7 @@ func oxRecvPublicKeys(client *xmpp.Client, iqc chan xmpp.IQ, recipient string, f
return nil, fmt.Errorf("oxRecvPublicKeys: failed to decode public key: %w", err)
}
if key.IsExpired() {
return nil, errors.New("Key is expired: " + fingerprint)
return nil, fmt.Errorf("Key is expired: %s", fingerprint)
}
err = keyring.AddKey(key)
if err != nil {
@ -552,7 +548,7 @@ func oxGetPublicKeyRing(client *xmpp.Client, iqc chan xmpp.IQ, recipient string)
log.Fatal(err)
}
if oxPublicKeyList.Type != strResult {
return nil, errors.New("error while requesting public openpgp keys for " +
return nil, fmt.Errorf("error while requesting public openpgp keys for %s",
recipient)
}
oxPubKeyListXML := etree.NewDocument()
@ -569,19 +565,19 @@ func oxGetPublicKeyRing(client *xmpp.Client, iqc chan xmpp.IQ, recipient string)
oxPubKeyListXMLPubsub := oxPubKeyListXML.SelectElement("pubsub")
if oxPubKeyListXMLPubsub == nil {
return nil, errors.New("ox: no pubsub element in public key list")
return nil, fmt.Errorf("ox: no pubsub element in public key list")
}
oxPubKeyListXMLPubsubItems := oxPubKeyListXMLPubsub.SelectElement("items")
if oxPubKeyListXMLPubsubItems == nil {
return nil, errors.New("ox: no items element in public key list")
return nil, fmt.Errorf("ox: no items element in public key list")
}
oxPubKeyListXMLPubsubItemsItem := oxPubKeyListXMLPubsubItems.SelectElement("item")
if oxPubKeyListXMLPubsubItemsItem == nil {
return nil, errors.New("ox: no item element in public key list")
return nil, fmt.Errorf("ox: no item element in public key list")
}
oxPubKeyListXMLPubsubItemsItemPkl := oxPubKeyListXMLPubsubItemsItem.SelectElement("public-keys-list")
if oxPubKeyListXMLPubsubItemsItemPkl == nil {
return nil, errors.New("ox: no public-keys-list element")
return nil, fmt.Errorf("ox: no public-keys-list element")
}
oxPubKeyListXMLPubsubItemsItemPklPm := oxPubKeyListXMLPubsubItemsItemPkl.SelectElements("pubkey-metadata")
for _, r := range oxPubKeyListXMLPubsubItemsItemPklPm {
@ -603,7 +599,7 @@ func oxGetPublicKeyRing(client *xmpp.Client, iqc chan xmpp.IQ, recipient string)
}
}
if pubKeyRingID == "none" {
return nil, errors.New("server didn't provide public key fingerprints for " + recipient)
return nil, fmt.Errorf("server didn't provide public key fingerprints for %s", recipient)
}
pubKeyRingLocation, err := oxGetPubKeyLoc(pubKeyRingID)
@ -623,7 +619,7 @@ func oxGetPublicKeyRing(client *xmpp.Client, iqc chan xmpp.IQ, recipient string)
if !savedKeysDate.Before(newestKey) {
pubKeys := pubKeyReadXML.SelectElements("pubkey")
if pubKeys == nil {
return nil, errors.New("couldn't read public keys from cache")
return nil, fmt.Errorf("couldn't read public keys from cache")
}
for _, r := range pubKeys {
keyByte, err := base64.StdEncoding.DecodeString(r.Text())

@ -6,7 +6,6 @@ package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
@ -26,7 +25,7 @@ func findConfig() (string, error) {
// Get home directory.
home := curUser.HomeDir
if home == "" {
return "", errors.New("no home directory found")
return "", fmt.Errorf("findConfig: no home directory found")
}
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
if osConfigDir == "" {
@ -45,7 +44,7 @@ func findConfig() (string, error) {
return r, nil
}
}
return "", errors.New("no configuration file found")
return "", fmt.Errorf("findConfig: no configuration file found")
}
// Opens the config file and returns the specified values
@ -77,8 +76,7 @@ func parseConfig(configPath string) (configuration, error) {
perm := info.Mode().Perm()
permissions := strconv.FormatInt(int64(perm), 8)
if permissions != "600" && permissions != "640" && permissions != "440" && permissions != "400" {
return output, errors.New("Wrong permissions for " + configPath + ": " +
permissions + " instead of 400, 440, 600 or 640.")
return output, fmt.Errorf("parseConfig: wrong permissions for %s: %s instead of 400, 440, 600 or 640.", configPath, permissions)
}
}
@ -154,7 +152,7 @@ func parseConfig(configPath string) (configuration, error) {
// Check if the username is a valid JID now
output.username, err = MarshalJID(output.username)
if err != nil {
return output, errors.New("invalid username/JID: " + output.username)
return output, fmt.Errorf("parseConfig: invalid username/JID: %s", output.username)
}
}

@ -6,7 +6,6 @@ package main
import (
"context"
"errors"
"fmt"
"io"
"log"
@ -30,7 +29,7 @@ func sendIQ(client *xmpp.Client, iqc chan xmpp.IQ, target string, iQtype string,
select {
case iq = <-c:
case <-time.After(60 * time.Second):
return iq, errors.New("sendIQ: server didn't reply to IQ: " + content)
return iq, fmt.Errorf("sendIQ: server didn't reply to IQ: %s", content)
}
return iq, nil
}

Loading…
Cancel
Save