Skip to content

Commit

Permalink
Merge 2d10d87 into 60d9ac8
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoniam committed Sep 5, 2019
2 parents 60d9ac8 + 2d10d87 commit 18ddc25
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 111 deletions.
99 changes: 99 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package errorhandler

import (
"fmt"
"net/http"
)

const (
MsgErrMissingParam = "missingParameter"

MsgErrInvalidQueryParam = "invalidQueryParameter"
MsgErrInvalidPathParam = "invalidPathParameter"
MsgErrInvalidParam = "invalidParameter"

MsgErrOpNotPermitted = "operationNotPermitted"
AuthHeader = "authorizationHeader"
BasicToken = "basicToken"
BearerToken = "bearerToken"
Token = "token"
Level = "level"
)

var emitter string

// SetEmitter is called by the components that call the methods of the common service
func SetEmitter(e string) {
emitter = e
}

// GetEmitter allows to see who called the common service
func GetEmitter() string {
return emitter
}

// Error can be returned by the API endpoints
type Error struct {
Status int
Message string
}

func (e Error) Error() string {
return fmt.Sprintf("%d %s", e.Status, e.Message)
}

// CreateInternalServerError creates an error relative to an internal server error
func CreateInternalServerError(message string) Error {
return Error{
Status: http.StatusInternalServerError,
Message: GetEmitter() + "." + message,
}
}

// CreateMissingParameterError creates an error relative to a missing mandatory parameter
func CreateMissingParameterError(name string) Error {
return Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrMissingParam, name),
}
}

// CreateInvalidQueryParameterError creates an error relative to a invalid query parameter
func CreateInvalidQueryParameterError(paramName string) Error {
return Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrInvalidQueryParam, paramName),
}
}

// CreateInvalidPathParameterError creates an error relative to a invalid path parameter
func CreateInvalidPathParameterError(paramName string) Error {
return Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrInvalidPathParam, paramName),
}
}

// CreateBadRequestError creates an error relative to a bad request
func CreateBadRequestError(publicMessage string) Error {
return Error{
Status: http.StatusBadRequest,
Message: GetEmitter() + "." + publicMessage,
}
}

// CreateNotAllowedError creates an error relative to a not allowed request
func CreateNotAllowedError(publicMessage string) Error {
return Error{
Status: http.StatusMethodNotAllowed,
Message: GetEmitter() + "." + publicMessage,
}
}

// CreateNotFoundError creates an error relative to a not found request
func CreateNotFoundError(publicMessage string) Error {
return Error{
Status: http.StatusNotFound,
Message: GetEmitter() + "." + publicMessage,
}
}
37 changes: 37 additions & 0 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package errorhandler

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHTTPResponse(t *testing.T) {
// Coverage
var error = CreateMissingParameterError("parameter")
assert.Contains(t, error.Error(), error.Message)

error = CreateBadRequestError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateInternalServerError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateInvalidQueryParameterError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateInvalidPathParameterError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateNotAllowedError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateNotFoundError("message")
assert.Contains(t, error.Error(), error.Message)
}

func TestEmitter(t *testing.T) {
var emitter = "component"
SetEmitter(emitter)
assert.Equal(t, GetEmitter(), emitter)
}
56 changes: 0 additions & 56 deletions http/errors.go

This file was deleted.

19 changes: 0 additions & 19 deletions http/errors_test.go

This file was deleted.

8 changes: 5 additions & 3 deletions http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"regexp"

errorhandler "github.com/cloudtrust/common-service/errors"
"github.com/cloudtrust/common-service/log"
"github.com/cloudtrust/common-service/security"
"github.com/go-kit/kit/ratelimit"
Expand Down Expand Up @@ -80,7 +81,7 @@ func DecodeRequest(_ context.Context, req *http.Request, pathParams map[string]s
for key, validationRegExp := range pathParams {
if v, ok := m[key]; ok {
if matched, _ := regexp.Match(validationRegExp, []byte(v)); !matched {
return nil, CreateInvalidQueryParameterError(key)
return nil, errorhandler.CreateInvalidQueryParameterError(key)
}
request[key] = m[key]
}
Expand All @@ -98,7 +99,7 @@ func DecodeRequest(_ context.Context, req *http.Request, pathParams map[string]s
for key, validationRegExp := range queryParams {
if value := req.URL.Query().Get(key); value != "" {
if matched, _ := regexp.Match(validationRegExp, []byte(value)); !matched {
return nil, CreateInvalidPathParameterError(key)
return nil, errorhandler.CreateInvalidPathParameterError(key)
}

request[key] = value
Expand Down Expand Up @@ -150,7 +151,8 @@ func ErrorHandler(logger log.Logger) func(context.Context, error, http.ResponseW
switch e := errors.Cause(err).(type) {
case security.ForbiddenError:
w.WriteHeader(http.StatusForbidden)
case Error:
w.Write([]byte(errorhandler.GetEmitter() + "." + errorhandler.MsgErrOpNotPermitted))
case errorhandler.Error:
w.WriteHeader(e.Status)
// You should really take care of what you are sending here : e.Message should not leak any sensitive information
w.Write([]byte(e.Message))
Expand Down
7 changes: 4 additions & 3 deletions http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"net/url"
"testing"

"github.com/cloudtrust/common-service/security"

errorhandler "github.com/cloudtrust/common-service/errors"
"github.com/cloudtrust/common-service/http/mock"
"github.com/cloudtrust/common-service/security"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/ratelimit"
http_transport "github.com/go-kit/kit/transport/http"
Expand Down Expand Up @@ -310,12 +310,13 @@ func TestErrorHandler(t *testing.T) {
// ForbiddenError
{
mockRespWriter.EXPECT().WriteHeader(http.StatusForbidden).Times(1)
mockRespWriter.EXPECT().Write(gomock.Any()).Times(1)
ErrorHandlerNoLog()(context.Background(), security.ForbiddenError{}, mockRespWriter)
}

// HTTPError
{
err := Error{
err := errorhandler.Error{
Status: 123,
Message: "abc",
}
Expand Down
5 changes: 3 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package log

import (
"fmt"
"errors"

errorhandler "github.com/cloudtrust/common-service/errors"
kit_log "github.com/go-kit/kit/log"
kit_level "github.com/go-kit/kit/log/level"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func ConvertToLevel(strLevel string) (kit_level.Option, error) {
var level, ok = levels[strLevel]

if !ok {
return nil, fmt.Errorf("Invalid level")
return nil, errors.New(errorhandler.MsgErrInvalidParam + errorhandler.Level)
}

return level, nil
Expand Down
Loading

0 comments on commit 18ddc25

Please sign in to comment.