Skip to content

Commit

Permalink
move errors to respective packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Winnicki committed Feb 12, 2018
1 parent 80c6319 commit 8945d40
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 115 deletions.
43 changes: 36 additions & 7 deletions function/errors.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
package function

import "fmt"
import (
"fmt"
)

// ErrFunctionNotFound occurs when function couldn't been found in the discovery.
type ErrFunctionNotFound struct {
ID ID
}

func (e ErrFunctionNotFound) Error() string {
return fmt.Sprintf("Function %q not found.", string(e.ID))
}

// ErrFunctionAlreadyRegistered occurs when function with specified name is already registered.
type ErrFunctionAlreadyRegistered struct {
ID ID
}

func (e ErrFunctionAlreadyRegistered) Error() string {
return fmt.Sprintf("Function %q already registered.", string(e.ID))
}

// ErrFunctionValidation occurs when function payload doesn't validate.
type ErrFunctionValidation struct {
Message string
}

func (e ErrFunctionValidation) Error() string {
return fmt.Sprintf("Function doesn't validate. Validation error: %q", e.Message)
}

// ErrFunctionCallFailed occurs when function call failed because of provider error.
type ErrFunctionCallFailed struct {
original error
Original error
}

func (e ErrFunctionCallFailed) Error() string {
return fmt.Sprintf("Function call failed. Error: %q", e.original)
return fmt.Sprintf("Function call failed. Error: %q", e.Original)
}

// ErrFunctionProviderError occurs when function call failed because of provider error.
type ErrFunctionProviderError struct {
original error
Original error
}

func (e ErrFunctionProviderError) Error() string {
return fmt.Sprintf("Function call failed because of provider error. Error: %q", e.original)
return fmt.Sprintf("Function call failed because of provider error. Error: %q", e.Original)
}

// ErrFunctionError occurs when function call failed because of function error.
type ErrFunctionError struct {
original error
Original error
}

