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

Is there any way to show a message directly in the showMessage function, and don't execute the method after this function #1267

Closed
ThomasZenX opened this issue Jun 1, 2019 · 4 comments

Comments

@ThomasZenX
Copy link

commented Jun 1, 2019

func showMessage(ctx iris.Context, msg string, code int) {
	ctx.JSON(iris.Map{
		"msg":    msg,
		"code":   code,
	})
        // add some thing here to output result, and don't execute the method after this function
        // like php exit funcion , eg. exit("{msg: "hello",code: 1}");
}

func main() {
    app := iris.Default()
    app.Get("/Hello", func(ctx iris.Context) {
        name := ctx.Params().Get("name")
        if name == "" {
                showMessage(ctx, "error", 1)
                return // directly output the response result and don't add the "return"
        }
        showMessage(ctx, "success", 1)
    })
    app.Run(iris.Addr(":8080"))
}
@kataras

This comment has been minimized.

Copy link
Owner

commented Jun 1, 2019

Hello, I don't quite understand, the ctx.JSON will send the response and the return is required at your situtation in order to not continue to the rest of the function. But if you mean you want to execute a chain of handlers and stop to the current handler you can use the ctx.StopExecution() to not respect any following ctx.Next() calls

@ThomasZenX

This comment has been minimized.

Copy link
Author

commented Jun 2, 2019

Hello, I don't quite understand, the ctx.JSON will send the response and the return is required at your situtation in order to not continue to the rest of the function. But if you mean you want to execute a chain of handlers and stop to the current handler you can use the ctx.StopExecution() to not respect any following ctx.Next() calls

I add ctx.StopExecution() to showMessage function, but the second showMessage still will be executed. Is it only possible to add a return after showMessage function in order to terminate the execution of the following content?

func showMessage(ctx iris.Context, msg string, code int) {
	ctx.JSON(iris.Map{
		"msg": msg,
		"code": code,
	})
	ctx.StopExecution()
}

func main() {
	app := iris.Default()
	app.Get("/Hello", func(ctx iris.Context) {
		showMessage(ctx, "success", 1)
		showMessage(ctx, "success", 1) // I add ctx.StopExecution() to showMessage function, but the second showMessage still will be executed
	})
	app.Run(iris.Addr(":8080"))
}
@kataras

This comment has been minimized.

Copy link
Owner

commented Jun 6, 2019

@ThomasZenX This is basic programming my friend... ctx.StopExecution will help you on middlewares - chain of handlers, the thing you are asking here is that the GO function is not stopped on your first showMessage, the simple answer is:

func showMessage(ctx iris.Context, msg string, code int) {
	ctx.JSON(iris.Map{
		"msg": msg,
		"code": code,
	})
	ctx.StopExecution()
}

func main() {
	app := iris.Default()
	app.Get("/Hello", func(ctx iris.Context) {
		showMessage(ctx, "success", 1)
                return // <- this will stop the execution.
		showMessage(ctx, "success", 1) 
	})
	app.Run(iris.Addr(":8080"))
}

The handler is finished when the function exits.

Here is how you can use ctx.StopExecution (we have many examples, please chekc them):

func showMessage(msg string, code int) iris.Handler {
   return func(ctx iris.Context) {
	ctx.JSON(iris.Map{
		"msg": msg,
		"code": code,
	})
       //  if [custom_logic...] {
       ctx.StopExecution()
      //   }
  }
}

func main() {
	app := iris.Default()
	app.Get("/Hello", showMessage("fail", 500),  func(ctx iris.Context) {
                // This will NOT be executed because
                // the "showMessage" middleware  stopped the execution.
		ctx.HTML("<h1> Hello </h1>")
	})
	app.Run(iris.Addr(":8080"))
}

OR you can use the ctx.IsStopped manually

func showMessage(ctx iris.Context, msg string, code int) iris.Handler {
       if ctx.IsStopped() { return } // <-

	ctx.JSON(iris.Map{
		"msg": msg,
		"code": code,
	})
       //  if [custom_logic...] {
       ctx.StopExecution()
      //   }
  }
}

func main() {
	app := iris.Default()
	app.Get("/Hello", func(ctx iris.Context) {
		showMessage(ctx, "success", 1)
		showMessage(ctx, "success", 1) 
	})
	app.Run(iris.Addr(":8080"))
}

Also, you should know that you can use the iris/context/ResponseRecorder or transactions to gain more control to the data sent accross your handlers.

Hope that helped you!

@kataras kataras added the question label Jun 6, 2019

@ThomasZenX

This comment has been minimized.

Copy link
Author

commented Jun 10, 2019

I have this requirement because when I use showMessage to output JSON data, I occasionally forget to add "return" after showMessage. Then it will continue to execute the business behind showMessage.

func showMessage(ctx iris.Context, msg string, code int) iris.Handler {
if ctx.IsStopped() { return } // <-

ctx.JSON(iris.Map{
	"msg": msg,
	"code": code,
})
   //  if [custom_logic...] {
   ctx.StopExecution()
  //   }

}
}

func main() {
app := iris.Default()
app.Get("/Hello", func(ctx iris.Context) {

	if addUserFailedBecauseTheUsernameIsShortThan3Chars {
		showMessage(ctx, "error", 0)
	}
	insertUserToDb(...)
	showMessage(ctx, "success", 1) 
})
app.Run(iris.Addr(":8080"))

}

@kataras kataras closed this Aug 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.