Skip to content

Commit 22be270

Browse files
authored
fix(api): guard access logger json marshal errors (#366)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent c763abe commit 22be270

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

api/api.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func WithPort(port uint) APIOption {
5050
}
5151
}
5252

53-
// Initialize singleton API instance
53+
// Initialize singleton API instance.
5454
var apiInstance = &APIv1{
5555
engine: ConfigureRouter(false),
5656
Host: "0.0.0.0",
@@ -70,6 +70,7 @@ func New(debug bool, options ...APIOption) *APIv1 {
7070
opt(apiInstance)
7171
}
7272
})
73+
7374
return apiInstance
7475
}
7576

@@ -115,7 +116,7 @@ func (a *APIv1) Start() error {
115116

116117
func (a *APIv1) AddRoute(method, path string, handler gin.HandlerFunc) {
117118
// Inner function to add routes to a given target
118-
//(either gin.Engine or gin.RouterGroup)
119+
// (either gin.Engine or gin.RouterGroup)
119120
addRouteToTarget := func(target gin.IRoutes) {
120121
switch method {
121122
case "GET":
@@ -150,6 +151,7 @@ func ConfigureRouter(debug bool) *gin.Engine {
150151
if !debug {
151152
gin.SetMode(gin.ReleaseMode)
152153
}
154+
153155
gin.DisableConsoleColor()
154156
g := gin.New()
155157
g.Use(gin.Recovery())
@@ -163,6 +165,7 @@ func ConfigureRouter(debug bool) *gin.Engine {
163165
})
164166
// Swagger UI
165167
g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
168+
166169
return g
167170
}
168171

@@ -179,7 +182,12 @@ func accessLogger(param gin.LogFormatterParams) string {
179182
"user_agent": param.Request.UserAgent(),
180183
"error_message": param.ErrorMessage,
181184
}
182-
ret, _ := json.Marshal(logEntry)
185+
186+
ret, err := json.Marshal(logEntry)
187+
if err != nil {
188+
return ""
189+
}
190+
183191
return string(ret) + "\n"
184192
}
185193

api/api_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
)
1212

1313
func TestRouteRegistration(t *testing.T) {
14-
1514
// Initialize the API and set it to debug mode for testing
1615
apiInstance := api.New(true)
1716

@@ -35,7 +34,6 @@ func TestRouteRegistration(t *testing.T) {
3534

3635
// Check the status code
3736
assert.Equal(t, http.StatusNotFound, rr.Code, "Expected status not found")
38-
3937
// You can also check the response body, headers, etc.
4038
// TODO check for JSON response (#338)
4139
// assert.Equal(t, `{"fcmToken":"someToken"}`, rr.Body.String())

0 commit comments

Comments
 (0)