func (e ErrFunctionError) Error() string {
return fmt.Sprintf("Function call failed because of runtime error. Error: %q", e.original)
return fmt.Sprintf("Function call failed because of runtime error. Error: %q", e.Original)
}
23 changes: 11 additions & 12 deletions httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/libkv"
"github.com/serverless/event-gateway/subscription"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func (h HTTPAPI) getFunction(w http.ResponseWriter, r *http.Request, params http

fn, err := h.Functions.GetFunction(function.ID(params.ByName("id")))
if err != nil {
if _, ok := err.(*libkv.ErrNotFound); ok {
if _, ok := err.(*function.ErrFunctionNotFound); ok {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -84,9 +83,9 @@ func (h HTTPAPI) registerFunction(w http.ResponseWriter, r *http.Request, params

output, err := h.Functions.RegisterFunction(fn)
if err != nil {
if _, ok := err.(*libkv.ErrValidation); ok {
if _, ok := err.(*function.ErrFunctionValidation); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*libkv.ErrAlreadyRegistered); ok {
} else if _, ok := err.(*function.ErrFunctionAlreadyRegistered); ok {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -115,9 +114,9 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h
fn.ID = function.ID(params.ByName("id"))
output, err := h.Functions.UpdateFunction(fn)
if err != nil {
if _, ok := err.(*libkv.ErrValidation); ok {
if _, ok := err.(*function.ErrFunctionValidation); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*libkv.ErrNotFound); ok {
} else if _, ok := err.(*function.ErrFunctionNotFound); ok {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -136,7 +135,7 @@ func (h HTTPAPI) deleteFunction(w http.ResponseWriter, r *http.Request, params h

err := h.Functions.DeleteFunction(function.ID(params.ByName("id")))
if err != nil {
if _, ok := err.(*libkv.ErrNotFound); ok {
if _, ok := err.(*function.ErrFunctionNotFound); ok {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -163,13 +162,13 @@ func (h HTTPAPI) createSubscription(w http.ResponseWriter, r *http.Request, para

output, err := h.Subscriptions.CreateSubscription(s)
if err != nil {
if _, ok := err.(*libkv.ErrSubscriptionAlreadyExists); ok {
if _, ok := err.(*subscription.ErrSubscriptionAlreadyExists); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*libkv.ErrFunctionNotFound); ok {
} else if _, ok := err.(*function.ErrFunctionNotFound); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*libkv.ErrSubscriptionValidation); ok {
} else if _, ok := err.(*subscription.ErrSubscriptionValidation); ok {
w.WriteHeader(http.StatusBadRequest)
} else if _, ok := err.(*libkv.ErrPathConfict); ok {
} else if _, ok := err.(*subscription.ErrPathConfict); ok {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -192,7 +191,7 @@ func (h HTTPAPI) deleteSubscription(w http.ResponseWriter, r *http.Request, para

err := h.Subscriptions.DeleteSubscription(subscription.ID(sid))
if err != nil {
if _, ok := err.(*libkv.ErrSubscriptionNotFound); ok {
if _, ok := err.(*subscription.ErrSubscriptionNotFound); ok {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
22 changes: 11 additions & 11 deletions libkv/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (f Service) RegisterFunction(fn *function.Function) (*function.Function, er

_, err := f.FunctionStore.Get(string(fn.ID), &store.ReadOptions{Consistent: true})
if err == nil {
return nil, &ErrAlreadyRegistered{fn.ID}
return nil, &function.ErrFunctionAlreadyRegistered{fn.ID}
}

byt, err := json.Marshal(fn)
Expand All @@ -43,7 +43,7 @@ func (f Service) RegisterFunction(fn *function.Function) (*function.Function, er
func (f Service) UpdateFunction(fn *function.Function) (*function.Function, error) {
_, err := f.FunctionStore.Get(string(fn.ID), &store.ReadOptions{Consistent: true})
if err != nil {
return nil, &ErrNotFound{fn.ID}
return nil, &function.ErrFunctionNotFound{fn.ID}
}

if err = f.validateFunction(fn); err != nil {
Expand All @@ -69,7 +69,7 @@ func (f Service) UpdateFunction(fn *function.Function) (*function.Function, erro
func (f Service) GetFunction(id function.ID) (*function.Function, error) {
kv, err := f.FunctionStore.Get(string(id), &store.ReadOptions{Consistent: true})
if err != nil {
return nil, &ErrNotFound{id}
return nil, &function.ErrFunctionNotFound{id}
}

fn := function.Function{}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (f Service) GetAllFunctions() ([]*function.Function, error) {
func (f Service) DeleteFunction(id function.ID) error {
err := f.FunctionStore.Delete(string(id))
if err != nil {
return &ErrNotFound{id}
return &function.ErrFunctionNotFound{id}
}

f.Log.Debug("Function deleted.", zap.String("functionId", string(id)))
Expand All @@ -121,12 +121,12 @@ func (f Service) validateFunction(fn *function.Function) error {
validate.RegisterValidation("functionid", functionIDValidator)
err := validate.Struct(fn)
if err != nil {
return &ErrValidation{err.Error()}
return &function.ErrFunctionValidation{err.Error()}
}

if fn.Provider.Type == function.AWSLambda {
if fn.Provider.ARN == "" || fn.Provider.Region == "" {
return &ErrValidation{"Missing required fields for AWS Lambda function."}
return &function.ErrFunctionValidation{"Missing required fields for AWS Lambda function."}
}
}

Expand All @@ -135,7 +135,7 @@ func (f Service) validateFunction(fn *function.Function) error {
}

if fn.Provider.Type == function.HTTPEndpoint && fn.Provider.URL == "" {
return &ErrValidation{"Missing required fields for HTTP endpoint."}
return &function.ErrFunctionValidation{"Missing required fields for HTTP endpoint."}
}

if fn.Provider.Type == function.Weighted {
Expand All @@ -147,16 +147,16 @@ func (f Service) validateFunction(fn *function.Function) error {

func (f Service) validateEmulator(fn *function.Function) error {
if fn.Provider.EmulatorURL == "" {
return &ErrValidation{"Missing required field emulatorURL for Emulator function."}
return &function.ErrFunctionValidation{"Missing required field emulatorURL for Emulator function."}
} else if fn.Provider.APIVersion == "" {
return &ErrValidation{"Missing required field apiVersion for Emulator function."}
return &function.ErrFunctionValidation{"Missing required field apiVersion for Emulator function."}
}
return nil
}

func (f Service) validateWeighted(fn *function.Function) error {
if len(fn.Provider.Weighted) == 0 {
return &ErrValidation{"Missing required fields for weighted function."}
return &function.ErrFunctionValidation{"Missing required fields for weighted function."}
}

weightTotal := uint(0)
Expand All @@ -165,7 +165,7 @@ func (f Service) validateWeighted(fn *function.Function) error {
}

if weightTotal < 1 {
return &ErrValidation{"Function weights sum to zero."}
return &function.ErrFunctionValidation{"Function weights sum to zero."}
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions libkv/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRegisterFunction_ValidationError(t *testing.T) {

_, err := service.RegisterFunction(&function.Function{ID: "testid", Provider: &function.Provider{Type: function.HTTPEndpoint}})

assert.Equal(t, err, &ErrValidation{"Missing required fields for HTTP endpoint."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required fields for HTTP endpoint."})
}

func TestRegisterFunction_AlreadyExistsError(t *testing.T) {
Expand All @@ -48,7 +48,7 @@ func TestRegisterFunction_AlreadyExistsError(t *testing.T) {

_, err := service.RegisterFunction(&function.Function{ID: "testid", Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example.com"}})

assert.Equal(t, err, &ErrAlreadyRegistered{ID: "testid"})
assert.Equal(t, err, &function.ErrFunctionAlreadyRegistered{ID: "testid"})
}

func TestRegisterFunction_PutError(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestUpdateFunction_ValidationError(t *testing.T) {

_, err := service.UpdateFunction(&function.Function{ID: "testid", Provider: &function.Provider{Type: function.HTTPEndpoint}})

assert.Equal(t, err, &ErrValidation{"Missing required fields for HTTP endpoint."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required fields for HTTP endpoint."})
}

func TestUpdateFunction_NotFoundError(t *testing.T) {
Expand All @@ -102,7 +102,7 @@ func TestUpdateFunction_NotFoundError(t *testing.T) {

_, err := service.UpdateFunction(&function.Function{ID: "testid", Provider: &function.Provider{Type: function.HTTPEndpoint, URL: "http://example.com"}})

assert.Equal(t, err, &ErrNotFound{ID: "testid"})
assert.Equal(t, err, &function.ErrFunctionNotFound{ID: "testid"})
}

func TestUpdateFunction_PutError(t *testing.T) {
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestGetFunction_NotFound(t *testing.T) {

_, err := service.GetFunction(function.ID("testid"))

assert.Equal(t, err, &ErrNotFound{"testid"})
assert.Equal(t, err, &function.ErrFunctionNotFound{"testid"})
}

func TestGetAllFunctions(t *testing.T) {
Expand Down Expand Up @@ -206,45 +206,45 @@ func TestValidateFunction_AWSLambdaMissingRegion(t *testing.T) {

err := service.validateFunction(&function.Function{ID: "id", Provider: &function.Provider{Type: function.AWSLambda, ARN: "arn::"}})

assert.Equal(t, err, &ErrValidation{"Missing required fields for AWS Lambda function."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required fields for AWS Lambda function."})
}

func TestValidateFunction_AWSLambdaMissingARN(t *testing.T) {
service := &Service{Log: zap.NewNop()}

err := service.validateFunction(&function.Function{ID: "id", Provider: &function.Provider{Type: function.AWSLambda, Region: "us-east-1"}})

assert.Equal(t, err, &ErrValidation{"Missing required fields for AWS Lambda function."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required fields for AWS Lambda function."})
}

func TestValidateFunction_HTTPMissingURL(t *testing.T) {
service := &Service{Log: zap.NewNop()}

err := service.validateFunction(&function.Function{ID: "id", Provider: &function.Provider{Type: function.HTTPEndpoint}})

assert.Equal(t, err, &ErrValidation{"Missing required fields for HTTP endpoint."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required fields for HTTP endpoint."})
}

func TestValidateFunction_MissingID(t *testing.T) {
service := &Service{Log: zap.NewNop()}

err := service.validateFunction(&function.Function{Provider: &function.Provider{Type: function.HTTPEndpoint}})

assert.Equal(t, err, &ErrValidation{"Key: 'Function.ID' Error:Field validation for 'ID' failed on the 'required' tag"})
assert.Equal(t, err, &function.ErrFunctionValidation{"Key: 'Function.ID' Error:Field validation for 'ID' failed on the 'required' tag"})
}

func TestValidateFunction_EmulatorMissingURL(t *testing.T) {
service := &Service{Log: zap.NewNop()}

err := service.validateFunction(&function.Function{ID: "id", Provider: &function.Provider{Type: function.Emulator}})

assert.Equal(t, err, &ErrValidation{"Missing required field emulatorURL for Emulator function."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required field emulatorURL for Emulator function."})
}

func TestValidateFunction_EmulatorMissingAPIVersion(t *testing.T) {
service := &Service{Log: zap.NewNop()}

err := service.validateFunction(&function.Function{ID: "id", Provider: &function.Provider{Type: function.Emulator, EmulatorURL: "http://example.com"}})

assert.Equal(t, err, &ErrValidation{"Missing required field apiVersion for Emulator function."})
assert.Equal(t, err, &function.ErrFunctionValidation{"Missing required field apiVersion for Emulator function."})
}
34 changes: 0 additions & 34 deletions libkv/functions_errors.go

This file was deleted.

Loading

0 comments on commit 8945d40

Please sign in to comment.