add OmitErrorHandler on rest Options

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-04-19 16:16:44 +03:00
parent 18b85f14dd
commit 2eb56ab525
No known key found for this signature in database
GPG Key ID: 66FCC29BD385FCA6

View File

@ -839,9 +839,10 @@ func (ctx *Context) StopWithPlainError(statusCode int, err error) {
// //
// If the status code is a failure one then // If the status code is a failure one then
// it will also fire the specified error code handler. // it will also fire the specified error code handler.
func (ctx *Context) StopWithJSON(statusCode int, jsonObject interface{}) { func (ctx *Context) StopWithJSON(statusCode int, jsonObject interface{}) error {
ctx.StopWithStatus(statusCode) ctx.StopWithStatus(statusCode)
ctx.JSON(jsonObject) _, err := ctx.writeJSON(jsonObject, nil)
return err
} }
// StopWithProblem stops the handlers chain, writes the status code // StopWithProblem stops the handlers chain, writes the status code
@ -850,10 +851,11 @@ func (ctx *Context) StopWithJSON(statusCode int, jsonObject interface{}) {
// //
// If the status code is a failure one then // If the status code is a failure one then
// it will also fire the specified error code handler. // it will also fire the specified error code handler.
func (ctx *Context) StopWithProblem(statusCode int, problem Problem) { func (ctx *Context) StopWithProblem(statusCode int, problem Problem) error {
ctx.StopWithStatus(statusCode) ctx.StopWithStatus(statusCode)
problem.Status(statusCode) problem.Status(statusCode)
ctx.Problem(problem) _, err := ctx.Problem(problem)
return err
} }
// +------------------------------------------------------------+ // +------------------------------------------------------------+
@ -3769,6 +3771,11 @@ type JSON struct {
Secure bool `yaml:"Secure"` // if true then it prepends a "while(1);" when Go slice (to JSON Array) value. Secure bool `yaml:"Secure"` // if true then it prepends a "while(1);" when Go slice (to JSON Array) value.
// proto.Message specific marshal options. // proto.Message specific marshal options.
Proto ProtoMarshalOptions `yaml:"ProtoMarshalOptions"` Proto ProtoMarshalOptions `yaml:"ProtoMarshalOptions"`
// If true and json writing failed then the error handler is skipped
// and it just returns to the caller.
//
// See StopWithJSON and x/errors package.
OmitErrorHandler bool `yaml:"OmitErrorHandler"`
} }
// DefaultJSONOptions is the optional settings that are being used // DefaultJSONOptions is the optional settings that are being used
@ -3791,6 +3798,7 @@ type JSONP struct {
// content-specific // content-specific
Indent string Indent string
Callback string Callback string
OmitErrorHandler bool // See JSON.OmitErrorHandler.
} }
// XML contains the options for the XML (Context's) Renderer. // XML contains the options for the XML (Context's) Renderer.
@ -3798,12 +3806,14 @@ type XML struct {
// content-specific // content-specific
Indent string Indent string
Prefix string Prefix string
OmitErrorHandler bool // See JSON.OmitErrorHandler.
} }
// Markdown contains the options for the Markdown (Context's) Renderer. // Markdown contains the options for the Markdown (Context's) Renderer.
type Markdown struct { type Markdown struct {
// content-specific // content-specific
Sanitize bool Sanitize bool
OmitErrorHandler bool // See JSON.OmitErrorHandler.
} }
var ( var (
@ -4020,8 +4030,12 @@ func (ctx *Context) JSON(v interface{}, opts ...JSON) (n int, err error) {
} }
if n, err = ctx.writeJSON(v, options); err != nil { if n, err = ctx.writeJSON(v, options); err != nil {
// if no options are given or OmitErrorHandler is true
// then do call the error handler (which may lead to a cycle).
if options == nil || !options.OmitErrorHandler {
ctx.handleContextError(err) ctx.handleContextError(err)
} }
}
return return
} }
@ -4115,8 +4129,10 @@ func (ctx *Context) JSONP(v interface{}, opts ...JSONP) (n int, err error) {
ctx.ContentType(ContentJavascriptHeaderValue) ctx.ContentType(ContentJavascriptHeaderValue)
if n, err = WriteJSONP(ctx.writer, v, options, ctx.shouldOptimize()); err != nil { if n, err = WriteJSONP(ctx.writer, v, options, ctx.shouldOptimize()); err != nil {
if !options.OmitErrorHandler {
ctx.handleContextError(err) ctx.handleContextError(err)
} }
}
return return
} }
@ -4211,8 +4227,10 @@ func (ctx *Context) XML(v interface{}, opts ...XML) (n int, err error) {
ctx.ContentType(ContentXMLHeaderValue) ctx.ContentType(ContentXMLHeaderValue)
if n, err = WriteXML(ctx.writer, v, options, ctx.shouldOptimize()); err != nil { if n, err = WriteXML(ctx.writer, v, options, ctx.shouldOptimize()); err != nil {
if !options.OmitErrorHandler {
ctx.handleContextError(err) ctx.handleContextError(err)
} }
}
return return
} }
@ -4261,7 +4279,7 @@ func (ctx *Context) Problem(v interface{}, opts ...ProblemOptions) (int, error)
} }
ctx.contentTypeOnce(ContentJSONProblemHeaderValue, "") ctx.contentTypeOnce(ContentJSONProblemHeaderValue, "")
return ctx.JSON(v, options.JSON) return ctx.writeJSON(v, &options.JSON)
} }
// WriteMarkdown parses the markdown to html and writes these contents to the writer. // WriteMarkdown parses the markdown to html and writes these contents to the writer.
@ -4291,8 +4309,10 @@ func (ctx *Context) Markdown(markdownB []byte, opts ...Markdown) (n int, err err
ctx.ContentType(ContentHTMLHeaderValue) ctx.ContentType(ContentHTMLHeaderValue)
if n, err = WriteMarkdown(ctx.writer, markdownB, options); err != nil { if n, err = WriteMarkdown(ctx.writer, markdownB, options); err != nil {
if !options.OmitErrorHandler {
ctx.handleContextError(err) ctx.handleContextError(err)
} }
}
return return
} }