diff --git a/README.md b/README.md index 48c49fb2d..0ce50b23b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # OpenAPI errors [![Build Status](https://travis-ci.org/go-openapi/errors.svg?branch=master)](https://travis-ci.org/go-openapi/errors) [![codecov](https://codecov.io/gh/go-openapi/errors/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/errors) [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io) -[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/errors/master/LICENSE) [![GoDoc](https://godoc.org/github.com/go-openapi/errors?status.svg)](http://godoc.org/github.com/go-openapi/errors) +[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/errors/master/LICENSE) +[![GoDoc](https://godoc.org/github.com/go-openapi/errors?status.svg)](http://godoc.org/github.com/go-openapi/errors) +[![GolangCI](https://golangci.com/badges/github.com/go-openapi/errors.svg)](https://golangci.com) +[![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/errors)](https://goreportcard.com/report/github.com/go-openapi/errors) -Shared errors used throughout the various libraries for the go-openapi toolkit \ No newline at end of file +Shared errors and error interface used throughout the various libraries found in the go-openapi toolkit. diff --git a/api.go b/api.go index edb3aa061..d1a752c42 100644 --- a/api.go +++ b/api.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" "net/http" + "reflect" "strings" ) @@ -136,7 +137,8 @@ func ServeError(rw http.ResponseWriter, r *http.Request, err error) { rw.Write(errorAsJSON(e)) } case Error: - if e == nil { + value := reflect.ValueOf(e) + if value.Kind() == reflect.Ptr && value.IsNil() { rw.WriteHeader(http.StatusInternalServerError) rw.Write(errorAsJSON(New(http.StatusInternalServerError, "Unknown error"))) return diff --git a/api_test.go b/api_test.go index 39bae8f43..1c66ba4a9 100644 --- a/api_test.go +++ b/api_test.go @@ -23,6 +23,10 @@ import ( "github.com/stretchr/testify/assert" ) +type customError struct { + apiError +} + func TestServeError(t *testing.T) { // method not allowed wins // err abides by the Error interface @@ -132,6 +136,12 @@ func TestServeError(t *testing.T) { ServeError(recorder, nil, nil) assert.Equal(t, http.StatusInternalServerError, recorder.Code) assert.Equal(t, `{"code":500,"message":"Unknown error"}`, recorder.Body.String()) + + recorder = httptest.NewRecorder() + var z *customError + ServeError(recorder, nil, z) + assert.Equal(t, http.StatusInternalServerError, recorder.Code) + assert.Equal(t, `{"code":500,"message":"Unknown error"}`, recorder.Body.String()) } func TestAPIErrors(t *testing.T) {