echo icon indicating copy to clipboard operation
echo copied to clipboard

Handler gets called Multiple Times

Open DawnKosmos opened this issue 2 years ago • 2 comments

Issue Description

Under specific conditions and wrong html header, a routes is executed multiple times

Checklist

  • [ Y] Dependencies installed
  • [ Y] No typos
  • [ Y] Searched existing issues and docs

Expected behaviour

Handler should be called once

Actual behaviour

Handler gets Called once, then another 3 times within microseconds?

Steps to reproduce

I have no idea, why. The Code Explains some debugging atempts Tested with Chrome and Firefox

Working code to debug


e.GET("/test/:uid", db.TestAlertGet, db.Auth) // Has a Bug
e.GET("/hello", db.TestAlertGet, db.Auth) // (For test) Same Handler on another route Has no bug!!!
// I also disabled all other handler to see if I still have that confilct.

// The problem function
func (p *Pgx) TestAlertGet(c echo.Context) error {
	uid := "404b1175-60c6-4c34-99f4-2371045bb1a1"  //c.Param("uid") changed to Static to see if c.Param causing the bug.
	userId := c.Get("ID").(int64)
	res, err := p.q.GetApiFromWebhookId(ctx, qq.GetApiFromWebhookIdParams{
		WebhookID: uid,
		UserID:    userId,
	})
	if err != nil {
		log.Println(err)
	}

// I only get the Bug when I render a Template!
	err = c.Render(200, "alert_test.html", TemplateData[qq.GetApiFromWebhookIdRow]{
		Title: "Test",
		Error: "",
		Data:  res,
	})
	if err != nil {
		log.Println(err)
	}
	return err
}

Edit: The Function gets called for every .css file I locally serve. Removing them fixes the multiple Calls .

Edit2: Fixed it

I included the stylsheets wrong
    <link rel="stylesheet" href="main.css">   | false gets the error
    <link rel="stylesheet" href="/main.css">  | correct no bugs anymore

But still I am confused why under some conditions a fault in my HTML Header leads to the function being called multiple times.

DawnKosmos avatar Jul 06 '23 15:07 DawnKosmos

  1. You should check browser network tab if there are multiple calls to that path
  2. You should configure debugger for your IDE and debug that handler. Some guides for popular IDEs Goland and Visual Studio Code
  3. Something I noticed. your first block:
	if err != nil {
		log.Println(err)
	}

should return err otherwise you will call render even for GetApiFromWebhookId errors.

other than that - issue does not have enough information to say why it is called.

aldas avatar Jul 06 '23 16:07 aldas

learning how to debug - line by line, could be your most significant "level up" in development career

aldas avatar Jul 06 '23 16:07 aldas