Skip to content

Commit

Permalink
Cleanup error messages and provide consistency in error reporting (#396)
Browse files Browse the repository at this point in the history
* Cleanup error messages and provide consistency in error reporting

* revert some changes in router

* remove unused field
  • Loading branch information
mthenw committed Mar 22, 2018
1 parent f0d923f commit bf66945
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 41 deletions.
12 changes: 6 additions & 6 deletions function/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ type ErrFunctionValidation struct {
}

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

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

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

// ErrFunctionAccessDenied occurs when Event Gateway don't have access to call a function.
Expand All @@ -46,7 +46,7 @@ type ErrFunctionAccessDenied struct {
}

func (e ErrFunctionAccessDenied) Error() string {
return fmt.Sprintf("Function access denied. Error: %q", e.Original)
return fmt.Sprintf("Function access denied. Error: %s", e.Original)
}

// ErrFunctionProviderError occurs when function call failed because of provider error.
Expand All @@ -55,7 +55,7 @@ type ErrFunctionProviderError struct {
}

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: %s", e.Original)
}

// ErrFunctionError occurs when function call failed because of function error.
Expand All @@ -64,7 +64,7 @@ type ErrFunctionError struct {
}

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: %s", e.Original)
}

// ErrFunctionHasSubscriptionsError occurs when function with subscription is being deleted.
Expand Down
11 changes: 0 additions & 11 deletions httpapi/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package httpapi

import "fmt"

// Response is a generic response object from Configuration and Events API.
type Response struct {
Errors []Error `json:"errors"`
Expand All @@ -11,12 +9,3 @@ type Response struct {
type Error struct {
Message string `json:"message"`
}

// ErrMalformedJSON occurring when it's impossible to decode JSON payload.
type ErrMalformedJSON struct {
Original error
}

func (e ErrMalformedJSON) Error() string {
return fmt.Sprintf("Malformed JSON payload: %q", e.Original)
}
9 changes: 6 additions & 3 deletions httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (h HTTPAPI) createFunction(w http.ResponseWriter, r *http.Request, params h
err := dec.Decode(fn)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
encoder.Encode(&Response{Errors: []Error{{Message: ErrMalformedJSON{err}.Error()}}})
validationErr := function.ErrFunctionValidation{Message: err.Error()}
encoder.Encode(&Response{Errors: []Error{{Message: validationErr.Error()}}})
return
}

Expand Down Expand Up @@ -125,7 +126,8 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h
err := dec.Decode(fn)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
encoder.Encode(&Response{Errors: []Error{{Message: ErrMalformedJSON{err}.Error()}}})
validationErr := function.ErrFunctionValidation{Message: err.Error()}
encoder.Encode(&Response{Errors: []Error{{Message: validationErr.Error()}}})
return
}

Expand Down Expand Up @@ -220,7 +222,8 @@ func (h HTTPAPI) createSubscription(w http.ResponseWriter, r *http.Request, para
err := dec.Decode(s)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
encoder.Encode(&Response{Errors: []Error{{Message: ErrMalformedJSON{err}.Error()}}})
validationErr := subscription.ErrSubscriptionValidation{Message: err.Error()}
encoder.Encode(&Response{Errors: []Error{{Message: validationErr.Error()}}})
return
}

Expand Down
7 changes: 4 additions & 3 deletions providers/awskinesis/awskinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package awskinesis

import (
"encoding/json"
"errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -83,12 +84,12 @@ func (p ProviderLoader) Load(data []byte) (function.Provider, error) {
provider := &AWSKinesis{}
err := json.Unmarshal(data, provider)
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Unable to load function provider config: " + err.Error()}
return nil, errors.New("unable to load function provider config: " + err.Error())
}

err = provider.validate()
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Kinesis function."}
return nil, errors.New("missing required fields for AWS Kinesis function")
}

config := aws.NewConfig().WithRegion(provider.Region)
Expand All @@ -98,7 +99,7 @@ func (p ProviderLoader) Load(data []byte) (function.Provider, error) {

awsSession, err := session.NewSession(config)
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Unable to create AWS Session: " + err.Error()}
return nil, errors.New("unable to create AWS Session: " + err.Error())
}

provider.Service = kinesis.New(awsSession)
Expand Down
7 changes: 3 additions & 4 deletions providers/awskinesis/awskinesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/golang/mock/gomock"
"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/providers/awskinesis"
"github.com/serverless/event-gateway/providers/awskinesis/mock"
"github.com/stretchr/testify/assert"
Expand All @@ -19,7 +18,7 @@ func TestLoad_MalformedJSON(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Unable to load function provider config: unexpected end of JSON input"})
assert.EqualError(t, err, "unable to load function provider config: unexpected end of JSON input")
}

