Skip to content

Commit 4de244c

Browse files
committed
Fixed invalid json value for error in logger middleware
Signed-off-by: Vishal Rana <[email protected]>
1 parent 8c13b9d commit 4de244c

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

echo.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import (
6464
type (
6565
// Echo is the top-level framework instance.
6666
Echo struct {
67+
common
6768
StdLogger *stdLog.Logger
6869
colorer *color.Color
6970
premiddleware []MiddlewareFunc
@@ -125,10 +126,8 @@ type (
125126
// Map defines a generic map of type `map[string]interface{}`.
126127
Map map[string]interface{}
127128

128-
// i is the interface for Echo and Group.
129-
i interface {
130-
GET(string, HandlerFunc, ...MiddlewareFunc) *Route
131-
}
129+
// Common struct for Echo & Group.
130+
common struct{}
132131
)
133132

134133
// HTTP methods
@@ -225,7 +224,7 @@ const (
225224

226225
const (
227226
// Version of Echo
228-
Version = "4.1.2"
227+
Version = "4.1.3"
229228
website = "https://echo.labstack.com"
230229
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
231230
banner = `
@@ -461,10 +460,10 @@ func (e *Echo) Static(prefix, root string) *Route {
461460
if root == "" {
462461
root = "." // For security we want to restrict to CWD.
463462
}
464-
return static(e, prefix, root)
463+
return e.static(prefix, root, e.GET)
465464
}
466465

467-
func static(i i, prefix, root string) *Route {
466+
func (_ common) static(prefix, root string, get func(string, HandlerFunc, ...MiddlewareFunc) *Route) *Route {
468467
h := func(c Context) error {
469468
p, err := url.PathUnescape(c.Param("*"))
470469
if err != nil {
@@ -473,21 +472,26 @@ func static(i i, prefix, root string) *Route {
473472
name := filepath.Join(root, path.Clean("/"+p)) // "/"+ for security
474473
return c.File(name)
475474
}
476-
i.GET(prefix, h)
475+
get(prefix, h)
477476
if prefix == "/" {
478-
return i.GET(prefix+"*", h)
477+
return get(prefix+"*", h)
479478
}
480479

481-
return i.GET(prefix+"/*", h)
480+
return get(prefix+"/*", h)
482481
}
483482

484-
// File registers a new route with path to serve a static file with optional route-level middleware.
485-
func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {
486-
return e.GET(path, func(c Context) error {
483+
func (_ common) file(path, file string, get func(string, HandlerFunc, ...MiddlewareFunc) *Route,
484+
m ...MiddlewareFunc) *Route {
485+
return get(path, func(c Context) error {
487486
return c.File(file)
488487
}, m...)
489488
}
490489

490+
// File registers a new route with path to serve a static file with optional route-level middleware.
491+
func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {
492+
return e.file(path, file, e.GET, m...)
493+
}
494+
491495
func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
492496
name := handlerName(handler)
493497
router := e.findRouter(host)

group.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type (
1010
// routes that share a common middleware or functionality that should be separate
1111
// from the parent echo instance while still inheriting from it.
1212
Group struct {
13+
common
1314
host string
1415
prefix string
1516
middleware []MiddlewareFunc
@@ -23,7 +24,7 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
2324
// Allow all requests to reach the group as they might get dropped if router
2425
// doesn't find a match, making none of the group middleware process.
2526
for _, p := range []string{"", "/*"} {
26-
g.echo.Any(path.Clean(g.prefix+p), func(c Context) error {
27+
g.Any(path.Clean(g.prefix+p), func(c Context) error {
2728
return NotFoundHandler(c)
2829
}, g.middleware...)
2930
}
@@ -104,12 +105,12 @@ func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) (sg *Group) {
104105

105106
// Static implements `Echo#Static()` for sub-routes within the Group.
106107
func (g *Group) Static(prefix, root string) {
107-
static(g, prefix, root)
108+
g.static(prefix, root, g.GET)
108109
}
109110

110111
// File implements `Echo#File()` for sub-routes within the Group.
111112
func (g *Group) File(path, file string) {
112-
g.echo.File(g.prefix+path, file)
113+
g.file(g.prefix+path, file, g.GET)
113114
}
114115

115116
// Add implements `Echo#Add()` for sub-routes within the Group.

middleware/logger.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"os"
78
"strconv"
@@ -175,7 +176,10 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
175176
return buf.WriteString(s)
176177
case "error":
177178
if err != nil {
178-
return buf.WriteString(err.Error())
179+
// Error may contain invalid JSON e.g. `"`
180+
b, _ := json.Marshal(err.Error())
181+
b = b[1 : len(b)-1]
182+
return buf.Write(b)
179183
}
180184
case "latency":
181185
l := stop.Sub(start)

0 commit comments

Comments
 (0)