1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

omit errors received by the server via configuration 🍪 | requested by https://github.com/kataras/iris/issues/668

relative link: https://github.com/kataras/iris/issues/668


Former-commit-id: 6491abd68b74e18bf4ed0b32406e67597c9b55a9
This commit is contained in:
hiveminded
2017-07-13 16:31:36 +03:00
parent ace439203d
commit 16ccb2edc4
12 changed files with 294 additions and 64 deletions

View File

@@ -16,6 +16,7 @@ It doesn't always contain the "best ways" but it does cover each important featu
### HTTP Listening
- [Common, with address](http-listening/listen-addr/main.go)
* [omit server errors](http-listening/listen-addr/omit-server-errors)
- [UNIX socket file](http-listening/listen-unix/main.go)
- [TLS](http-listening/listen-tls/main.go)
- [Letsencrypt (Automatic Certifications)](http-listening/listen-letsencrypt/main.go)

View File

@@ -1,35 +0,0 @@
// +build go1.9
package main
import (
"github.com/kataras/iris"
)
func main() {
app := iris.Default()
// Method: GET
// Resource: http://localhost:8080/
app.Handle("GET", "/", func(ctx iris.Context) {
ctx.HTML("<b>Hello world!</b>")
})
// same as app.Handle("GET", "/ping", [...])
// Method: GET
// Resource: http://localhost:8080/ping
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString("pong")
})
// Method: GET
// Resource: http://localhost:8080/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello iris web framework."})
})
// http://localhost:8080
// http://localhost:8080/ping
// http://localhost:8080/hello
app.Run(iris.Addr(":8080"))
}

View File

@@ -12,8 +12,6 @@ func main() {
ctx.HTML("<h1>Hello World!/</h1>")
})
if err := app.Run(iris.Addr(":8080")); err != nil {
panic(err)
}
// http://localhost:8080
app.Run(iris.Addr(":8080"))
}

View File

@@ -0,0 +1,24 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.HTML("<h1>Hello World!/</h1>")
})
err := app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
if err != nil {
// do something
}
// same as:
// err := app.Run(iris.Addr(":8080"))
// if err != nil && (err != iris.ErrServerClosed || err.Error() != iris.ErrServerClosed.Error()) {
// [...]
// }
}

View File

@@ -0,0 +1,84 @@
package main
import (
"bytes"
stdContext "context"
"fmt"
"testing"
"time"
"github.com/kataras/iris"
"github.com/sirupsen/logrus"
)
func logger(app *iris.Application) *bytes.Buffer {
buf := &bytes.Buffer{}
app.Logger().Formatter = &logrus.TextFormatter{
DisableColors: true,
DisableSorting: true,
DisableTimestamp: true,
}
app.Logger().Out = buf
// disable the "Now running at...." in order to have a clean log of the error.
// we could attach that on `Run` but better to keep things simple here.
app.Configure(iris.WithoutStartupLog)
return buf
}
func TestListenAddr(t *testing.T) {
app := iris.New()
// we keep the logger running as well but in a controlled way.
log := logger(app)
// close the server at 3-6 seconds
go func() {
time.Sleep(3 * time.Second)
ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
defer cancel()
app.Shutdown(ctx)
}()
err := app.Run(iris.Addr(":9829"))
// in this case the error should be logged and return as well.
if err != iris.ErrServerClosed {
t.Fatalf("expecting err to be `iris.ErrServerClosed` but got: %v", err)
}
// println(log.Bytes())
// println(len(log.Bytes()))
expected := fmt.Sprintln("level=error msg=\"" + iris.ErrServerClosed.Error() + "\" ")
// println([]byte(expected))
// println(len([]byte(expected)))
if got := log.String(); expected != got {
t.Fatalf("expecting to log the:\n'%s'\ninstead of:\n'%s'", expected, got)
}
}
func TestListenAddrWithoutServerErr(t *testing.T) {
app := iris.New()
// we keep the logger running as well but in a controlled way.
log := logger(app)
// close the server at 3-6 seconds
go func() {
time.Sleep(3 * time.Second)
ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
defer cancel()
app.Shutdown(ctx)
}()
// we disable the ErrServerClosed, so the error should be nil when server is closed by `app.Shutdown`.
// so in this case the iris/http.ErrServerClosed should be NOT logged and NOT return.
err := app.Run(iris.Addr(":9827"), iris.WithoutServerError(iris.ErrServerClosed))
if err != nil {
t.Fatalf("expecting err to be nil but got: %v", err)
}
if got := log.String(); got != "" {
t.Fatalf("expecting to log nothing but logged: '%s'", got)
}
}