Skip to content

Commit

Permalink
chore: back to pkgerrors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhcmrlchtdj committed Mar 3, 2024
1 parent 15ef3fc commit e5c72f3
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 196 deletions.
22 changes: 2 additions & 20 deletions cmd/feedbox/init_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"os"
"os/signal"
"strconv"
"syscall"

"github.com/joho/godotenv"
"github.com/morikuni/failure"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"

"github.com/dhcmrlchtdj/feedbox/internal/database"
"github.com/dhcmrlchtdj/feedbox/internal/email"
Expand All @@ -29,24 +28,7 @@ func initEnv() {
}

func initLogger() {
zerolog.ErrorStackMarshaler = marshalStack // nolint:reassign
}

func marshalStack(err error) interface{} {
cs, ok := failure.CallStackOf(err)
if !ok {
return nil
}
frames := cs.Frames()
out := make([]map[string]string, 0, len(frames))
for _, frame := range frames {
out = append(out, map[string]string{
"path": frame.Path(),
"line": strconv.Itoa(frame.Line()),
"func": frame.Func(),
})
}
return out
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack // nolint:reassign
}

func initDatabase(ctx context.Context) {
Expand Down
38 changes: 17 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ go 1.22

require (
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
github.com/gofiber/fiber/v2 v2.52.0
github.com/gofiber/fiber/v2 v2.52.2
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/joho/godotenv v1.5.1
github.com/mmcdole/gofeed v1.2.1
github.com/morikuni/failure v1.1.2
github.com/mmcdole/gofeed v1.3.0
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.32.0
github.com/zeebo/xxh3 v1.0.2
golang.org/x/crypto v0.18.0
golang.org/x/oauth2 v0.16.0
golang.org/x/crypto v0.20.0
golang.org/x/oauth2 v0.17.0
golang.org/x/text v0.14.0
modernc.org/sqlite v1.28.0
modernc.org/sqlite v1.29.2
)

require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/PuerkitoBio/goquery v1.9.1 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -28,37 +28,33 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mmcdole/goxpp v1.1.0 // indirect
github.com/mmcdole/goxpp v1.1.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.41.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
92 changes: 35 additions & 57 deletions go.sum

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions internal/database/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"time"

"github.com/morikuni/failure"
"github.com/pkg/errors"
)

type User struct {
Expand All @@ -20,6 +20,7 @@ type Feed struct {
ETag string `json:"etag"`
}

var ErrEmptyRow failure.StringCode = "EmptyRow"

var ErrInvalidURL failure.StringCode = "InvalidUrl"
var (
ErrEmptyRow = errors.New("empty row")
ErrInvalidURL = errors.New("invalid url")
)
4 changes: 2 additions & 2 deletions internal/database/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/dhcmrlchtdj/feedbox/internal/database/common"
"github.com/dhcmrlchtdj/feedbox/internal/database/sqlite"
"github.com/morikuni/failure"
"github.com/pkg/errors"
)

type Database interface {
Expand Down Expand Up @@ -48,5 +48,5 @@ func New(ctx context.Context, uri string) (Database, error) {
return sqlite.New(ctx, uri)
}

return nil, failure.Unexpected("unknown database url")
return nil, errors.New("unknown database url")
}
11 changes: 6 additions & 5 deletions internal/database/sqlite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/morikuni/failure"
"github.com/pkg/errors"
"github.com/rs/xid"
"github.com/rs/zerolog"
_ "modernc.org/sqlite"
Expand All @@ -19,7 +19,7 @@ type Database struct {

func New(ctx context.Context, uri string) (*Database, error) {
if !strings.HasPrefix(uri, "sqlite://") {
return nil, failure.Unexpected("invalid DATABASE_URL", failure.Message(uri))
return nil, errors.WithMessage(errors.New("invalid DATABASE_URL"), uri)
}
dbURI := uri[9:]
db, err := sql.Open("sqlite", dbURI)
Expand Down Expand Up @@ -67,8 +67,8 @@ func (db *Database) Exec(ctx context.Context, query string, args ...any) (sql.Re
latency := time.Since(start)
logger.Trace().Dur("latency", latency).Send()
}()

return db.db.ExecContext(ctx, query, args...)
r, err := db.db.ExecContext(ctx, query, args...)
return r, errors.WithStack(err)
}

func (db *Database) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
Expand All @@ -85,7 +85,8 @@ func (db *Database) Query(ctx context.Context, query string, args ...any) (*sql.
latency := time.Since(start)
logger.Trace().Dur("latency", latency).Send()
}()
return db.db.QueryContext(ctx, query, args...)
r, err := db.db.QueryContext(ctx, query, args...)
return r, errors.WithStack(err)
}

