Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility issues with the Echo framework #15

Closed
dxvgef opened this issue Jun 23, 2017 · 2 comments
Closed

Compatibility issues with the Echo framework #15

dxvgef opened this issue Jun 23, 2017 · 2 comments
Labels

Comments

@dxvgef
Copy link

dxvgef commented Jun 23, 2017

I use the scs/session package in Echo, and if I export anything to the browser in the HTTPErrorHandler function of Echo, the Echo of logger will print such warning messages:
Level:WARN, file:context.go, line:484, message:response, already, committed.
If I don't use session, there won't be such a problem.

package main

import (
	"time"

	"github.com/alexedwards/scs/engine/memstore"
	"github.com/alexedwards/scs/session"
	"github.com/labstack/echo"
)

func main() {
	app := echo.New()
	app.Debug = true
	sessionManager := session.Manage(memstore.New(12 * time.Hour))
	app.Use(echo.WrapMiddleware(sessionManager))
	app.HTTPErrorHandler = errorHandler
	app.Start(":80")
}

func errorHandler(err error, ctx echo.Context) {
	ctx.String(200, "test")
}
@dxvgef dxvgef changed the title response already committed Compatibility issues with the Echo framework Jun 23, 2017
@dxvgef
Copy link
Author

dxvgef commented Jul 8, 2017

I try to remove manager.go:74 The following code can solve this problem and work fine, do i do not know the hidden dangers?

if bw.code != 0 {
	w.WriteHeader(bw.code)
}

_, err = w.Write(bw.buf.Bytes())
if err != nil {
	m.opts.errorFunc(w, r, err)
}

@alexedwards
Copy link
Owner

Again, I'm not too familiar with Echo but I think this was probably being caused by the fact that both SCS and Echo's HTTPErrorHandler were trying to call the underlying http.ResponseWriter.WriteHeader method.

The new version SCS (> 1.0.0) no longer calls http.ResponseWriter.WriteHeader at all so I suspect this is solved.

The code below works for me and triggers a 500 status without any warnings:

package main

import (
	"errors"
	"fmt"

	"github.com/alexedwards/scs"
	"github.com/labstack/echo"
)

var sessionManager = scs.NewCookieManager("u46IpCV9y5Vlur8YvODJEhgOY8m9JVE4")

func main() {
	e := echo.New()
	e.Use(echo.WrapMiddleware(sessionManager.Use))
	e.HTTPErrorHandler = errorHandler

	e.GET("/", func(c echo.Context) error {
		c.Error(errors.New("test"))
		return nil
	})

	e.Start(":4000")
}

func errorHandler(err error, ctx echo.Context) {
	ctx.String(500, fmt.Sprintln(err))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants