Skip to content

Commit

Permalink
Use pkg/errors to add error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ohtomi committed Feb 3, 2017
1 parent 6d867d6 commit ae67ac1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
17 changes: 9 additions & 8 deletions command/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -14,6 +13,8 @@ import (
"regexp"
"strings"
"time"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -62,12 +63,12 @@ func createQueryResultFile(host, project string, tags []string, skip, limit int)

directory := path.Join(ScrapboxHome, "query", trimPortFromHost(host), project, path.Join(tags...))
if err := os.MkdirAll(directory, os.ModePerm); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make query cache directory")
}
filepath := path.Join(directory, EncodeFilename(fmt.Sprintf("%d-%d", skip, limit)))
fout, err := os.Create(filepath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to create query cache file")
}

return fout, nil
Expand Down Expand Up @@ -97,7 +98,7 @@ func openQueryResultFile(host, project string, tags []string, skip, limit int) (
filepath := path.Join(directory, EncodeFilename(fmt.Sprintf("%d-%d", skip, limit)))
fin, err := os.Open(filepath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to open query cache file")
}

return fin, nil
Expand All @@ -107,12 +108,12 @@ func createPageFile(host, project, page string) (*os.File, error) {

directory := path.Join(ScrapboxHome, "page", trimPortFromHost(host), project)
if err := os.MkdirAll(directory, os.ModePerm); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to make page cache directory")
}
filepath := path.Join(directory, EncodeFilename(page))
fout, err := os.Create(filepath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to create page cache file")
}

return fout, nil
Expand Down Expand Up @@ -142,7 +143,7 @@ func openPageFile(host, project, page string) (*os.File, error) {
filepath := path.Join(directory, EncodeFilename(page))
fin, err := os.Open(filepath)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to open page cache file")
}

return fin, nil
Expand Down Expand Up @@ -173,7 +174,7 @@ func (c *Client) newRequest(ctx context.Context, method, spath string, body io.R

req, err := http.NewRequest(method, u, body)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to instantiate http request")
}

req = req.WithContext(ctx)
Expand Down
4 changes: 3 additions & 1 deletion command/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/url"
"os"
"strings"

"github.com/pkg/errors"
)

type LinkCommand struct {
Expand All @@ -17,7 +19,7 @@ func (c *LinkCommand) FetchAllLinks(client *Client, project, page string) ([]str

p, err := client.GetPage(context.Background(), project, page)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get page")
}

return p.ExtractExternalLinks(), nil
Expand Down
6 changes: 4 additions & 2 deletions command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/url"
"os"
"strings"

"github.com/pkg/errors"
)

type ListCommand struct {
Expand All @@ -17,7 +19,7 @@ func (c *ListCommand) FetchRelatedPages(client *Client, project string, tags []s

q, err := client.ExecQuery(context.Background(), project, tags, 0, 100)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to execute query")
}

return q.Pages, nil
Expand Down Expand Up @@ -67,7 +69,7 @@ func (c *ListCommand) Run(args []string) int {

parsedURL, err := url.ParseRequestURI(host)
if err != nil {
c.Ui.Error("failed to parse url: " + host)
c.Ui.Error(fmt.Sprintf("failed to parse the url. host: %s, cause: %s", host, err))
return int(ExitCodeInvalidURL)
}

Expand Down
4 changes: 3 additions & 1 deletion command/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/url"
"os"
"strings"

"github.com/pkg/errors"
)

type ReadCommand struct {
Expand All @@ -17,7 +19,7 @@ func (c *ReadCommand) FetchContent(client *Client, project, page string) ([]stri

p, err := client.GetPage(context.Background(), project, page)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get page")
}

return p.Lines, nil
Expand Down

0 comments on commit ae67ac1

Please sign in to comment.