~samwhited/problem

f3e12ef3029e43fa9f06a6fe65ee7558c3939149 — Sam Whited 1 year, 5 months ago f912928
Return errors instead of logging them

Now that this package is public, let the user handle encoding errors
instead of logging them.
4 files changed, 10 insertions(+), 17 deletions(-)

M go.mod
D go.sum
M problem.go
M problem_test.go
M go.mod => go.mod +0 -2
@@ 1,5 1,3 @@
module code.soquee.net/problem

go 1.11

require code.soquee.net/testlog v0.0.1

D go.sum => go.sum +0 -2
@@ 1,2 0,0 @@
code.soquee.net/testlog v0.0.1 h1:oWNjgsx51u6aqAszu3U2wLQykZFJ9vkYI1/Vwu1x8bc=
code.soquee.net/testlog v0.0.1/go.mod h1:yfJIdROWb6p+nkczs313619wx6f0JsNsdh/tgy2kWZQ=

M problem.go => problem.go +6 -5
@@ 3,7 3,7 @@ package problem // import "code.soquee.net/problem"

import (
	"encoding/json"
	"log"
	"fmt"
	"net/http"
)



@@ 55,8 55,8 @@ func (p Problem) Error() string {
// If the HTTPStatus method returns 0, no status code is written and the user
// must write one elsewhere.
// If the error value is nil, a 200 is returned.
func NewResponder(logger *log.Logger) func(http.ResponseWriter, *http.Request, error) {
	return func(w http.ResponseWriter, req *http.Request, err error) {
func NewResponder() func(http.ResponseWriter, *http.Request, error) error {
	return func(w http.ResponseWriter, req *http.Request, err error) error {
		if req.Method != "HEAD" {
			w.Header().Set("Content-Type", "application/json")
		}


@@ 72,7 72,7 @@ func NewResponder(logger *log.Logger) func(http.ResponseWriter, *http.Request, e
			w.WriteHeader(status.HTTPStatus())
		case err == nil:
			w.WriteHeader(200)
			return
			return nil
		default:
			w.WriteHeader(http.StatusInternalServerError)
		}


@@ 80,8 80,9 @@ func NewResponder(logger *log.Logger) func(http.ResponseWriter, *http.Request, e
		if req.Method != "HEAD" {
			err = json.NewEncoder(w).Encode(err)
			if err != nil {
				logger.Printf("Error encoding JSON error: %q\n", err)
				return fmt.Errorf("error encoding JSON error: %q", err)
			}
		}
		return nil
	}
}

M problem_test.go => problem_test.go +4 -8
@@ 1,9 1,7 @@
package problem_test

import (
	"bytes"
	"errors"
	"log"
	"net/http"
	"net/http/httptest"
	"strconv"


@@ 11,7 9,6 @@ import (
	"testing"

	"code.soquee.net/problem"
	"code.soquee.net/testlog"
)

var errResponderTestCases = [...]struct {


@@ 52,7 49,7 @@ func TestErrorResponder(t *testing.T) {
			if tc.writeCode != 0 {
				recorder.WriteHeader(tc.writeCode)
			}
			problem.NewResponder(testlog.New(t))(recorder, req, tc.err)
			problem.NewResponder()(recorder, req, tc.err)

			contentType := recorder.Header().Get("Content-Type")
			if tc.method == "HEAD" && contentType != "" {


@@ 75,13 72,12 @@ func (unmarshalableErrType) Error() string {
}

func TestErrorResponderBadEncode(t *testing.T) {
	b := new(bytes.Buffer)
	problem.NewResponder(log.New(b, "", 0))(
	err := problem.NewResponder()(
		httptest.NewRecorder(),
		httptest.NewRequest("GET", "/", nil),
		make(unmarshalableErrType))

	if b.Len() == 0 {
		t.Errorf("Expected error responder to log errors encountered during marshaling")
	if err == nil {
		t.Errorf("Expected error responder to return errors encountered during marshaling")
	}
}