You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use server app writen on Gin for rendering pdf documents with wkhtmltopdf. wkhtmltopdf receives url with html to transforming it to pdf.
In case if i did some error in template or model (for example, template has variable SomeVar and i passed model without this field) gin's method c.HTML returns 500 and body filled with html (after recover). wkhtmltopdf doesn't check only status, if it see body - it works with it (although it has errors).
But i want to catch those cases and return 500 status with empty body in case if rendering completed with errors.
Are there any way to do it? I think it could be three paths:
Add middleware after endpoint. But i cannot clear response body in middleware. Writer.Write only adds bytes to already written body, not reset it.
Render HTML separately (without auto-writing it to response buffer) and check error. But i didn't find any method for this.
Use recover in handler. But i cannot clear already filled response body in recover func, Abort... funcs don't clear body too.
The text was updated successfully, but these errors were encountered:
nightlord189
changed the title
Any way to check error of render HTML template or clear response body?
Any way to check error of render HTML template or manually clear response body?
Aug 20, 2021
You can replace the gin.Context writer to delay the response using an in memory writer. The response body can later on be sent to the client once it has been completely generated.
This should work :
import(
"bytes""net/http""github.com/gin-gonic/gin"
)
typeInMemoryWriterstruct {
gin.ResponseWriterbody*bytes.Buffer
}
func (writer*InMemoryWriter) Write(b []byte) (int, error) {
returnwriter.body.Write(b)
}
func (writer*InMemoryWriter) WriteString(sstring) (int, error) {
returnwriter.body.WriteString(s)
}
funcDelayedResponse(c*gin.Context, callbackfunc()) {
writer:=&InMemoryWriter{
body: bytes.NewBufferString(""),
ResponseWriter: c.Writer,
}
c.Writer=writercallback()
body:=writer.body.String()
// if everything is ok : send the body to the clientwriter.ResponseWriter.WriteString(body)
}
funcregisterActions(engine*gin.Engine) {
engine.GET("/pdf", func(c*gin.Context) {
DelayedResponse(c, func() {
c.HTML(http.StatusOK, "pdf.tmpl", gin.H{
"foo": "bar"
})
})
})
}
I use server app writen on Gin for rendering pdf documents with wkhtmltopdf. wkhtmltopdf receives url with html to transforming it to pdf.
In case if i did some error in template or model (for example, template has variable SomeVar and i passed model without this field) gin's method
c.HTML
returns 500 and body filled with html (after recover). wkhtmltopdf doesn't check only status, if it see body - it works with it (although it has errors).But i want to catch those cases and return 500 status with empty body in case if rendering completed with errors.
Are there any way to do it? I think it could be three paths:
Writer.Write
only adds bytes to already written body, not reset it.Abort
... funcs don't clear body too.The text was updated successfully, but these errors were encountered: