diff options
| author | Quey-Liang Kao <s101062801@m101.nthu.edu.tw> | 2017-01-19 03:19:48 +0800 |
|---|---|---|
| committer | Quey-Liang Kao <s101062801@m101.nthu.edu.tw> | 2017-01-19 03:28:23 +0800 |
| commit | eb807193d3ddba1c6037dc100cb422db3d68e07e (patch) | |
| tree | 3c2479c3c8aab968fb266a93f1d0b9467e4a60e2 /todolist/parser.go | |
| parent | de346783c83061b2b88e5b158f6a659d569f206e (diff) | |
Makes all output messages consistent
Since the panic() function provides overwhelming messages available
for developers but meaningless to users, the way to deal with error
output is simplified.
There are two panic()'s that are fixed:
* The one in parseArbitraryDateWithYear() function.
This function is the possible endpoint of adding a todo or editing
dues. Instead of calling panic(), now it outputs the specific
parsing error, and then terminate itself by os.Exit().
* The one in formatDue() function
This function is responsible for parsing the due information of a
given todo. If the todos being added previously have passed the
checking, then there is no reason that a due fails the format
check here, except for the corruption of the todo file.
Diffstat (limited to 'todolist/parser.go')
| -rw-r--r-- | todolist/parser.go | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/todolist/parser.go b/todolist/parser.go index 767785b..b16dccb 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -2,6 +2,7 @@ package todolist import ( "fmt" + "os" "regexp" "strconv" "strings" @@ -109,14 +110,21 @@ func (p *Parser) parseArbitraryDate(_date string, pivot time.Time) string { func (p *Parser) parseArbitraryDateWithYear(_date string, year int) time.Time { res := strings.Join([]string{_date, strconv.Itoa(year)}, " ") - if date, err := time.Parse("Jan 2 2006", res); err == nil { + var date time.Time + var err1 error + var err2 error + if date, err1 = time.Parse("Jan 2 2006", res); err1 == nil { return date } - if date, err := time.Parse("2 Jan 2006", res); err == nil { + if date, err2 = time.Parse("2 Jan 2006", res); err2 == nil { return date } - panic(fmt.Errorf("Could not parse the date you gave me: '%s'", _date)) + fmt.Printf("Could not parse the date you gave me: %s\n", _date) + fmt.Println(err1) + fmt.Println(err2) + os.Exit(-1) + return time.Now() } func (p *Parser) monday(day time.Time) string { |
