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

Errors encountered during rendering do not assign the type ErrorTypeRender #3954

Open
nicholastanx opened this issue May 7, 2024 · 2 comments

Comments

@nicholastanx
Copy link

Description

In summary, ErrorTypeRender does not appear to be used anywhere in the code.

We discovered this issue when we were having marshalling failures in context.JSON(). We tried checking context.Errors for errors with type ErrorTypeRender, and realised that nowhere in the code is that set.

Could open a PR for this but wanted to confirm if it was intended or not, since ErrorTypes are super old. I could not find any thread or issue that would suggest it though.

How to reproduce

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/gin-gonic/gin"
)

type ProblemObj struct {
}

func (f *ProblemObj) MarshalJSON() ([]byte, error) {
	return nil, fmt.Errorf("this is an error")
}

func main() {
	c, _ := gin.CreateTestContext(httptest.NewRecorder())
	prob := &ProblemObj{}
	c.JSON(http.StatusOK, prob)
	for _, err := range c.Errors {
		fmt.Println(err.IsType(gin.ErrorTypeRender))
	}
}

Expectations

The test code should print true, since the error was occurred during rendering.

Actual result

The test code prints false.

Environment

  • go version: v1.21.1
  • gin version (or commit ref): v1.9.1 and v1.10.0
  • operating system: Mac AMD64
@RedCrazyGhost
Copy link
Contributor

I checked the relevant functions and submission history. This method is quite old and modifications may have a greater impact.
I feel that it is not suitable to modify it directly. It is more appropriate to write a branch function and handle it separately for Render.

@dukepan2005
Copy link

@RedCrazyGhost
What impact?

I don't see any impact, just update following code:

// Render writes the response headers and calls render.Render to render data.
func (c *Context) Render(code int, r render.Render) {
	c.Status(code)

	if !bodyAllowedForStatus(code) {
		r.WriteContentType(c.Writer)
		c.Writer.WriteHeaderNow()
		return
	}

	if err := r.Render(c.Writer); err != nil {
		// Pushing error to c.Errors
		_ = c.Error(err)
		c.Abort()
	}
}

replace
_ = c.Error(err)

with
_ = c.Error(err).SetType(ErrorTypeRender)

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

No branches or pull requests

3 participants