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

Move error strings into codecs #6

Merged
merged 2 commits into from
Oct 3, 2023
Merged
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
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ SHELL := bash
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-print-directory
GO ?= go
BIN := .tmp/bin
export PATH := $(BIN):$(PATH)
export GOBIN := $(abspath $(BIN))

.PHONY: help
help: ## Describe useful make targets
Expand All @@ -20,21 +21,21 @@ all: ## Build, test, and lint (default)

.PHONY: test
test: build ## Run unit tests
$(GO) test -vet=off -race -cover ./...
go test -vet=off -race -cover ./...

.PHONY: build
build: ## Build all packages
$(GO) build ./...
go build ./...

.PHONY: lint
lint: $(BIN)/gofmt $(BIN)/staticcheck ## Lint Go
test -z "$$($(BIN)/gofmt -s -l . | tee /dev/stderr)"
$(GO) vet ./...
$(BIN)/staticcheck ./...
test -z "$$(gofmt -s -l . | tee /dev/stderr)"
go vet ./...
staticcheck ./...

.PHONY: lintfix
lintfix: $(BIN)/gofmt ## Automatically fix some lint errors
$(BIN)/gofmt -s -w .
gofmt -s -w .

.PHONY: upgrade
upgrade: ## Upgrade dependencies
Expand All @@ -46,8 +47,8 @@ clean: ## Remove intermediate artifacts

$(BIN)/gofmt:
@mkdir -p $(@D)
$(GO) build -o $(@) cmd/gofmt
go build -o $(@) cmd/gofmt

$(BIN)/staticcheck:
@mkdir -p $(@D)
GOBIN=$(abspath $(@D)) $(GO) install honnef.co/go/tools/cmd/staticcheck@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
12 changes: 10 additions & 2 deletions binary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connectproto

import (
"fmt"

"connectrpc.com/connect"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -49,7 +51,10 @@ func (b *binaryCodec) Unmarshal(binary []byte, msg any) error {
if !ok {
return errNotProto(msg)
}
return b.unmarshal.Unmarshal(binary, pm)
if err := b.unmarshal.Unmarshal(binary, pm); err != nil {
return fmt.Errorf("unmarshal into %T: %w", pm, err)
}
return nil
}

func (b *binaryCodec) Marshal(msg any) ([]byte, error) {
Expand Down Expand Up @@ -85,7 +90,10 @@ func newBinaryVTCodec() *vtBinaryCodec {

func (v *vtBinaryCodec) Unmarshal(binary []byte, msg any) error {
if vt, ok := msg.(interface{ UnmarshalVT([]byte) error }); ok {
return vt.UnmarshalVT(binary)
if err := vt.UnmarshalVT(binary); err != nil {
return fmt.Errorf("unmarshal into %T: %w", msg, err)
}
return nil
}
return v.binaryCodec.Unmarshal(binary, msg)
}
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func errNotProto(msg any) error {
if _, ok := msg.(protoiface.MessageV1); ok {
return fmt.Errorf("%T uses github.com/golang/protobuf, but connect-go only supports google.golang.org/protobuf: see https://go.dev/blog/protobuf-apiv2", msg)
return fmt.Errorf("%T uses github.com/golang/protobuf, but connectproto only supports google.golang.org/protobuf: see https://go.dev/blog/protobuf-apiv2", msg)
}
return fmt.Errorf("%T doesn't implement proto.Message", msg)
}
6 changes: 5 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"

"connectrpc.com/connect"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -36,7 +37,10 @@ func (j *jsonCodec) Unmarshal(binary []byte, msg any) error {
if len(binary) == 0 {
return errors.New("zero-length payload is not a valid JSON object")
}
return j.unmarshal.Unmarshal(binary, pm)
if err := j.unmarshal.Unmarshal(binary, pm); err != nil {
return fmt.Errorf("unmarshal into %T: %w", pm, err)
}
return nil
}

func (j *jsonCodec) Marshal(msg any) ([]byte, error) {
Expand Down