mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
add 'context.StopWithStatus, StopWithJSON, StopWithProblem' and update the json-struct-validation example
Former-commit-id: dd0347f22324ef4913be284082b8afc6229206a8
This commit is contained in:
@@ -249,12 +249,32 @@ type Context interface {
|
||||
// Skip skips/ignores the next handler from the handlers chain,
|
||||
// it should be used inside a middleware.
|
||||
Skip()
|
||||
// StopExecution if called then the following .Next calls are ignored,
|
||||
// StopExecution stops the handlers chain of this request.
|
||||
// Meaning that any following `Next` calls are ignored,
|
||||
// as a result the next handlers in the chain will not be fire.
|
||||
StopExecution()
|
||||
// IsStopped checks and returns true if the current position of the Context is 255,
|
||||
// means that the StopExecution() was called.
|
||||
// IsStopped reports whether the current position of the context's handlers is -1,
|
||||
// means that the StopExecution() was called at least once.
|
||||
IsStopped() bool
|
||||
// StopWithJSON stops the handlers chain and writes the "statusCode".
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
StopWithStatus(statusCode int)
|
||||
// StopWithJSON stops the handlers chain, writes the status code
|
||||
// and sends a JSON response.
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
StopWithJSON(statusCode int, jsonObject interface{})
|
||||
// StopWithProblem stops the handlers chain, writes the status code
|
||||
// and sends an application/problem+json response.
|
||||
// See `iris.NewProblem` to build a "problem" value correctly.
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
StopWithProblem(statusCode int, problem Problem)
|
||||
|
||||
// OnConnectionClose registers the "cb" function which will fire (on its own goroutine, no need to be registered goroutine by the end-dev)
|
||||
// when the underlying connection has gone away.
|
||||
//
|
||||
@@ -1452,18 +1472,50 @@ func (ctx *context) Skip() {
|
||||
|
||||
const stopExecutionIndex = -1 // I don't set to a max value because we want to be able to reuse the handlers even if stopped with .Skip
|
||||
|
||||
// StopExecution if called then the following .Next calls are ignored,
|
||||
// StopExecution stops the handlers chain of this request.
|
||||
// Meaning that any following `Next` calls are ignored,
|
||||
// as a result the next handlers in the chain will not be fire.
|
||||
func (ctx *context) StopExecution() {
|
||||
ctx.currentHandlerIndex = stopExecutionIndex
|
||||
}
|
||||
|
||||
// IsStopped checks and returns true if the current position of the context is -1,
|
||||
// means that the StopExecution() was called.
|
||||
// IsStopped reports whether the current position of the context's handlers is -1,
|
||||
// means that the StopExecution() was called at least once.
|
||||
func (ctx *context) IsStopped() bool {
|
||||
return ctx.currentHandlerIndex == stopExecutionIndex
|
||||
}
|
||||
|
||||
// StopWithJSON stops the handlers chain and writes the "statusCode".
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
func (ctx *context) StopWithStatus(statusCode int) {
|
||||
ctx.StopExecution()
|
||||
ctx.StatusCode(statusCode)
|
||||
}
|
||||
|
||||
// StopWithJSON stops the handlers chain, writes the status code
|
||||
// and sends a JSON response.
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
func (ctx *context) StopWithJSON(statusCode int, jsonObject interface{}) {
|
||||
ctx.StopWithStatus(statusCode)
|
||||
ctx.JSON(jsonObject)
|
||||
}
|
||||
|
||||
// StopWithProblem stops the handlers chain, writes the status code
|
||||
// and sends an application/problem+json response.
|
||||
// See `iris.NewProblem` to build a "problem" value correctly.
|
||||
//
|
||||
// If the status code is a failure one then
|
||||
// it will also fire the specified error code handler.
|
||||
func (ctx *context) StopWithProblem(statusCode int, problem Problem) {
|
||||
ctx.StopWithStatus(statusCode)
|
||||
problem.Status(statusCode)
|
||||
ctx.Problem(problem)
|
||||
}
|
||||
|
||||
// OnConnectionClose registers the "cb" function which will fire (on its own goroutine, no need to be registered goroutine by the end-dev)
|
||||
// when the underlying connection has gone away.
|
||||
//
|
||||
@@ -3641,7 +3693,13 @@ func (ctx *context) Problem(v interface{}, opts ...ProblemOptions) (int, error)
|
||||
// }
|
||||
p.updateURIsToAbs(ctx)
|
||||
code, _ := p.getStatus()
|
||||
ctx.StatusCode(code)
|
||||
if code == 0 { // get the current status code and set it to the problem.
|
||||
code = ctx.GetStatusCode()
|
||||
ctx.StatusCode(code)
|
||||
} else {
|
||||
// send the problem's status code
|
||||
ctx.StatusCode(code)
|
||||
}
|
||||
|
||||
if options.RenderXML {
|
||||
ctx.contentTypeOnce(ContentXMLProblemHeaderValue, "")
|
||||
|
||||
@@ -78,7 +78,7 @@ func (p Problem) updateURIsToAbs(ctx Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if uriRef := p.getURI("type"); uriRef != "" {
|
||||
if uriRef := p.getURI("type"); uriRef != "" && !strings.HasPrefix(uriRef, "http") {
|
||||
p.Type(ctx.AbsoluteURI(uriRef))
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func (p Problem) Key(key string, value interface{}) Problem {
|
||||
//
|
||||
// Empty URI or "about:blank", when used as a problem type,
|
||||
// indicates that the problem has no additional semantics beyond that of the HTTP status code.
|
||||
// When "about:blank" is used,
|
||||
// When "about:blank" is used and "title" was not set-ed,
|
||||
// the title is being automatically set the same as the recommended HTTP status phrase for that code
|
||||
// (e.g., "Not Found" for 404, and so on) on `Status` call.
|
||||
//
|
||||
@@ -151,15 +151,16 @@ func (p Problem) Title(title string) Problem {
|
||||
func (p Problem) Status(statusCode int) Problem {
|
||||
shouldOverrideTitle := !p.keyExists("title")
|
||||
|
||||
if !shouldOverrideTitle {
|
||||
typ, found := p["type"]
|
||||
shouldOverrideTitle = !found || isEmptyTypeURI(typ.(string))
|
||||
}
|
||||
// if !shouldOverrideTitle {
|
||||
// typ, found := p["type"]
|
||||
// shouldOverrideTitle = !found || isEmptyTypeURI(typ.(string))
|
||||
// }
|
||||
|
||||
if shouldOverrideTitle {
|
||||
// Set title by code.
|
||||
p.Title(http.StatusText(statusCode))
|
||||
}
|
||||
|
||||
return p.Key("status", statusCode)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user