Install on existing project
diff --git a/README_FA.md b/README_FA.md
index b5fa2637..079d7284 100644
--- a/README_FA.md
+++ b/README_FA.md
@@ -242,7 +242,7 @@ Venkatt Guhesan" title="vguhesan" with="75" style="width:75px;max-width:75px;hei
$ mkdir myapp
$ cd myapp
$ go mod init myapp
-$ go get github.com/kataras/iris/v12@master # or @v12.2.0-alpha8
+$ go get github.com/kataras/iris/v12@master # or @v12.2.0-beta2
```
diff --git a/_examples/README.md b/_examples/README.md
index 8a7477e1..05d93dc8 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -106,6 +106,7 @@
* [Swagger](https://github.com/iris-contrib/swagger/tree/master/_examples/basic)
* [Testing](testing/httptest/main_test.go)
* [Recovery](recover/main.go)
+ * [Panic and custom Error Handler with Compression](recover/panic-and-custom-error-handler-with-compression/main.go)
* [Profiling](pprof/main.go)
* File Server
* [File Server](file-server/file-server/main.go)
diff --git a/_examples/recover/panic-and-custom-error-handler-with-compression/main.go b/_examples/recover/panic-and-custom-error-handler-with-compression/main.go
new file mode 100644
index 00000000..545e9b5a
--- /dev/null
+++ b/_examples/recover/panic-and-custom-error-handler-with-compression/main.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+
+ "github.com/kataras/iris/v12"
+ // "github.com/kataras/iris/v12/context"
+)
+
+func main() {
+ app := iris.New()
+ app.UseRouter(iris.Compression)
+ app.UseRouter(myErrorHandler)
+
+ app.Get("/", handler)
+
+ app.Listen(":8080")
+}
+
+func myErrorHandler(ctx iris.Context) {
+ recorder := ctx.Recorder()
+
+ defer func() {
+ var err error
+
+ if v := recover(); v != nil { // panic
+ if panicErr, ok := v.(error); ok {
+ err = panicErr
+ } else {
+ err = errors.New(fmt.Sprint(v))
+ }
+ } else { // custom error.
+ err = ctx.GetErr()
+ }
+
+ if err != nil {
+ // To keep compression after reset:
+ // clear body and any headers created between recorder and handler.
+ recorder.ResetBody()
+ recorder.ResetHeaders()
+ //
+
+ // To disable compression after reset:
+ // recorder.Reset()
+ // recorder.ResponseWriter.(*context.CompressResponseWriter).Disabled = true
+ //
+
+ ctx.StopWithJSON(iris.StatusInternalServerError, iris.Map{
+ "message": err.Error(),
+ })
+ }
+ }()
+
+ ctx.Next()
+}
+
+func handler(ctx iris.Context) {
+ ctx.WriteString("Content may fall")
+ ctx.Header("X-Test", "value")
+
+ // ctx.SetErr(fmt.Errorf("custom error message"))
+ panic("errr!")
+}
diff --git a/context/context.go b/context/context.go
index 26dc5b4e..992477dc 100644
--- a/context/context.go
+++ b/context/context.go
@@ -841,7 +841,7 @@ func (ctx *Context) StopWithPlainError(statusCode int, err error) {
// it will also fire the specified error code handler.
func (ctx *Context) StopWithJSON(statusCode int, jsonObject interface{}) error {
ctx.StopWithStatus(statusCode)
- _, err := ctx.writeJSON(jsonObject, nil)
+ _, err := ctx.writeJSON(jsonObject, nil) // do not modify - see errors.DefaultContextErrorHandler.
return err
}
@@ -3975,9 +3975,14 @@ type (
// ErrorHandler describes a context error handler which applies on
// JSON, JSONP, Protobuf, MsgPack, XML, YAML and Markdown write errors.
//
+ // It does NOT modify or handle the result of Context.GetErr at all,
+ // use a custom middleware instead if you want to handle the handler-provided custom errors (Context.SetErr)
+ //
// An ErrorHandler can be registered once via Application.SetErrorHandler method to override the default behavior.
// The default behavior is to simply send status internal code error
// without a body back to the client.
+ //
+ // See Application.SetContextErrorHandler for more.
ErrorHandler interface {
HandleContextError(ctx *Context, err error)
}
diff --git a/context/response_recorder.go b/context/response_recorder.go
index de27f8ba..d2162d97 100644
--- a/context/response_recorder.go
+++ b/context/response_recorder.go
@@ -143,6 +143,11 @@ func (w *ResponseRecorder) ClearHeaders() {
// Reset clears headers, sets the status code to 200
// and clears the cached body.
//
+// - Use ResetBody() and ResetHeaders() instead to keep compression after reseting.
+//
+// - Use Reset() & ResponseRecorder.ResponseWriter.(*context.CompressResponseWriter).Disabled = true
+// to set a new body without compression when the previous handler was iris.Compression.
+//
// Implements the `ResponseWriterReseter`.
func (w *ResponseRecorder) Reset() bool {
w.ClearHeaders()
diff --git a/doc.go b/doc.go
index de79828f..cca05463 100644
--- a/doc.go
+++ b/doc.go
@@ -38,7 +38,7 @@ Source code and other details for the project are available at GitHub:
Current Version
-12.2.0-beta1
+12.2.0-beta2
Installation
diff --git a/iris.go b/iris.go
index 7dd9e4e8..697dc562 100644
--- a/iris.go
+++ b/iris.go
@@ -39,7 +39,7 @@ import (
)
// Version is the current version of the Iris Web Framework.
-const Version = "12.2.0-beta1"
+const Version = "12.2.0-beta2"
// Byte unit helpers.
const (
@@ -146,7 +146,7 @@ func Default() *Application {
app.logger.Debugf(`Log level set to "debug"`)
// Register the accesslog middleware.
- logFile, err := os.OpenFile("./access.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
+ logFile, err := os.OpenFile("./access.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err == nil {
// Close the file on shutdown.
app.ConfigureHost(func(su *Supervisor) {
@@ -945,7 +945,8 @@ func TLS(addr string, certFileOrContents, keyFileOrContents string, hostConfigs
func AutoTLS(
addr string,
domain string, email string,
- hostConfigs ...host.Configurator) Runner {
+ hostConfigs ...host.Configurator,
+) Runner {
return func(app *Application) error {
return app.NewHost(&http.Server{Addr: addr}).
Configure(hostConfigs...).