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

Improve the acceptable error #15

Open
wants to merge 1 commit 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
16 changes: 7 additions & 9 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"net/http"
"net/url"
"reflect"
"regexp"

"github.com/gin-gonic/gin"
Expand All @@ -24,16 +23,15 @@ var (
// ErrRequestMissingAttrs error represents missing attributes error when create or update a resource
ErrRequestMissingAttrs = errors.New("couldn't bind resource")

// AcceptableError will be compared against the type of errors
// from `db.Create`. If the types match, Post will respond with
// AcceptableError will check the errors from `db.Create`.
// If the errors is acceptable, Post will respond with
// HTTPStatusUnprocessableEntity instead of panicking.
//
// This could be a lot smarter, but it was the simplest thing that
// supported my use-case at the time, without introducing more
// package dependencies.
AcceptableError reflect.Type
AcceptableError AcceptableErrorChecker = func(error) bool { return false }
)

// AcceptableErrorChecker checks whether the given error is acceptable.
type AcceptableErrorChecker func(err error) bool

var regexpID = regexp.MustCompile(`\d+`)

// DBModel defines an interface for ownership/authorisation when
Expand Down Expand Up @@ -166,7 +164,7 @@ func New(db *gorm.DB, single func() DBModel, collection func() interface{}, link
}

if err := db.Create(s).Error; err != nil {
if reflect.TypeOf(err) == AcceptableError {
if AcceptableError(err) {
ctx.JSON(HTTPStatusUnprocessableEntity, errToJSON(err))
} else {
panic(err)
Expand Down
5 changes: 2 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -220,7 +219,7 @@ func TestPostWithAcceptableError(t *testing.T) {

resourceError = errors.New("acceptable error")

resources.AcceptableError = reflect.TypeOf(resourceError)
resources.AcceptableError = func(err error) bool { return err == resourceError }
defer clearResourceErrors()

res := req(postBody(t, body))
Expand Down Expand Up @@ -416,6 +415,6 @@ func assertNoErr(err error) {
}

func clearResourceErrors() {
resources.AcceptableError = nil
resources.AcceptableError = func(error) bool { return false }
resourceError = nil
}