func (db *Database) QueryRow(ctx context.Context, query string, args ...any) *sql.Row {
Expand Down
21 changes: 10 additions & 11 deletions internal/database/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
netUrl "net/url"
"time"

"github.com/pkg/errors"
_ "modernc.org/sqlite"

"github.com/dhcmrlchtdj/feedbox/internal/database/common"
"github.com/morikuni/failure"
)

type (
Expand Down Expand Up @@ -39,7 +38,7 @@ func (db *Database) GetUserByID(ctx context.Context, id int64) (*User, error) {
return nil, err
}
if user == nil {
return nil, failure.New(ErrEmptyRow)
return nil, errors.WithStack(ErrEmptyRow)
}

return user, nil
Expand All @@ -48,7 +47,7 @@ func (db *Database) GetUserByID(ctx context.Context, id int64) (*User, error) {
func (db *Database) GetOrCreateUserByGithub(ctx context.Context, githubID string, email string) (*User, error) {
addition, err := json.Marshal(map[string]string{"email": email})
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
row := db.QueryRow(
ctx,
Expand Down Expand Up @@ -96,7 +95,7 @@ func (db *Database) GetOrCreateUserByTelegram(ctx context.Context, chatID string

func (db *Database) GetFeedIDByURL(ctx context.Context, url string) (int64, error) {
if !isValidURL(url) {
return 0, failure.New(ErrInvalidURL)
return 0, errors.WithStack(ErrInvalidURL)
}

_, err := db.Exec(ctx, `INSERT OR IGNORE INTO feeds(url) VALUES ($1)`, url)
Expand Down Expand Up @@ -300,12 +299,12 @@ func readUser(row *sql.Row) (*User, error) {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
return nil, errors.WithStack(err)
}

err = json.Unmarshal(addition, &user.Addition)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

return &user, nil
Expand All @@ -318,11 +317,11 @@ func readUsers(rows *sql.Rows) ([]User, error) {
var addition []byte
err := rows.Scan(&user.ID, &user.Platform, &user.PID, &addition)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
err = json.Unmarshal(addition, &user.Addition)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
users = append(users, user)
}
Expand All @@ -336,7 +335,7 @@ func readFeeds(rows *sql.Rows) ([]Feed, error) {
var ts *int64
err := rows.Scan(&feed.ID, &feed.URL, &ts, &feed.ETag)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
feed.Updated = parseTime(ts)
feeds = append(feeds, feed)
Expand All @@ -359,7 +358,7 @@ func readLinks(rows *sql.Rows) ([]string, error) {
var link string
err := rows.Scan(&link)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
links = append(links, link)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/email/mailchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"net/http"

"github.com/morikuni/failure"
"github.com/pkg/errors"
)

var _ Client = (*mailchannels)(nil)
Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *mailchannels) buildSendPayload(addr string, subject string, text string
func (c *mailchannels) Send(ctx context.Context, addr string, subject string, text string) error {
payload, err := json.Marshal(c.buildSendPayload(addr, subject, text))
if err != nil {
return err
return errors.WithStack(err)
}

req, err := http.NewRequestWithContext(
Expand All @@ -54,19 +54,19 @@ func (c *mailchannels) Send(ctx context.Context, addr string, subject string, te
bytes.NewBuffer(payload),
)
if err != nil {
return err
return errors.WithStack(err)
}
req.SetBasicAuth(c.username, c.password)
req.Header.Set("content-type", "application/json")

resp, err := c.client.Do(req)
if err != nil {
return err
return errors.WithStack(err)
}
defer resp.Body.Close()

if resp.StatusCode != 202 {
return failure.Unexpected("mailchannels", failure.Message(resp.Status))
return errors.WithMessage(errors.New("mailchannels"), resp.Status)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/email/mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"net/http"

"github.com/morikuni/failure"
"github.com/pkg/errors"

"github.com/dhcmrlchtdj/feedbox/internal/multipart"
)
Expand Down Expand Up @@ -61,14 +61,14 @@ func (c *mailgun) Send(ctx context.Context, addr string, subject string, text st
defer resp.Body.Close()

if resp.StatusCode != 200 {
e := failure.Unexpected(resp.Status)
e := errors.New(resp.Status)
var r struct {
Message string `json:"message,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return failure.Wrap(e, failure.Message(err.Error()))
return errors.WithMessage(e, err.Error())
}
return failure.Wrap(e, failure.Message(r.Message))
return errors.WithMessage(e, r.Message)
}

return nil
Expand Down
Loading

0 comments on commit e5c72f3

Please sign in to comment.