Skip to content

Commit

Permalink
changed error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 6, 2018
1 parent 840fd85 commit 744d964
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 68 deletions.
10 changes: 6 additions & 4 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ func requestSearch() string {
res, err := prompt.Run()

if err != nil {
fmt.Println("Aborting")
os.Exit(1)
panic("aborting")
}

return res
Expand Down Expand Up @@ -303,7 +302,7 @@ func addDOI(doi string) *scholar.Entry {
panic(err)
}

e := scholar.Parse(w)
e := parseCrossref(w)

return e
}
Expand Down Expand Up @@ -361,7 +360,10 @@ func selectType() string {
}

func add(entryType string) *scholar.Entry {
entry := scholar.NewEntry(entryType)
entry, err := scholar.NewEntry(entryType)
if err != nil {
panic(err)
}

reader := bufio.NewReader(os.Stdin)
for field := range entry.Required {
Expand Down
6 changes: 5 additions & 1 deletion cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ Edit an entry's metadata using the default's text editor.
return
}
if editType != "" {
entry = scholar.Convert(entry, editType)
var err error
entry, err = scholar.Convert(entry, editType)
if err != nil {
panic(err)
}
update(entry)
}
edit(entry)
Expand Down
63 changes: 53 additions & 10 deletions cmd/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"sync"

"github.com/cgxeiji/crossref"
"github.com/cgxeiji/scholar/scholar"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -126,7 +127,7 @@ func libraryPath() string {
fmt.Println(" ", k)
fmt.Println(" ", v)
}
os.Exit(1)
panic("not found: library")
}

return viper.Sub("LIBRARIES").GetString(currentLibrary)
Expand All @@ -146,7 +147,7 @@ Add an entry to create this directory or run:
to set the correct path of this library.
`)
os.Exit(1)
panic("not found: library path")
}

var wg sync.WaitGroup
Expand Down Expand Up @@ -216,18 +217,60 @@ func queryEntry(search string) *scholar.Entry {
entry = guiQuery(entryList(), search)
} else {
found := guiSearch(search, entryList(), searcher)
if len(found) == 1 {
switch len(found) {
case 0:
panic("no entries found")
case 1:
entry = found[0]
} else {
fmt.Println("Found", len(found), "entries.")
fmt.Println("Please, refine your query.")
os.Exit(1)
default:
panic(fmt.Errorf("too many entries (%d) matched\nplease, refine your query", len(found)))
}
}

if entry == nil {
fmt.Println("No entries match the query.")
return entry
}

func parseCrossref(work *crossref.Work) *scholar.Entry {
var e *scholar.Entry
var err error

switch work.Type {
case "journal-article":
if e, err = scholar.NewEntry("article"); err != nil {
panic(err)
}
e.Required["journaltitle"] = work.BookTitle
e.Optional["issn"] = work.ISSN
case "proceedings-article":
if e, err = scholar.NewEntry("inproceedings"); err != nil {
panic(err)
}
e.Required["booktitle"] = work.BookTitle
e.Optional["isbn"] = work.ISBN
e.Optional["publisher"] = work.Publisher
default:
if e, err = scholar.NewEntry("article"); err != nil {
panic(err)
}
e.Required["journaltitle"] = work.BookTitle
}

return entry
for _, a := range work.Authors {
e.Required["author"] = fmt.Sprintf("%s%s, %s and ", e.Required["author"], a.Last, a.First)
}
e.Required["author"] = strings.TrimSuffix(e.Required["author"], " and ")

e.Required["date"] = work.Date
e.Required["title"] = work.Title

for _, a := range work.Editors {
e.Optional["editor"] = fmt.Sprintf("%s%s, %s and ", e.Optional["editor"], a.Last, a.First)
}
e.Optional["editor"] = strings.TrimSuffix(e.Optional["editor"], " and ")

e.Optional["volume"] = work.Volume
e.Optional["number"] = work.Issue
e.Optional["doi"] = work.DOI

return e
}
12 changes: 11 additions & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ func importParse(filename string) {
var es []*scholar.Entry

for _, entry := range entries {
e := scholar.NewEntry(entry["type"])
e, err := scholar.NewEntry(entry["type"])
if err != nil {
if err == scholar.TypeNotFoundError {
e, err = scholar.NewEntry("misc")
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
delete(entry, "type")
e.Key = entry["key"]
delete(entry, "key")
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ your documents metadata using YAML files with biblatex format.`,
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
defer func() {
if r := recover(); r != nil {
fmt.Println("error:", r)
os.Exit(1)
}
}()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
9 changes: 6 additions & 3 deletions scholar/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ func (e *Entry) GetKey() string {
// type to the other.
// If the entry does not exits, it defaults to misc entry.
// TODO: create a ErrFieldNotFound error
func Convert(e *Entry, entryType string) *Entry {
to := NewEntry(entryType)
func Convert(e *Entry, entryType string) (*Entry, error) {
to, err := NewEntry(entryType)
if err != nil {
return to, err
}
to.Key = e.Key
to.Attach(e.File)

Expand Down Expand Up @@ -145,7 +148,7 @@ func Convert(e *Entry, entryType string) *Entry {
}
}

return to
return to, nil
}

// Bib returns a string with all the information of the entry
Expand Down
13 changes: 13 additions & 0 deletions scholar/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package scholar

import "fmt"

type tnfError struct {
requested string
}

func (e *tnfError) Error() string {
return fmt.Sprintf("not found: type %s", e.requested)
}

var TypeNotFoundError *tnfError
54 changes: 5 additions & 49 deletions scholar/scholar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"io/ioutil"
"sort"
"strings"

"github.com/cgxeiji/crossref"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -49,54 +47,12 @@ func TypesInfo(level int) {

// NewEntry returns an empty copy of an entry according to the types of entries
// loaded.
func NewEntry(label string) *Entry {
func NewEntry(label string) (*Entry, error) {
if entryType, ok := EntryTypes[label]; ok {
// Return the entry type only if it exists
return entryType.get()
return entryType.get(), nil
}
// Otherwise, default to misc type
return EntryTypes["misc"].get()
}

// Parse parses the information for a work to an entry.
func Parse(work *crossref.Work) *Entry {
return parseCrossref(work)
}

func parseCrossref(work *crossref.Work) *Entry {
var e *Entry

switch work.Type {
case "journal-article":
e = NewEntry("article")
e.Required["journaltitle"] = work.BookTitle
e.Optional["issn"] = work.ISSN
case "proceedings-article":
e = NewEntry("inproceedings")
e.Required["booktitle"] = work.BookTitle
e.Optional["isbn"] = work.ISBN
e.Optional["publisher"] = work.Publisher
default:
e = NewEntry("article")
e.Required["journaltitle"] = work.BookTitle
}

for _, a := range work.Authors {
e.Required["author"] = fmt.Sprintf("%s%s, %s and ", e.Required["author"], a.Last, a.First)
}
e.Required["author"] = strings.TrimSuffix(e.Required["author"], " and ")

e.Required["date"] = work.Date
e.Required["title"] = work.Title

for _, a := range work.Editors {
e.Optional["editor"] = fmt.Sprintf("%s%s, %s and ", e.Optional["editor"], a.Last, a.First)
}
e.Optional["editor"] = strings.TrimSuffix(e.Optional["editor"], " and ")

e.Optional["volume"] = work.Volume
e.Optional["number"] = work.Issue
e.Optional["doi"] = work.DOI

return e
// Otherwise, send a type not found error
TypeNotFoundError = &tnfError{label}
return nil, TypeNotFoundError
}

0 comments on commit 744d964

Please sign in to comment.