Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 738a84f

Browse filesBrowse files
committed
add HTTPCode() for Controller.RenderError()
1 parent 5c52331 commit 738a84f
Copy full SHA for 738a84f

File tree

1 file changed

+12
-5
lines changed
Filter options

1 file changed

+12
-5
lines changed

‎controller.go

Copy file name to clipboardExpand all lines: controller.go
+12-5Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package revel
77
import (
88
"errors"
99
"fmt"
10-
"github.com/revel/revel/logger"
1110
"io"
1211
"net/http"
1312
"os"
@@ -16,6 +15,8 @@ import (
1615
"runtime"
1716
"strings"
1817
"time"
18+
19+
"github.com/revel/revel/logger"
1920
)
2021

2122
// Controller Revel's controller structure that gets embedded in user defined
@@ -41,6 +42,7 @@ type Controller struct {
4142
Validation *Validation // Data validation helpers
4243
Log logger.MultiLogger // Context Logger
4344
}
45+
4446
// The map of controllers, controllers are mapped by using the namespace|controller_name as the key
4547
var controllers = make(map[string]*ControllerType)
4648
var controllerLog = RevelLog.New("section", "controller")
@@ -119,8 +121,16 @@ func (c *Controller) SetCookie(cookie *http.Cookie) {
119121
c.Response.Out.internalHeader.SetCookie(cookie.String())
120122
}
121123

124+
type ErrorCoder interface {
125+
HTTPCode() int
126+
}
127+
122128
func (c *Controller) RenderError(err error) Result {
123-
c.setStatusIfNil(http.StatusInternalServerError)
129+
if coder, ok := err.(ErrorCoder); ok {
130+
c.setStatusIfNil(coder.HTTPCode())
131+
} else {
132+
c.setStatusIfNil(http.StatusInternalServerError)
133+
}
124134

125135
return ErrorResult{c.ViewArgs, err}
126136
}
@@ -486,9 +496,6 @@ func findControllers(appControllerType reflect.Type) (indexes [][]int) {
486496
return
487497
}
488498

489-
490-
491-
492499
// RegisterController registers a Controller and its Methods with Revel.
493500
func RegisterController(c interface{}, methods []*MethodType) {
494501
// De-star the controller type

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.