-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add error middleware if there is a problem marshalling and writing json the error middleware handles mapping the context errors to a result error and returning json as a 500 * add service input define a method for exposing a request body struct and auto marshaling into the given struct. The struct is then stored on the req object and returned as a param to the config handler
- Loading branch information
1 parent
ca90a3a
commit 69249ab
Showing
17 changed files
with
388 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package requestreceiver | ||
|
||
import ( | ||
"github.com/simiancreative/simiango/server" | ||
"github.com/simiancreative/simiango/service" | ||
) | ||
|
||
var Config = service.Config{ | ||
Kind: service.DIRECT, | ||
Method: "POST", | ||
Path: "/request-receiver", | ||
Direct: direct, | ||
Input: input, | ||
} | ||
|
||
type Product struct { | ||
ID string `json:"id" db:"id"` | ||
Name string `json:"name" db:"name"` | ||
} | ||
|
||
func direct(req service.Req) (interface{}, error) { | ||
return req.Input, nil | ||
} | ||
|
||
func input() interface{} { | ||
return &Product{} | ||
} | ||
|
||
func init() { | ||
server.AddService(Config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package sentry_test | ||
|
||
import ( | ||
"errors" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
sentrygin "github.com/getsentry/sentry-go" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/simiancreative/simiango/monitoring/sentry" | ||
) | ||
|
||
func TestGinCaptureError(t *testing.T) { | ||
t.Run("sentryScopeFunc is not ok", func(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
c, _ := gin.CreateTestContext(httptest.NewRecorder()) | ||
|
||
hub := sentrygin.CurrentHub() | ||
c.Set("sentry", hub) | ||
|
||
err := errors.New("test error") | ||
returnedErr := sentry.GinCaptureError(c, err) | ||
|
||
assert.Equal(t, err, returnedErr) | ||
}) | ||
} | ||
|
||
func TestScopeFunctionError(t *testing.T) { | ||
t.Run("scopeFunction is not ok", func(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
c, _ := gin.CreateTestContext(httptest.NewRecorder()) | ||
|
||
hub := sentrygin.CurrentHub() | ||
c.Set("sentry", hub) | ||
|
||
// Set a non-function value for "sentryScopeFunc" | ||
c.Set("sentryScopeFunc", "not a function") | ||
|
||
err := errors.New("test error") | ||
returnedErr := sentry.GinCaptureError(c, err) | ||
|
||
assert.Equal(t, err, returnedErr) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/simiancreative/simiango/service" | ||
) | ||
|
||
func errorMiddleware(c *gin.Context) { | ||
c.Next() | ||
|
||
if len(c.Errors) == 0 { | ||
return | ||
} | ||
|
||
resp := service.ResultError{ | ||
Status: http.StatusInternalServerError, | ||
Message: "Internal Server Error", | ||
} | ||
|
||
for _, err := range c.Errors { | ||
resp.Reasons = append(resp.Reasons, map[string]interface{}{ | ||
"error": err.Error(), | ||
}) | ||
} | ||
|
||
c.JSON(http.StatusInternalServerError, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/simiancreative/simiango/service" | ||
) | ||
|
||
func TestErrorMiddleware(t *testing.T) { | ||
// Happy path | ||
t.Run("no errors", func(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
|
||
errorMiddleware(c) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
assert.Equal(t, "", w.Body.String()) | ||
}) | ||
|
||
// Error path | ||
t.Run("with errors", func(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
|
||
c.Error(gin.Error{ | ||
Err: &service.ResultError{}, | ||
Type: gin.ErrorTypePublic, | ||
}) | ||
|
||
errorMiddleware(c) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, w.Code) | ||
assert.Contains(t, w.Body.String(), "Internal Server Error") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.