From 555cb064dd1d001e3ca8f0cdd75a971ca2367255 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Sun, 1 Aug 2021 21:38:35 +1000
Subject: [PATCH] cmd/torrentfs: Fix panic on missing info

---
 client.go             |  6 +++++-
 cmd/torrentfs/main.go |  2 +-
 spec.go               | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/client.go b/client.go
index 1f2af67f..ea8a4803 100644
--- a/client.go
+++ b/client.go
@@ -1323,7 +1323,11 @@ func (cl *Client) AddMagnet(uri string) (T *Torrent, err error) {
 }
 
 func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) {
-	T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
+	ts, err := TorrentSpecFromMetaInfoErr(mi)
+	if err != nil {
+		return
+	}
+	T, _, err = cl.AddTorrentSpec(ts)
 	return
 }
 
diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go
index dd66e989..adce6024 100644
--- a/cmd/torrentfs/main.go
+++ b/cmd/torrentfs/main.go
@@ -114,7 +114,7 @@ func mainErr() error {
 				if ev.TorrentFilePath != "" {
 					_, err := client.AddTorrentFromFile(ev.TorrentFilePath)
 					if err != nil {
-						log.Printf("error adding torrent to client: %s", err)
+						log.Printf("error adding torrent from file %q to client: %v", ev.TorrentFilePath, err)
 					}
 				} else if ev.MagnetURI != "" {
 					_, err := client.AddMagnet(ev.MagnetURI)
diff --git a/spec.go b/spec.go
index 7de7aa61..e12bb0cb 100644
--- a/spec.go
+++ b/spec.go
@@ -1,6 +1,8 @@
 package torrent
 
 import (
+	"fmt"
+
 	"github.com/anacrolix/torrent/metainfo"
 	"github.com/anacrolix/torrent/storage"
 )
@@ -46,10 +48,10 @@ func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
 	return
 }
 
-func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
+func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
 	info, err := mi.UnmarshalInfo()
 	if err != nil {
-		panic(err)
+		return nil, fmt.Errorf("unmarshalling info: %w", err)
 	}
 	return &TorrentSpec{
 		Trackers:    mi.UpvertedAnnounceList(),
@@ -64,5 +66,13 @@ func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
 			}
 			return
 		}(),
+	}, nil
+}
+
+func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
+	ts, err := TorrentSpecFromMetaInfoErr(mi)
+	if err != nil {
+		panic(err)
 	}
+	return ts
 }
-- 
2.51.0