mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
HandleHTTPError MVC Method as requested at #1595. Read HISTORY.md
example at: https://github.com/kataras/iris/tree/master/_examples/mvc/error-handler-http
This commit is contained in:
59
_examples/mvc/error-handler-http/main.go
Normal file
59
_examples/mvc/error-handler-http/main.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Logger().SetLevel("debug")
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
m := mvc.New(app)
|
||||
m.Handle(new(controller))
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
type controller struct{}
|
||||
|
||||
func (c *controller) Get() string {
|
||||
return "Hello!"
|
||||
}
|
||||
|
||||
// The input parameter of mvc.Code is optional but a good practise to follow.
|
||||
// You could register a Context and get its error code through ctx.GetStatusCode().
|
||||
//
|
||||
// This can accept dependencies and output values like any other Controller Method,
|
||||
// however be careful if your registered dependencies depend only on succesful(200...) requests.
|
||||
//
|
||||
// Also note that, if you register more than one controller.HandleHTTPError
|
||||
// in the same Party, you need to use the RouteOverlap feature as shown
|
||||
// in the "authenticated-controller" example, and a dependency on
|
||||
// a controller's field (or method's input argument) is required
|
||||
// to select which, between those two controllers, is responsible
|
||||
// to handle http errors.
|
||||
func (c *controller) HandleHTTPError(statusCode mvc.Code) mvc.View {
|
||||
code := int(statusCode) // cast it to int.
|
||||
|
||||
view := mvc.View{
|
||||
Code: code,
|
||||
Name: "unexpected-error.html",
|
||||
}
|
||||
|
||||
switch code {
|
||||
case 404:
|
||||
view.Name = "404.html"
|
||||
// [...]
|
||||
case 500:
|
||||
view.Name = "500.html"
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
20
_examples/mvc/error-handler-http/main_test.go
Normal file
20
_examples/mvc/error-handler-http/main_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
)
|
||||
|
||||
func TestControllerHandleHTTPError(t *testing.T) {
|
||||
const (
|
||||
expectedIndex = "Hello!"
|
||||
expectedNotFound = "<h3>Not Found Custom Page Rendered through Controller's HandleHTTPError</h3>"
|
||||
)
|
||||
|
||||
app := newApp()
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(expectedIndex)
|
||||
e.GET("/a_notefound").Expect().Status(httptest.StatusNotFound).ContentType("text/html").Body().Equal(expectedNotFound)
|
||||
}
|
||||
1
_examples/mvc/error-handler-http/views/404.html
Normal file
1
_examples/mvc/error-handler-http/views/404.html
Normal file
@@ -0,0 +1 @@
|
||||
<h3>Not Found Custom Page Rendered through Controller's HandleHTTPError</h3>
|
||||
1
_examples/mvc/error-handler-http/views/500.html
Normal file
1
_examples/mvc/error-handler-http/views/500.html
Normal file
@@ -0,0 +1 @@
|
||||
<h3>Internal Server Err</h3>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Unexpected Error</h1>
|
||||
Reference in New Issue
Block a user