Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor interface -> any, new errors format etc #54

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Decode decodes the body of a request into the specified object.
// If the object implements the OK interface, that method is called
// to validate the object.
func Decode(r *http.Request, v interface{}) error {
func Decode(r *http.Request, v any) error {
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
return &ErrJSON{err}
Expand Down
9 changes: 4 additions & 5 deletions api/respond.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

gmerrors "github.com/graymeta/gmkit/errors"
"github.com/graymeta/gmkit/http/middleware"
"github.com/graymeta/gmkit/logger"

"github.com/pkg/errors"
)

// Responder writes API responses.
Expand Down Expand Up @@ -49,7 +48,7 @@ func Header(key, value string) Option {
const HeaderAPIVersion = `X-Api-Version`

// With responds with the specified data.
func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, data interface{}, opts ...Option) {
func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, data any, opts ...Option) {
var buf bytes.Buffer
// cannot write to buf if data is nil, in case of StatusNoContent, this write will fail
// so we need an escape hatch here.
Expand All @@ -58,7 +57,7 @@ func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, d
enc.SetIndent("", "\t")
err := enc.Encode(data)
if err != nil {
err = errors.Wrap(err, "failed to encode response object")
err = fmt.Errorf("failed to encode response object: %w", err)
r.Err(w, req, err)
return
}
Expand All @@ -74,7 +73,7 @@ func (r *Responder) With(w http.ResponseWriter, req *http.Request, status int, d
}
w.WriteHeader(status)
if _, err := io.Copy(w, &buf); err != nil {
err = errors.Wrap(err, "failed to copy response bytes")
err = fmt.Errorf("failed to copy response bytes: %w", err)
r.log.Err("api_response", err)
}
}
Expand Down
9 changes: 5 additions & 4 deletions backoff/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type Runner struct {
// New returns a runner with the defined options. If no options are given,
// then the new runner is given the defaults for initial and max backoff as well
// as max calls. The defaults being:
// InitBackoff: 1 second
// MaxBackoff: 1 minute
// MaxCalls: 10
// Jitter: false
//
// InitBackoff: 1 second
// MaxBackoff: 1 minute
// MaxCalls: 10
// Jitter: false
func New(opts ...RunnerOptFn) Runner {
r := Runner{
initDur: defaultInitialBackoff,
Expand Down
9 changes: 5 additions & 4 deletions crypter/aes_crypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package crypter

import (
"encoding/base64"
"errors"
"fmt"

"github.com/gtank/cryptopasta"
"github.com/pkg/errors"
)

// ErrInvalidAESKeyLength is the error returned when the encryption key isn't
// the correct length
var ErrInvalidAESKeyLength = errors.New("Invalid key length")
var ErrInvalidAESKeyLength = errors.New("invalid key length")

type aesCrypter struct {
key *[32]byte
Expand Down Expand Up @@ -42,12 +43,12 @@ func (c *aesCrypter) Encrypt(data string) (string, error) {
func (c *aesCrypter) Decrypt(ciphertext string) (string, error) {
unencoded, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", errors.Wrap(err, "decoding base64 ciphertext")
return "", fmt.Errorf("decoding base64 ciphertext: %w", err)
}

plaintext, err := cryptopasta.Decrypt([]byte(unencoded), c.key)
if err != nil {
return "", errors.Wrap(err, "decrypting ciphertext")
return "", fmt.Errorf("decrypting ciphertext: %w", err)
}

return string(plaintext), nil
Expand Down
6 changes: 3 additions & 3 deletions errors/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package errors
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -53,15 +53,15 @@ func NewClientErr(op string, err error, resp *http.Response, opts ...ClientOptFn
newClientErr.method = req.Method

if req.Header != nil && strings.Contains(req.Header.Get("Content-Type"), "application/json") {
if body, err := ioutil.ReadAll(req.Body); err == nil {
if body, err := io.ReadAll(req.Body); err == nil {
newClientErr.respBody = string(body)
}
}
}
newClientErr.StatusCode = resp.StatusCode
newClientErr.reqID = resp.Header.Get(middleware.RequestHeader)

if body, err := ioutil.ReadAll(resp.Body); err == nil {
if body, err := io.ReadAll(resp.Body); err == nil {
newClientErr.respBody = string(body)
}

Expand Down
4 changes: 3 additions & 1 deletion errors/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package errors

import "fmt"
import (
"fmt"
)

var (
// NewExistsSVCErrGen is a generator function for building ExistsSVCErrs with the specified
Expand Down
47 changes: 32 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,57 @@ module github.com/graymeta/gmkit
replace gopkg.in/russross/blackfriday.v2 => github.com/russross/blackfriday/v2 v2.0.1

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.20.0+incompatible // indirect
github.com/aws/aws-sdk-go v1.23.4
github.com/carlosdp/twiliogo v0.0.0-20161027183705-b26045ebb9d1
github.com/ernesto-jimenez/httplogger v0.0.0-20150224132909-86cc44f6150a
github.com/garyburd/redigo v1.6.0
github.com/go-kit/kit v0.9.0
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-stack/stack v1.8.0
github.com/google/uuid v1.1.1 // indirect
github.com/gorilla/mux v1.7.3
github.com/graymeta/env v0.0.2
github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69
github.com/hashicorp/go-multierror v1.0.0
github.com/huandu/xstrings v1.2.0 // indirect
github.com/hyperboloide/lk v0.0.0-20190531110207-c022f7a15f5a
github.com/imdario/mergo v0.3.7 // indirect
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 // indirect
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.2.0
github.com/matcornic/hermes v1.2.0
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1 // indirect
github.com/pkg/errors v0.8.1
github.com/quipo/statsd v0.0.0-20180118161217-3d6a5565f314
github.com/reiver/go-pqerror v0.0.0-20160209202356-63f13fe5516a
github.com/sendgrid/rest v2.4.1+incompatible // indirect
github.com/sendgrid/sendgrid-go v3.5.0+incompatible
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
gopkg.in/olivere/elastic.v5 v5.0.81
)

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.20.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/uniuri v1.2.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect
github.com/mailru/easyjson v0.0.0-20180730094502-03f2033d19d5 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.27.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sendgrid/rest v2.4.1+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
gopkg.in/olivere/elastic.v5 v5.0.81
github.com/stretchr/objx v0.1.0 // indirect
golang.org/x/net v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/russross/blackfriday.v2 v2.0.0-00010101000000-000000000000 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

go 1.13
go 1.20
Loading