Skip to content

Commit

Permalink
Merge pull request #26 from themue/version/2.25.2
Browse files Browse the repository at this point in the history
Version 2.15.2
  • Loading branch information
Frank Mueller authored Sep 10, 2017
2 parents 2b5c10e + 9adcd92 commit 74af51f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Tideland Go REST Server Library

## 2017-09-10

- Fixed return code in case of not implemented HTTP methods
to *405* (method not allowed)

## 2017-06-19

- Reduced job handling complexity

## 2017-05-15

- Changed *Job.Path()* to return a *Path* instance
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ I hope you like it. ;)

## Version

Version 2.15.1
Version 2.15.2

## Packages

Expand Down
16 changes: 8 additions & 8 deletions rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func handleGetJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return rrh.Read(job)
}
return false, errors.New(ErrNoGetHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handleHeadJob handles a job containing a HEAD request.
Expand All @@ -154,7 +154,7 @@ func handleHeadJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return hrh.Head(job)
}
return false, errors.New(ErrNoHeadHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handlePutJob handles a job containing a PUT request.
Expand All @@ -167,7 +167,7 @@ func handlePutJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return urh.Update(job)
}
return false, errors.New(ErrNoPutHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handlePostJob handles a job containing a POST request.
Expand All @@ -180,7 +180,7 @@ func handlePostJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return crh.Create(job)
}
return false, errors.New(ErrNoPostHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handlePatchJob handles a job containing a PATCH request.
Expand All @@ -193,7 +193,7 @@ func handlePatchJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return mrh.Modify(job)
}
return false, errors.New(ErrNoPatchHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handleDeleteJob handles a job containing a DELETE request.
Expand All @@ -202,7 +202,7 @@ func handleDeleteJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return drh.Delete(job)
}
return false, errors.New(ErrNoDeleteHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// handleOptionsJob handles a job containing an OPTIONS request.
Expand All @@ -215,12 +215,12 @@ func handleOptionsJob(handler ResourceHandler, job Job) (bool, error) {
if ok {
return irh.Info(job)
}
return false, errors.New(ErrNoOptionsHandler, errorMessages, jobDescription(handler, job))
return false, errors.New(ErrMethodNotSupported, errorMessages, jobDescription(handler, job))
}

// jobDescription returns a description for possible errors.
func jobDescription(handler ResourceHandler, job Job) string {
return fmt.Sprintf("%s@%s/%s", handler.ID(), job.Domain(), job.Resource())
return fmt.Sprintf("%s %s@%s/%s", job.Request().Method, handler.ID(), job.Domain(), job.Resource())
}

// EOF
13 changes: 9 additions & 4 deletions rest/multiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http"
"sync"

"github.com/tideland/golib/errors"
"github.com/tideland/golib/etc"
"github.com/tideland/golib/logger"
"github.com/tideland/golib/monitoring"
Expand Down Expand Up @@ -132,15 +133,19 @@ func (mux *multiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
measuring := monitoring.BeginMeasuring(job.String())
defer measuring.EndMeasuring()
if err := mux.mapping.handle(job); err != nil {
mux.internalServerError("error handling request", job, err)
mux.handleError("error handling request", job, err)
}
}

// internalServerError logs an internal error and returns it to the user.
func (mux *multiplexer) internalServerError(format string, job Job, err error) {
// handleError logs an error and returns it to the user.
func (mux *multiplexer) handleError(format string, job Job, err error) {
code := http.StatusInternalServerError
msg := fmt.Sprintf(format+" %q: %v", job, err)
logger.Errorf(msg)
http.Error(job.ResponseWriter(), msg, http.StatusInternalServerError)
if errors.IsError(err, ErrMethodNotSupported) {
code = http.StatusMethodNotAllowed
}
http.Error(job.ResponseWriter(), msg, code)
}

// EOF
2 changes: 2 additions & 0 deletions rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package rest_test
import (
"context"
"fmt"
"net/http"
"testing"

"github.com/tideland/golib/audit"
Expand Down Expand Up @@ -279,6 +280,7 @@ func TestMethodNotSupported(t *testing.T) {
// Perform test requests.
req := restaudit.NewRequest("OPTIONS", "/base/test/no-options")
resp := ts.DoRequest(req)
resp.AssertStatusEquals(http.StatusMethodNotAllowed)
resp.AssertBodyContains("no-options")
}

Expand Down

0 comments on commit 74af51f

Please sign in to comment.