Skip to content

Commit

Permalink
Merge pull request kubernetes#16 from fredbi/serve-nil-error
Browse files Browse the repository at this point in the history
Completed test for ServeError with nil Error, as discussed in kubernetes#13
  • Loading branch information
casualjim committed Sep 26, 2018
2 parents 87bb653 + 41df026 commit 1801eed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Shared errors and error interface used throughout the various libraries found in the go-openapi toolkit.
4 changes: 3 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
)

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1801eed

Please sign in to comment.