func TestLoad_MissingStreamName(t *testing.T) {
Expand All @@ -29,7 +28,7 @@ func TestLoad_MissingStreamName(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Kinesis function."})
assert.EqualError(t, err, "missing required fields for AWS Kinesis function")
}

func TestLoad_MissingRegion(t *testing.T) {
Expand All @@ -39,7 +38,7 @@ func TestLoad_MissingRegion(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Kinesis function."})
assert.EqualError(t, err, "missing required fields for AWS Kinesis function")
}

func TestCall(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions providers/awslambda/awslambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func (p ProviderLoader) Load(data []byte) (function.Provider, error) {
provider := &AWSLambda{}
err := json.Unmarshal(data, provider)
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Unable to load function provider config: " + err.Error()}
return nil, errors.New("unable to load function provider config: " + err.Error())
}

err = provider.validate()
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Lambda function."}
return nil, errors.New("missing required fields for AWS Lambda function")
}

config := aws.NewConfig().WithRegion(provider.Region)
Expand All @@ -110,7 +110,7 @@ func (p ProviderLoader) Load(data []byte) (function.Provider, error) {

awsSession, err := session.NewSession(config)
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Unable to create AWS Session: " + err.Error()}
return nil, errors.New("unable to create AWS Session: " + err.Error())
}

provider.Service = lambda.New(awsSession)
Expand Down
7 changes: 3 additions & 4 deletions providers/awslambda/awslambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/golang/mock/gomock"
"github.com/serverless/event-gateway/function"
"github.com/serverless/event-gateway/providers/awslambda"
"github.com/serverless/event-gateway/providers/awslambda/mock"
"github.com/stretchr/testify/assert"
Expand All @@ -19,7 +18,7 @@ func TestLoad_MalformedJSON(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Unable to load function provider config: unexpected end of JSON input"})
assert.EqualError(t, err, "unable to load function provider config: unexpected end of JSON input")
}

func TestLoad_MissingARN(t *testing.T) {
Expand All @@ -29,7 +28,7 @@ func TestLoad_MissingARN(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Lambda function."})
assert.EqualError(t, err, "missing required fields for AWS Lambda function")
}

func TestLoad_MissingRegion(t *testing.T) {
Expand All @@ -39,7 +38,7 @@ func TestLoad_MissingRegion(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for AWS Lambda function."})
assert.EqualError(t, err, "missing required fields for AWS Lambda function")
}

func TestCall(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions providers/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -67,12 +68,12 @@ func (p ProviderLoader) Load(data []byte) (function.Provider, error) {
provider := &HTTP{}
err := json.Unmarshal(data, provider)
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Unable to load function provider config: " + err.Error()}
return nil, errors.New("unable to load function provider config: " + err.Error())
}

err = provider.validate()
if err != nil {
return nil, &function.ErrFunctionValidation{Message: "Missing required fields for HTTP endpoint."}
return nil, errors.New("missing required fields for HTTP endpoint")
}

return provider, nil
Expand Down
5 changes: 2 additions & 3 deletions providers/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"testing"

"github.com/serverless/event-gateway/function"
httpprovider "github.com/serverless/event-gateway/providers/http"
"github.com/stretchr/testify/assert"
)
Expand All @@ -19,7 +18,7 @@ func TestLoad(t *testing.T) {
provider, err := loader.Load(config)

assert.Nil(t, provider)
assert.Equal(t, err, &function.ErrFunctionValidation{Message: "Missing required fields for HTTP endpoint."})
assert.EqualError(t, err, "missing required fields for HTTP endpoint")
}

func TestCall(t *testing.T) {
Expand Down Expand Up @@ -48,5 +47,5 @@ func TestCall_InternalError(t *testing.T) {

_, err := provider.Call([]byte("hello"))

assert.EqualError(t, err, "Function call failed because of runtime error. Error: \"HTTP status code: 500\"")
assert.EqualError(t, err, "Function call failed because of runtime error. Error: HTTP status code: 500")
}
4 changes: 2 additions & 2 deletions subscription/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ErrSubscriptionValidation struct {
}

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

// ErrPathConfict occurs when HTTP subscription path conflicts with existing path.
Expand All @@ -37,5 +37,5 @@ type ErrPathConfict struct {
}

func (e ErrPathConfict) Error() string {
return fmt.Sprintf("Subscription path conflict: %s.", e.Message)
return fmt.Sprintf("Subscription path conflict: %s", e.Message)
}

0 comments on commit bf66945

Please sign in to comment.