Skip to content

Commit 7d7c55a

Browse files
committed
Make error output more helpful
1 parent d5efbe3 commit 7d7c55a

File tree

8 files changed

+130
-61
lines changed

8 files changed

+130
-61
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog (command line tool)
22

3+
## v1.2
4+
- **[ INFO ]** Provided more helpful error messages
5+
- **[ FIX ]** Fix unhandled error with experimental `template` subcommand
6+
(introduced in v1.1)
7+
38
## v1.1
49
- **[ INFO ]** Introduced hidden and experimental `template` subcommand,
510
see https://github.com/jotaen/klog/pull/12

src/app/cli/cmd_append.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ type Append struct {
1212
func (args *Append) Run(ctx app.Context) error {
1313
target := args.File
1414
if target == "" {
15-
target = ctx.Bookmark().Path
15+
b, err := ctx.Bookmark()
16+
if err != nil {
17+
return err
18+
}
19+
target = b.Path
1620
}
1721
return ctx.AppendTemplateToFile(target, args.From)
1822
}

src/app/cli/cmd_bookmark.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ type Bookmark struct {
1010

1111
func (args *Bookmark) Run(ctx app.Context) error {
1212
if args.File == "" {
13-
file := ctx.Bookmark()
14-
if file == nil {
15-
ctx.Print("Bookmark is empty\n")
16-
return nil
13+
b, err := ctx.Bookmark()
14+
if err != nil {
15+
return err
1716
}
18-
ctx.Print("Current bookmark: " + file.Path + "\n")
17+
ctx.Print("Current bookmark: " + b.Path + "\n")
1918
return nil
2019
}
2120
err := ctx.SetBookmark(args.File)

src/app/cli/exec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ func prettifyError(err error) error {
9292
) + "\n\n"
9393
}
9494
return errors.New(message)
95+
case app.Error:
96+
return errors.New("Error: " + e.Error() + "\n" + e.Help())
9597
}
96-
return err
98+
return errors.New("Error: " + err.Error())
9799
}
98100

99101
func breakLines(text string, maxLength int) []string {

src/app/cli/testutil_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli
22

33
import (
4-
"errors"
54
"klog"
65
"klog/app"
76
"klog/parser"
@@ -57,22 +56,22 @@ func (m *TestContext) RetrieveRecords(_ ...string) ([]klog.Record, error) {
5756
return m.records, nil
5857
}
5958

60-
func (m *TestContext) SetBookmark(_ string) error {
59+
func (m *TestContext) SetBookmark(_ string) app.Error {
6160
return nil
6261
}
6362

64-
func (m *TestContext) Bookmark() *app.File {
63+
func (m *TestContext) Bookmark() (*app.File, app.Error) {
6564
return &app.File{
6665
Name: "myfile.klg",
6766
Location: "/",
6867
Path: "/myfile.klg",
69-
}
68+
}, nil
7069
}
7170

72-
func (m *TestContext) OpenInFileBrowser(_ string) error {
71+
func (m *TestContext) OpenInFileBrowser(_ string) app.Error {
7372
return nil
7473
}
7574

76-
func (m *TestContext) AppendTemplateToFile(string, string) error {
77-
return errors.New("No such template")
75+
func (m *TestContext) AppendTemplateToFile(string, string) app.Error {
76+
return nil
7877
}

src/app/context.go

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package app
22

33
import (
4-
"errors"
54
"fmt"
65
"io/ioutil"
76
"klog"
@@ -27,10 +26,10 @@ type Context interface {
2726
BuildHash string
2827
}
2928
RetrieveRecords(...string) ([]klog.Record, error)
30-
SetBookmark(string) error
31-
Bookmark() *File
32-
OpenInFileBrowser(string) error
33-
AppendTemplateToFile(string, string) error
29+
SetBookmark(string) Error
30+
Bookmark() (*File, Error)
31+
OpenInFileBrowser(string) Error
32+
AppendTemplateToFile(string, string) Error
3433
}
3534

3635
type context struct {
@@ -91,11 +90,14 @@ func (c *context) MetaInfo() struct {
9190

9291
func (c *context) RetrieveRecords(paths ...string) ([]klog.Record, error) {
9392
if len(paths) == 0 {
94-
if b := c.Bookmark(); b != nil {
95-
paths = []string{b.Path}
96-
} else {
97-
return nil, errors.New("No input file(s) specified; couldn’t read from bookmarked file either.")
93+
b, err := c.Bookmark()
94+
if err != nil {
95+
return nil, appError{
96+
"No input files specified",
97+
"Either specify input files, or set a bookmark",
98+
}
9899
}
100+
paths = []string{b.Path}
99101
}
100102
var records []klog.Record
101103
for _, p := range paths {
@@ -118,84 +120,123 @@ type File struct {
118120
Path string
119121
}
120122

121-
func (c *context) Bookmark() *File {
123+
func (c *context) Bookmark() (*File, Error) {
122124
bookmarkPath := c.KlogFolder() + "bookmark.klg"
123125
dest, err := os.Readlink(bookmarkPath)
124126
if err != nil {
125-
return nil
127+
return nil, appError{
128+
"No bookmark set",
129+
"You can set a bookmark by running: klog bookmark somefile.klg",
130+
}
126131
}
127132
_, err = os.Stat(dest)
128133
if err != nil {
129-
return nil
134+
return nil, appError{
135+
"Bookmark doesn’t point to valid file",
136+
"Please check the current bookmark location or set a new one",
137+
}
130138
}
131139
return &File{
132140
Name: filepath.Base(dest),
133141
Location: filepath.Dir(dest),
134142
Path: dest,
135-
}
143+
}, nil
136144
}
137145

138-
func (c *context) SetBookmark(path string) error {
146+
func (c *context) SetBookmark(path string) Error {
139147
bookmark, err := filepath.Abs(path)
140148
if err != nil {
141-
return errors.New("Target file does not exist")
149+
return appError{
150+
"Invalid target file",
151+
"Please check the file path",
152+
}
142153
}
143154
if !strings.HasSuffix(bookmark, ".klg") {
144-
return errors.New("File name must have .klg extension")
155+
return appError{
156+
"Invalid file extension",
157+
"File name must have .klg extension",
158+
}
145159
}
146160
klogFolder := c.KlogFolder()
147161
err = os.MkdirAll(klogFolder, 0700)
148162
if err != nil {
149-
return errors.New("Unable to initialise ~/.klog folder")
163+
return appError{
164+
"Unable to initialise ~/.klog folder",
165+
"Please create a ~/.klog folder manually",
166+
}
150167
}
151168
symlink := klogFolder + "/bookmark.klg"
152169
_ = os.Remove(symlink)
153170
err = os.Symlink(bookmark, symlink)
154171
if err != nil {
155-
return errors.New("Failed to create bookmark")
172+
return appError{
173+
"Failed to create bookmark",
174+
"",
175+
}
156176
}
157177
return nil
158178
}
159179

160-
func (c *context) OpenInFileBrowser(path string) error {
180+
func (c *context) OpenInFileBrowser(path string) Error {
161181
cmd := exec.Command("open", path)
162-
return cmd.Run()
182+
err := cmd.Run()
183+
if err != nil {
184+
return appError{
185+
"Failed to open file browser",
186+
err.Error(),
187+
}
188+
}
189+
return nil
163190
}
164191

165-
func (c *context) AppendTemplateToFile(filePath string, templateName string) error {
192+
func (c *context) AppendTemplateToFile(filePath string, templateName string) Error {
166193
location := c.KlogFolder() + templateName + ".template.klg"
167194
template, err := readFile(location)
168195
if err != nil {
169-
return errors.New("No such template: " + location)
196+
return appError{
197+
"No such template",
198+
"There is no template at location " + location,
199+
}
170200
}
171-
instance, err := service.RenderTemplate(template, time.Now())
172-
if err != nil {
173-
return err
201+
instance, tErr := service.RenderTemplate(template, time.Now())
202+
if tErr != nil {
203+
return appError{
204+
"Invalid template",
205+
tErr.Error(),
206+
}
174207
}
175208
contents, err := readFile(filePath)
176209
if err != nil {
177210
return err
178211
}
179-
err = appendToFile(filePath, service.AppendableText(contents, instance))
180-
return err
212+
return appendToFile(filePath, service.AppendableText(contents, instance))
181213
}
182214

183-
func readFile(path string) (string, error) {
215+
func readFile(path string) (string, Error) {
184216
contents, err := ioutil.ReadFile(path)
185217
if err != nil {
186-
return "", errors.New("Cannot read file: " + path)
218+
return "", appError{
219+
"Cannot read file",
220+
"Location: " + path,
221+
}
187222
}
188223
return string(contents), nil
189224
}
190225

191-
func appendToFile(path string, textToAppend string) error {
226+
func appendToFile(path string, textToAppend string) Error {
192227
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
193228
if err != nil {
194-
return errors.New("Cannot write to file: " + path)
229+
return appError{
230+
"Cannot write to file",
231+
"Location: " + path,
232+
}
195233
}
196234
defer file.Close()
197235
if _, err := file.WriteString(textToAppend); err != nil {
198-
return errors.New("Cannot write to file: " + path)
236+
return appError{
237+
"Cannot write to file",
238+
"Location: " + path,
239+
}
199240
}
200241
return nil
201242
}

src/app/error.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package app
2+
3+
type Error interface {
4+
Error() string
5+
Help() string
6+
}
7+
8+
type appError struct {
9+
message string
10+
help string
11+
}
12+
13+
func (e appError) Error() string {
14+
return e.message
15+
}
16+
17+
func (e appError) Help() string {
18+
return e.help
19+
}

src/app/mac_widget/render_darwin.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ func render(ctx app.Context, agent *launchAgent) []menuet.MenuItem {
1515
var items []menuet.MenuItem
1616

1717
items = append(items, func() []menuet.MenuItem {
18-
if file := ctx.Bookmark(); file != nil {
19-
rs, err := ctx.RetrieveRecords()
20-
if err == nil {
21-
return renderRecords(ctx, rs, file)
22-
}
18+
file, err := ctx.Bookmark()
19+
if err != nil {
2320
return []menuet.MenuItem{{
24-
Text: "Bookmarked file invalid",
21+
Text: "No bookmark specified",
2522
FontWeight: menuet.WeightBold,
2623
}, {
27-
Text: "Please fix the syntax errors",
24+
Text: "Bookmark a file by running:",
25+
}, {
26+
Text: "klog bookmark yourfile.klg",
27+
}}
28+
}
29+
rs, pErr := ctx.RetrieveRecords()
30+
if pErr != nil {
31+
return []menuet.MenuItem{{
32+
Text: file.Name,
33+
}, {
34+
Text: "Error: file cannot be parsed",
2835
}}
2936
}
30-
return []menuet.MenuItem{{
31-
Text: "No bookmark specified",
32-
FontWeight: menuet.WeightBold,
33-
}, {
34-
Text: "Bookmark a file by running:",
35-
}, {
36-
Text: "klog bookmark yourfile.klg",
37-
}}
37+
return renderRecords(ctx, rs, file)
3838
}()...)
3939

4040
items = append(items, menuet.MenuItem{

0 commit comments

Comments
 (0)